GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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
ilevkivskyi merged 4 commits into python:master from ilevkivskyi:better-binder-any
Sep 28, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Limit binder special-casing to imprecise Any #22050

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
e0a60ec
Limit binder special-casing to imprecise Any
ilevkivskyi Sep 25, 2026
6837346
Keep reverse logic
ilevkivskyi Sep 25, 2026
0151e5d
Merge remote-tracking branch 'upstream/master' into better-binder-any
ilevkivskyi Sep 28, 2026
6aee713
Add dedicated tests
ilevkivskyi Sep 28, 2026
File filter
Filter by extension

Clear filters
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
Diff view
Next Next commit
Limit binder special-casing to imprecise Any
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi
ilevkivskyi committed Sep 25, 2026
commit e0a60eceedd75270b3cd7fc5110324815425f73d

There are no files selected for viewing

17 changes: 14 additions & 3 deletions 17 mypy/binder.py
Show comments
View file
Open in desktop
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
14 changes: 14 additions & 0 deletions 14 mypy/types.py
Show comments
View file
Open in desktop
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
13 changes: 8 additions & 5 deletions 13 test-data/unit/check-redefine2.test
Show comments
View file
Open in desktop
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
6 changes: 4 additions & 2 deletions 6 test-data/unit/check-unions.test
Show comments
View file
Open in desktop
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
12 changes: 5 additions & 7 deletions 12 test-data/unit/check-unreachable-code.test
Show comments
View file
Open in desktop
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

@JukkaL JukkaL Sep 28, 2026

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 1 reaction
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.