Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Narrow type variable bounds in binder#19183

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
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
PrevPrevious commit
NextNext commit
Reorder branches in narrowing logic
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedMay 31, 2025
commite2fadecbfbf44132087a777bc3f63ab10b22598f
8 changes: 4 additions & 4 deletionsmypy/meet.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -150,6 +150,10 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
return make_simplified_union(
[narrow_declared_type(declared, x) for x in narrowed.relevant_items()]
)
elif isinstance(narrowed, UnionType):
return make_simplified_union(
[narrow_declared_type(declared, x) for x in narrowed.relevant_items()]
)
elif (
isinstance(declared, TypeVarType)
and not has_type_vars(original_narrowed)
Expand All@@ -161,10 +165,6 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
return UninhabitedType()
else:
return NoneType()
elif isinstance(narrowed, UnionType):
return make_simplified_union(
[narrow_declared_type(declared, x) for x in narrowed.relevant_items()]
)
elif isinstance(narrowed, AnyType):
return original_narrowed
elif isinstance(narrowed, TypeVarType) and is_subtype(narrowed.upper_bound, declared):
Expand Down
18 changes: 18 additions & 0 deletionstest-data/unit/check-typeguard.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -777,3 +777,21 @@ def handle(model: Model) -> int:
return process_model(model)
return 0
[builtins fixtures/tuple.pyi]

[case testTypeGuardRestrictTypeVarUnion]
from typing import Union, TypeVar
from typing_extensions import TypeGuard

class A:
x: int
class B:
x: str

def is_b(x: object) -> TypeGuard[B]: ...

T = TypeVar("T")
def test(x: T) -> T:
if isinstance(x, A) or is_b(x):
reveal_type(x.x) # N: Revealed type is "Union[builtins.int, builtins.str]"
return x
[builtins fixtures/isinstance.pyi]

[8]ページ先頭

©2009-2025 Movatter.jp