Skip to content

Add warning for implicit relative imports issues#49

Open
Eddy114514 wants to merge 3 commits intosoftdevteam:migrationfrom
Eddy114514:import_improve
Open

Add warning for implicit relative imports issues#49
Eddy114514 wants to merge 3 commits intosoftdevteam:migrationfrom
Eddy114514:import_improve

Conversation

@Eddy114514
Copy link
Copy Markdown
Collaborator

Python 2 supports implicit relative imports inside packages. A bare import inside a package can resolve to a sibling module.

Example package:

pkg/
  __init__.py
  foo.py
  main.py
# pkg/main.py
import foo

In Python 2, this can resolve to:

pkg.foo

In Python 3, imports are absolute by default, so the same code resolves as:

foo

This means the code can import a different top-level module or fail with ImportError.

For same environment of

# pkg/main.py
import foo

Python 2 resolves to pkg.foo
Python 3 resolves to top-level foo

I modified import.c to fire warning only if

  1. The import uses Python 2 implicit-relative semantics.
  2. The importing module has package context.
  3. The import is bare, not explicit-relative.
  4. Runtime resolution actually selected a package sibling module.

To ensure above, I checks whether the resolved module name is:
current_package + "." + imported_name
For example:

pkg + "." + foo == pkg.foo
If that condition is true, the import depended on Python 2 implicit relative import behavior, so pygrate2 emits a warning.

@Eddy114514 Eddy114514 self-assigned this May 2, 2026
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

Successfully merging this pull request may close these issues.

1 participant