[ty] Allow self-referential imports outside of the global scope#22963
Merged
charliermarsh merged 1 commit intomainfrom Jan 30, 2026
Merged
[ty] Allow self-referential imports outside of the global scope#22963charliermarsh merged 1 commit intomainfrom
charliermarsh merged 1 commit intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
AlexWaygood
approved these changes
Jan 30, 2026
Comment on lines
8778
to
+8787
| // Avoid looking up attributes on a module if a module imports from itself | ||
| // (e.g. `from parent import submodule` inside the `parent` module). | ||
| let import_is_self_referential = module_ty | ||
| // at the module-global scope, where the import definition itself is one of the | ||
| // bindings for the symbol being looked up, which would cause a query cycle. | ||
| // | ||
| // In nested scopes (e.g. function bodies), the module's global-scope definitions | ||
| // are resolved independently, so there is no cycle risk and the lookup is safe. | ||
| let skip_self_referential_member_lookup = module_ty | ||
| .as_module_literal() | ||
| .is_some_and(|module| Some(self.file()) == module.module(self.db()).file(self.db())); | ||
| .is_some_and(|module| Some(self.file()) == module.module(self.db()).file(self.db())) | ||
| && self.scope().file_scope_id(self.db()).is_global(); |
Member
There was a problem hiding this comment.
we should see (separately) if we can lift this restriction altogether... this works fine at runtime; it's a shame we emit a false-positive error on it. (But not high-priority, because I don't know why you'd do this)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes astral-sh/ty#2596.