Fix relative import resolution in namespace packages - #3178
Fix relative import resolution in namespace packages#3178TheGreatApollyon wants to merge 3 commits into
Conversation
Fixes pylint-dev#520 Add an inference tip for re.compile that returns an instance of re.Pattern instead of Uninferable. The Pattern/Match synthetic classes now also expose the regular expression methods (match, search, findall, etc.) so attribute access works on compiled patterns.
When a module is part of a namespace package (no __init__.py in its directory), relative_to_absolute_name used to drop the namespace package prefix, so from .subpkg import mod in pkg.main was resolved as subpkg instead of pkg.subpkg, failing with AstroidImportError. Keep the prefix from the module name so the relative import resolves against the namespace package. Modules whose name carries no package prefix (e.g. built from a file not on sys.path) keep the previous behavior. Closes pylint-dev#2766
for more information, see https://pre-commit.ci
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (88.46%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #3178 +/- ##
==========================================
- Coverage 93.53% 93.51% -0.03%
==========================================
Files 93 93
Lines 11480 11502 +22
==========================================
+ Hits 10738 10756 +18
- Misses 742 746 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
CI still fails. |
Description
When a module is part of a namespace package (a directory without an init.py), Module.relative_to_absolute_name used to drop the namespace package prefix.
For example, with this layout:
where pkg is a namespace package on sys.path, from .subpkg import mod in pkg.main was resolved as subpkg instead of pkg.subpkg, raising AstroidImportError: Failed to import module subpkg.
Fix
Keep the namespace package prefix (derived from the module name) when the relative import target lives in the same directory as the module. Modules whose name carries no package prefix (e.g. built from a file not on sys.path) keep the previous behavior.
Closes #2766