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

Prevent TypeGuardedType leak fromnarrow_declared_type as part of typevar bound#20046

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 fromall 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
4 changes: 3 additions & 1 deletionmypy/meet.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,7 +170,9 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
):
# We put this branch early to get T(bound=Union[A, B]) instead of
# Union[T(bound=A), T(bound=B)] that will be confusing for users.
returndeclared.copy_modified(upper_bound=original_narrowed)
returndeclared.copy_modified(
upper_bound=narrow_declared_type(declared.upper_bound,original_narrowed)
)
elifnotis_overlapping_types(declared,narrowed,prohibit_none_typevar_overlap=True):
ifstate.strict_optional:
returnUninhabitedType()
Expand Down
20 changes: 20 additions & 0 deletionstest-data/unit/check-typeguard.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -825,6 +825,26 @@ def handle(model: Model) -> int:
return 0
[builtins fixtures/tuple.pyi]

[case testTypeGuardedTypeDoesNotLeakTypeVar]
# flags: --debug-serialize
# https://github.com/python/mypy/issues/20015
from typing import Generic, TypeVar, TypeGuard

class A: ...
class B: ...

def is_a(_: object) -> TypeGuard[A]: return True
def is_b(_: object) -> TypeGuard[B]: return True

_T = TypeVar("_T")

class Foo(Generic[_T]):
def __init__(self, v: _T) -> None:
if is_a(v) or is_b(v):
self.v = v
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testTypeGuardRestrictTypeVarUnion]
from typing import Union, TypeVar
from typing_extensions import TypeGuard
Expand Down
1 change: 1 addition & 0 deletionstest-data/unit/fixtures/typing-full.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,7 @@ no_type_check = 0
ClassVar = 0
Final = 0
TypedDict = 0
TypeGuard = 0
NoReturn = 0
NewType = 0
Self = 0
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp