scientific-python / pytest-doctestplus

Pytest plugin providing advanced doctest features
Other
94 stars 38 forks source link

Can’t handle relative imports #230

Closed flying-sheep closed 11 months ago

flying-sheep commented 11 months ago

Trying to use doctestplus results in

scanpy/_utils/compute/is_constant.py:13: in <module>
    from ..._compat import DaskArray
E   ImportError: attempted relative import with no known parent package
pllim commented 11 months ago

Hmm pretty sure I have ran doctestplus in package with relative imports. Did you install the package first?

bsipocz commented 11 months ago

We do use relative imports in packages using pytest-doctestplus, but I don't think we have any actual doctest examples in the test suite that actually have relative imports in the example itself.

Can you copy the full traceback, or links to where you see this? I assume this failure you see is in the doctest and not in the file level? (that being said, using relative imports in an actual example doesn't look like best practice, at least IMO, examples should work as copy-paste for the users)

flying-sheep commented 11 months ago

I think this is just #231, since it triggers in import_path, and when I edited pytest-doctestplus’s code so it uses mode=ImportMode.importlib, the error went away.

____________________________________________________________________________________________ ERROR collecting scanpy/_utils/compute/is_constant.py ____________________________________________________________________________________________
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/runner.py:372: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/pytest_doctestplus/plugin.py:250: in collect
    module = import_path(fspath, root=self.config.rootpath)
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/usr/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:940: in exec_module
    ???
<frozen importlib._bootstrap>:241: in _call_with_frames_removed
    ???
scanpy/_utils/compute/is_constant.py:13: in <module>
    from ..._compat import DaskArray
E   ImportError: attempted relative import with no known parent package
____________________________________________________________________________________________ ERROR collecting scanpy/neighbors/_backends/rapids.py ____________________________________________________________________________________________
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/runner.py:372: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/pytest_doctestplus/plugin.py:250: in collect
    module = import_path(fspath, root=self.config.rootpath)
/home/phil/.local/share/hatch/env/virtual/scanpy/q4In3tK-/test.py3.11/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/usr/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:940: in exec_module
    ???
<frozen importlib._bootstrap>:241: in _call_with_frames_removed
    ???
scanpy/neighbors/_backends/rapids.py:12: in <module>
    from ..._settings import settings
E   ImportError: attempted relative import with no known parent package