-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] No union with Unknown for module-global symbols
#20664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -810,7 +810,7 @@ fn place_by_id<'db>( | |
| // modified externally, but those changes do not take effect. We therefore issue | ||
| // a diagnostic if we see it being modified externally. In type inference, we | ||
| // can assign a "narrow" type to it even if it is not *declared*. This means, we | ||
| // do not have to call [`widen_type_for_undeclared_public_symbol`]. | ||
| // do not have to union with `Unknown`. | ||
| // | ||
| // `TYPE_CHECKING` is a special variable that should only be assigned `False` | ||
| // at runtime, but is always considered `True` in type checking. | ||
|
|
@@ -822,18 +822,37 @@ fn place_by_id<'db>( | |
| ) | ||
| }); | ||
|
|
||
| if scope.file(db).is_stub(db) || scope.scope(db).visibility().is_private() { | ||
| // We generally trust module-level undeclared places in stubs and do not union | ||
| // with `Unknown`. If we don't do this, simple aliases like `IOError = OSError` in | ||
| // stubs would result in `IOError` being a union of `OSError` and `Unknown`, which | ||
| // leads to all sorts of downstream problems. Similarly, type variables are often | ||
| // defined as `_T = TypeVar("_T")`, without being declared. | ||
| // Also, if the scope is private, such as a function scope, | ||
| // meaning that the place cannot be rewritten from elsewhere, we do not union with `Unknown`. | ||
|
|
||
| // Module-level globals can be mutated externally. A `MY_CONSTANT = 1` global might | ||
| // be changed to `"some string"` from code outside of the module that we're looking | ||
| // at, and so from a gradual-guarantee perspective, it makes sense to infer a type | ||
| // of `Literal[1] | Unknown` for global symbols. This allows the code that does the | ||
| // mutation to type check correctly, and for code that uses the global, it accurately | ||
| // reflects the lack of knowledge about the type. | ||
| // | ||
| // However, external modifications (or modifications through `global` statements) that | ||
| // would require a wider type are relatively rare. From a practical perspective, we can | ||
| // therefore achieve a better user experience by trusting the inferred type. Users who | ||
| // need the external mutation to work can always annotate the global with the wider | ||
| // type. And everyone else benefits from more precise type inference. | ||
| let is_module_global = scope.node(db).scope_kind().is_module(); | ||
|
|
||
| // If the visibility of the scope is private (like for a function scope), we also do | ||
| // not union with `Unknown`, because the symbol cannot be modified externally. | ||
| let scope_has_private_visibility = scope.scope(db).visibility().is_private(); | ||
|
|
||
| // We generally trust undeclared places in stubs and do not union with `Unknown`. | ||
| let in_stub_file = scope.file(db).is_stub(db); | ||
|
Comment on lines
+843
to
+844
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that we can now remove this exception here, but we'll evaluate that in a separate PR, to make sure it does not have any effects on the ecosystem. |
||
|
|
||
| if is_considered_non_modifiable | ||
| || is_module_global | ||
| || scope_has_private_visibility | ||
| || in_stub_file | ||
| { | ||
| inferred.into() | ||
| } else { | ||
| widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable) | ||
| // Widen the inferred type of undeclared public symbols by unioning with `Unknown` | ||
| inferred | ||
| .map_type(|ty| UnionType::from_elements(db, [Type::unknown(), ty])) | ||
| .into() | ||
| } | ||
| } | ||
|
|
@@ -1585,29 +1604,6 @@ pub(crate) enum BoundnessAnalysis { | |
| BasedOnUnboundVisibility, | ||
| } | ||
|
|
||
| /// Computes a possibly-widened type `Unknown | T_inferred` from the inferred type `T_inferred` | ||
| /// of a symbol, unless the type is a known-instance type (e.g. `typing.Any`) or the symbol is | ||
| /// considered non-modifiable (e.g. when the symbol is `@Final`). We need this for public uses | ||
| /// of symbols that have no declared type. | ||
| fn widen_type_for_undeclared_public_symbol<'db>( | ||
| db: &'db dyn Db, | ||
| inferred: Place<'db>, | ||
| is_considered_non_modifiable: bool, | ||
| ) -> Place<'db> { | ||
| // We special-case known-instance types here since symbols like `typing.Any` are typically | ||
| // not declared in the stubs (e.g. `Any = object()`), but we still want to treat them as | ||
| // such. | ||
| let is_known_instance = inferred | ||
| .ignore_possibly_unbound() | ||
| .is_some_and(|ty| matches!(ty, Type::SpecialForm(_) | Type::KnownInstance(_))); | ||
|
|
||
| if is_considered_non_modifiable || is_known_instance { | ||
| inferred | ||
| } else { | ||
| inferred.map_type(|ty| UnionType::from_elements(db, [Type::unknown(), ty])) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.