Skip to content

Commit

Permalink
Add magics with old behavior + deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
chmp committed Jul 28, 2021
1 parent b61247f commit 87c1841
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions ipytest/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,74 @@ def configure_magics(enable):
if enable:
shell = get_ipython()
shell.register_magic_function(pytest_magic, "cell", "ipytest")
shell.register_magic_function(_deprecated_magic, "cell", "run_pytest")
shell.register_magic_function(_deprecated_magic, "cell", "run_pytest[clean]")
shell.register_magic_function(_deprecated_pytest_magic, "cell", "run_pytest")
shell.register_magic_function(
_deprecated_pytest_clean_magic, "cell", "run_pytest[clean]"
)

else:
warnings.warn("IPython does not support de-registering magics.")


def _deprecated_magic(line, cell):
def _deprecated_pytest_magic(line, cell):
print(
"%%run_pytest[clean] and %%run_pytest are deprecated in favor of "
"%%ipytest. %%ipytest will clean tests, evaluate the cell and then "
"run pytest. To disable cleaning, configure ipytest with "
"ipytest.config(clean=False).",
file=sys.stderr,
)
from ._impl import pytest_magic
import shlex
from IPython import get_ipython
from ._impl import run

try:
get_ipython().run_cell(cell)

except TypeError as e:
if "raw_cell" in str(e):
raise RuntimeError(
"The ipytest magic cannot evaluate the cell. Most likely you "
"are running a modified ipython version. Consider using "
"`ipytest.run` and `ipytest.clean_tests` directly."
) from e

else:
raise e

run(*shlex.split(line))


def _deprecated_pytest_clean_magic(line, cell):
print(
"%%run_pytest[clean] and %%run_pytest are deprecated in favor of "
"%%ipytest. %%ipytest will clean tests, evaluate the cell and then "
"run pytest. To disable cleaning, configure ipytest with "
"ipytest.config(clean=False).",
file=sys.stderr,
)
import shlex
from IPython import get_ipython
from ._impl import clean_tests, run

if current_config["clean"] is not False:
clean_tests(current_config["clean"])

try:
get_ipython().run_cell(cell)

except TypeError as e:
if "raw_cell" in str(e):
raise RuntimeError(
"The ipytest magic cannot evaluate the cell. Most likely you "
"are running a modified ipython version. Consider using "
"`ipytest.run` and `ipytest.clean_tests` directly."
) from e

else:
raise e

pytest_magic(line, cell)
run(*shlex.split(line))


def replace_with_default(sentinel, value, default):
Expand Down

0 comments on commit 87c1841

Please sign in to comment.