8000 Do not narrow types to Never with binder by ilevkivskyi · Pull Request #18972 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Do not narrow types to Never with binder #18972

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 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6297,7 +6297,13 @@ def narrow_type_from_binder(
known_type, restriction, prohibit_none_typevar_overlap=True
):
return None
return narrow_declared_type(known_type, restriction)
narrowed = narrow_declared_type(known_type, restriction)
if isinstance(get_proper_type(narrowed), UninhabitedType):
# If we hit this case, it means that we can't reliably mark the code as
# unreachable, but the resulting type can't be expressed in type system.
# Falling back to restriction is more intuitive in most cases.
return restriction
return narrowed
return known_type

def has_abstract_type_part(self, caller_type: ProperType, callee_type: ProperType) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1812,9 +1812,9 @@ reveal_type(fm) # N: Revealed type is "__main__.FooMetaclass"
if issubclass(fm, Foo):
reveal_type(fm) # N: Revealed type is "Type[__main__.Foo]"
if issubclass(fm, Bar):
reveal_type(fm) # N: Revealed type is "Never"
reveal_type(fm) # N: Revealed type is "Type[__main__.Bar]"
if issubclass(fm, Baz):
reveal_type(fm) # N: Revealed type is "Never"
reveal_type(fm) # N: Revealed type is "Type[__main__.Baz]"
[builtins fixtures/isinstance.pyi]

[case testIsinstanceAndNarrowTypeVariable]
Expand Down
13 changes: 12 additions & 1 deletion test-data/unit/check-narrowing.test
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ def f(t: Type[T], a: A, b: B) -> None:
reveal_type(a) # N: Revealed type is "__main__.A"

if type(b) is t:
reveal_type(b) # N: Revealed type is "Never"
reveal_type(b) # N: Revealed type is "T`-1"
else:
reveal_type(b) # N: Revealed type is "__main__.B"

Expand Down Expand Up @@ -2413,3 +2413,14 @@ def foo(x: T) -> T:
reveal_type(x) # N: Revealed type is "T`-1"
return x
[builtins fixtures/isinstance.pyi]

[case testDoNotNarrowToNever]
def any():
return 1

def f() -> None:
x = "a"
x = any()
assert isinstance(x, int)
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/isinstance.pyi]
2 changes: 1 addition & 1 deletion test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ m: str

match m:
case a if a := 1: # E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type(a) # N: Revealed type is "Never"
reveal_type(a) # N: Revealed type is "Literal[1]?"

[case testMatchAssigningPatternGuard]
m: str
Expand Down
0