GeezerVison is showing github.com — Limit binder special-casing to imprecise Any by ilevkivskyi · Pull Request #22050 · python/mypy
Limit binder special-casing to imprecise Any
#22050
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.py
(2)
.test
(3)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
Limit binder special-casing to imprecise Any
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit e0a60eceedd75270b3cd7fc5110324815425f73d
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -21,6 +21,7 @@
from mypy.subtypes import is_same_type, is_subtype
from mypy.typeops import make_simplified_union
from mypy.types import (
IMPRECISE_ANYS,
AnyType,
Instance,
NoneType,
Expand Down
Expand Up
@@ -499,8 +500,7 @@ def assign_type(self, expr: Expression, type: Type, declared_type: Type | None)
return
p_declared = get_proper_type(declared_type)
p_type = get_proper_type(type)
if isinstance(p_type, AnyType):
if is_imprecise_any(type):
# Any type requires some special casing, for both historical reasons,
# and to optimise user experience without sacrificing correctness too much.
if isinstance(expr, RefExpr) and isinstance(expr.node, Var) and expr.node.is_inferred:
Expand All
@@ -517,7 +517,7 @@ def assign_type(self, expr: Expression, type: Type, declared_type: Type | None)
for item in all_items
]
self.put(expr, UnionType(new_items))
elif any(isinstance(get_proper_type( item), AnyType ) for item in all_items):
elif any(is_imprecise_any( item) for item in all_items):
# Third case: a union already containing Any (most likely from
# an un-imported name), in this case we allow assigning Any as well.
self.put(expr, type)
Expand Down
Expand Up
@@ -708,3 +708,14 @@ def collapse_variadic_union(typ: UnionType) -> Type:
else:
simplified = TupleType(prefix + [unpack] + suffix, fallback=last.partial_fallback)
return UnionType.make_union([simplified] + other_items)
def is_imprecise_any(tp: Type) -> bool:
"""Is this an imprecise Any type?"""
tp = get_proper_type(tp)
if not isinstance(tp, AnyType):
return False
if tp.type_of_any == TypeOfAny.from_another_any:
assert tp.source_any is not None
tp = tp.source_any
return tp.type_of_any in IMPRECISE_ANYS
Original file line number
Diff line number
Diff line change
Expand Up
@@ -261,6 +261,20 @@ class TypeOfAny:
suggestion_engine: Final = 9
# Some parts of the code distinguish two categories of Any types:
# * Intentional: explicit, from_error, special_form - these are what other
# languages/type-checkers may call "Dynamic", i.e. too tricky to express.
# * Imprecise: unannotated, from_unimported_type, from_omitted_generics - these
# are what other languages/type-checkers may call "Unknown", i.e. values in
# partially annotated legacy code. These are usually "non-sticky", after assigning
# such value to a variable, the variable keeps the original (more precise) type.
IMPRECISE_ANYS: Final = (
TypeOfAny.unannotated,
TypeOfAny.from_unimported_type,
TypeOfAny.from_omitted_generics,
)
def deserialize_type(data: JsonDict | str) -> Type:
if isinstance(data, str):
return Instance.deserialize(data)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1525,10 +1525,11 @@ def test(x: Union[str, Int]) -> None:
reveal_type(y) # N: Revealed type is "Any"
[case testNewRedefineNarrowingForNestedUnionWithNone]
# flags: --allow-redefinition
from typing import Any, Union
# flags: --allow-redefinition --ignore-missing-imports
from typing import Union
from somewhere import Unknown
a: Any
a: Unknown
Int = Union[int, None]
def test(x: Union[str, Int]) -> None:
Expand All
@@ -1552,9 +1553,11 @@ def test(x: Union[str, Int]) -> None:
reveal_type(y) # N: Revealed type is "Any"
[case testRegularNarrowingForNestedUnionWithNone]
from typing import Any, Union
# flags: --ignore-missing-imports
from typing import Union
from somewhere import Unknown
a: Any
a: Unknown
Int = Union[int, None]
def test(x: Union[str, Int]) -> None:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -524,9 +524,11 @@ def foo(bar: Union[Union[A, B], C]) -> None:
[out]
[case testAssignAnyToUnion]
from typing import Union, Any
from typing import Union
from somewhere import Unknown # type: ignore
x: Union[int, str]
a: Any
a: Unknown
if bool():
x = a
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -777,10 +777,8 @@ reveal_type(x) # N: Revealed type is "__main__.B"
[typing fixtures/typing-medium.pyi]
[case testUnreachableWhenSuperclassIsAny]
from typing import Any
from somewhere import Parent # type: ignore
# This can happen if we're importing a class from a missing module
Parent: Any
class Child(Parent):
def foo(self) -> int:
reveal_type(self) # N: Revealed type is "__main__.Child"
Expand All
@@ -793,11 +791,11 @@ class Child(Parent):
def bar(self) -> int:
if 1:
self = super(Child, self).something()
reveal_type(self) # N: Revealed type is "__main__.Child "
reveal_type(self) # N: Revealed type is "Any "
if self is None:
reveal_type(self)
return None
reveal_type(self) # N: Revealed type is "__main__.Child "
reveal_type(self) # N: Revealed type is "None"
return None # E: Incompatible return value type (got "None", expected "int")
reveal_type(self) # N: Revealed type is "Any "
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Add a new test case focused on the new behavior, as now it seems to be tested only incidentally?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ilevkivskyi reacted with thumbs up emoji
return 3
[builtins fixtures/isinstance.pyi]
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.