Skip to content

Handle nested imports correctly in from ... import#15026

Merged
dcreager merged 13 commits intomainfrom
dcreager/relative-nested-imports
Dec 17, 2024
Merged

Handle nested imports correctly in from ... import#15026
dcreager merged 13 commits intomainfrom
dcreager/relative-nested-imports

Conversation

@dcreager
Copy link
Member

#14946 fixed our handling of nested imports with the import statement, but didn't touch from...import statements.

cf #14826 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Dec 16, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dcreager dcreager marked this pull request as ready for review December 16, 2024 23:31
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Dec 16, 2024
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like the use of early return

* main:
  [red-knot] Explicitly test diagnostics are emitted for unresolvable submodule imports (#15035)
  Fix stale File status in tests (#15030)
  [red-knot] Basic support for other legacy `typing` aliases (#14998)
  feat(AIR302): extend the following rules (#15015)
  [`perflint`] Simplify finding the loop target in `PERF401` (#15025)
  [red-knot] Avoid undeclared path when raising conflicting declarations (#14958)
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

EDIT: well, except for the failing test 😆

@dcreager
Copy link
Member Author

well, except for the failing test 😆

Details 😄

@dcreager dcreager merged commit 91c9168 into main Dec 17, 2024
@dcreager dcreager deleted the dcreager/relative-nested-imports branch December 17, 2024 19:23
dcreager added a commit that referenced this pull request Dec 17, 2024
This tweaks the new semantics from #15026 a bit when a symbol could be
interpreted both as an attribute and a submodule of a package. For
`from...import`, we should actually prioritize the attribute, because of
how the statement itself is implemented [1].

> 1. check if the imported module has an attribute by that name
> 2. if not, attempt to import a submodule with that name and then check
the imported module again for that attribute

[1] https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants