Skip to content

Fix relative import resolution in namespace packages - #3178

Open
TheGreatApollyon wants to merge 3 commits into
pylint-dev:mainfrom
TheGreatApollyon:fix/issue-2766-namespace-relative-import
Open

Fix relative import resolution in namespace packages#3178
TheGreatApollyon wants to merge 3 commits into
pylint-dev:mainfrom
TheGreatApollyon:fix/issue-2766-namespace-relative-import

Conversation

@TheGreatApollyon

Copy link
Copy Markdown
Contributor

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:

pkg/
├── main.py          # from .subpkg import mod
└── subpkg/
    └── mod.py

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

TheGreatApollyon and others added 3 commits July 31, 2026 22:56
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
@codspeed-hq

codspeed-hq Bot commented Jul 31, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing TheGreatApollyon:fix/issue-2766-namespace-relative-import (b506576) with main (2beb35b)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.46154% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.51%. Comparing base (3aacc67) to head (b506576).
⚠️ Report is 38 commits behind head on main.

Files with missing lines Patch % Lines
astroid/brain/brain_re.py 88.00% 3 Missing ⚠️

❌ 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

Impacted file tree graph

@@            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     
Flag Coverage Δ
linux 93.38% <88.46%> (-0.03%) ⬇️
pypy 93.51% <88.46%> (-0.03%) ⬇️
windows 93.48% <88.46%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/nodes/scoped_nodes/scoped_nodes.py 93.20% <100.00%> (-0.08%) ⬇️
astroid/brain/brain_re.py 93.33% <88.00%> (-6.67%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord

Copy link
Copy Markdown
Collaborator

CI still fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

packages are not recognized as packages if __init__.py as it should since python 3.3

3 participants