Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove relative imports from tests #52

Open
mcepl opened this issue Oct 31, 2022 · 1 comment
Open

Remove relative imports from tests #52

mcepl opened this issue Oct 31, 2022 · 1 comment
Assignees

Comments

@mcepl
Copy link

mcepl commented Oct 31, 2022

While packaging this module for openSUSE, we have encountered this error:

[   24s] + PYTHONPATH=/home/abuild/rpmbuild/BUILDROOT/python-ast-decompiler-0.7.0-0.x86_64/usr/lib/python3.8/site-packages
[   24s] + PYTHONDONTWRITEBYTECODE=1
[   24s] + pytest-3.8 --ignore=_build.python38 --ignore=_build.python39 --ignore=_build.python310 -v tests/
[   24s] ============================= test session starts ==============================
[   24s] platform linux -- Python 3.8.15, pytest-7.1.2, pluggy-1.0.0 -- /usr/bin/python3.8
[   24s] cachedir: .pytest_cache
[   24s] rootdir: /home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0
[   24s] collecting ... collected 0 items / 7 errors
[   24s] 
[   24s] ==================================== ERRORS ====================================
[   24s] _____________________ ERROR collecting tests/test_basic.py _____________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_basic.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_basic.py:3: in <module>
[   24s]     from .tests import assert_decompiles, check, only_on_version
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] __________________ ERROR collecting tests/test_indentation.py __________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_indentation.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_indentation.py:1: in <module>
[   24s]     from .tests import assert_decompiles
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] __________________ ERROR collecting tests/test_line_length.py __________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_line_length.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_line_length.py:1: in <module>
[   24s]     from .tests import assert_decompiles
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] ____________________ ERROR collecting tests/test_literal.py ____________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_literal.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_literal.py:1: in <module>
[   24s]     from .tests import assert_decompiles, only_on_version
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] _____________________ ERROR collecting tests/test_patma.py _____________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_patma.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_patma.py:1: in <module>
[   24s]     from .tests import check, skip_before
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] __________________ ERROR collecting tests/test_precedence.py ___________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_precedence.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_precedence.py:1: in <module>
[   24s]     from .tests import check, only_on_version
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] __________________ ERROR collecting tests/test_py3_syntax.py ___________________
[   24s] ImportError while importing test module '/home/abuild/rpmbuild/BUILD/ast_decompiler-0.7.0/tests/test_py3_syntax.py'.
[   24s] Hint: make sure your test modules/packages have valid Python names.
[   24s] Traceback:
[   24s] /usr/lib64/python3.8/importlib/__init__.py:127: in import_module
[   24s]     return _bootstrap._gcd_import(name[level:], package, level)
[   24s] tests/test_py3_syntax.py:1: in <module>
[   24s]     from .tests import check, skip_before, skip_after
[   24s] E   ImportError: attempted relative import with no known parent package
[   24s] =========================== short test summary info ============================
[   24s] ERROR tests/test_basic.py
[   24s] ERROR tests/test_indentation.py
[   24s] ERROR tests/test_line_length.py
[   24s] ERROR tests/test_literal.py
[   24s] ERROR tests/test_patma.py
[   24s] ERROR tests/test_precedence.py
[   24s] ERROR tests/test_py3_syntax.py
[   24s] !!!!!!!!!!!!!!!!!!! Interrupted: 7 errors during collection !!!!!!!!!!!!!!!!!!!!
[   24s] ============================== 7 errors in 0.11s ===============================

Complete build log showing all packages used with their versions and steps taken.

In my opinion the problem are relative imports. And really, when I apply this patch:

---
 tests/test_basic.py       |    2 +-
 tests/test_indentation.py |    2 +-
 tests/test_line_length.py |    2 +-
 tests/test_literal.py     |    2 +-
 tests/test_patma.py       |    2 +-
 tests/test_precedence.py  |    2 +-
 tests/test_py3_syntax.py  |    2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)

--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -1,6 +1,6 @@
 import ast
 from ast_decompiler import decompile
-from .tests import assert_decompiles, check, only_on_version
+from tests import assert_decompiles, check, only_on_version
 
 
 def test_non_module() -> None:
--- a/tests/test_indentation.py
+++ b/tests/test_indentation.py
@@ -1,4 +1,4 @@
-from .tests import assert_decompiles
+from tests import assert_decompiles
 
 
 def test_indentation() -> None:
--- a/tests/test_line_length.py
+++ b/tests/test_line_length.py
@@ -1,4 +1,4 @@
-from .tests import assert_decompiles
+from tests import assert_decompiles
 
 
 def check_split(original: str, multiline: str, length_reduction: int = 2) -> None:
--- a/tests/test_literal.py
+++ b/tests/test_literal.py
@@ -1,4 +1,4 @@
-from .tests import assert_decompiles, only_on_version
+from tests import assert_decompiles, only_on_version
 
 
 def test_With() -> None:
--- a/tests/test_patma.py
+++ b/tests/test_patma.py
@@ -1,4 +1,4 @@
-from .tests import check, skip_before
+from tests import check, skip_before
 
 
 @skip_before((3, 10))
--- a/tests/test_precedence.py
+++ b/tests/test_precedence.py
@@ -1,4 +1,4 @@
-from .tests import check, only_on_version
+from tests import check, only_on_version
 
 
 def test_Yield() -> None:
--- a/tests/test_py3_syntax.py
+++ b/tests/test_py3_syntax.py
@@ -1,4 +1,4 @@
-from .tests import check, skip_before, skip_after
+from tests import check, skip_before, skip_after
 
 
 @skip_before((3, 5))

then the problem goes away.

@troyfigiel
Copy link

I just encountered the same issue when packaging this module for Guix. The problem arises because tests/__init__.py is not included in the distributed version. Building directly from git works fine for this reason. An alternative fix would be to add this init file in the include section of flit in pyproject.toml.

@JelleZijlstra JelleZijlstra self-assigned this Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants