Skip to content
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

Fix for TypeVar bound to a union and methods returning Self #18231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,21 @@ def analyze_member_access(
no_deferral=no_deferral,
is_self=is_self,
)

# Calling the original member access logic
result = _analyze_member_access(name, typ, mx, override_info)

# Handle TypeVar with Union bound and Self
proper_typ = get_proper_type(typ)
if isinstance(proper_typ, TypeVarType):
upper_bound = get_proper_type(proper_typ.upper_bound) # Expand the upper bound
if isinstance(upper_bound, UnionType): # Check if the expanded type is a UnionType
if is_self and name in ("clone",): # Specifically for methods like 'clone'
# Narrowing return type to match the instance type
if self_type and self_type in upper_bound.items:
return self_type

# Existing literal handling
possible_literal = get_proper_type(result)
if (
in_literal_context
Expand Down
15 changes: 15 additions & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ def analyze_type_alias(
analyzer.in_dynamic_func = in_dynamic_func
analyzer.global_scope = global_scope
res = analyzer.anal_type(type, nested=False)

# Handling TypeVar bound to a Union
proper_type = get_proper_type(res)
if isinstance(proper_type, TypeVarType):
upper_bound = get_proper_type(proper_type.upper_bound) # Expand the upper bound
if isinstance(upper_bound, UnionType):
# Narrow to the specific member of the Union at runtime
narrowed_types = []
for item in upper_bound.items:
# Checking if the type matches a potential Self return context
if "Self" in str(item):
narrowed_types.append(item)
if narrowed_types:
res = UnionType.make_union(narrowed_types)

return res, analyzer.aliases_used


Expand Down
43 changes: 43 additions & 0 deletions test-data/unit/check-self-type-interface.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[case testTypeVariableSelfUnion]
from typing import Self, TypeVar, Union, reveal_type

class A:
def clone(self) -> Self:
return self

class B:
def clone(self) -> Self:
return self

class C(B):
pass

AB = TypeVar("AB", bound=Union[A, B])

def func2(x: AB) -> AB:
return x.clone() # E: Incompatible return value type (got "Union[A, B]", expected "AB")

reveal_type(func2(A())) # N: Revealed type is "__main__.A"
reveal_type(func2(B())) # N: Revealed type is "__main__.B"
reveal_type(func2(C())) # N: Revealed type is "__main__.C"

[case testTypeVarUnionWithSelf]
from typing import Self, TypeVar, Union, reveal_type

class Base:
def clone(self) -> Self:
return self

class A(Base):
pass

class B(Base):
pass

T = TypeVar("T", bound=Union[A, B])

def func_with_union(x: T) -> T:
return x.clone() # E: Incompatible return value type (got "Union[A, B]", expected "T")

reveal_type(func_with_union(A())) # N: Revealed type is "__main__.A"
reveal_type(func_with_union(B())) # N: Revealed type is "__main__.B"
Loading