Skip to content

Commit 9de6192

Browse files
committed
Fixed and changed logic in checkmember.py and typeanal.py related to errors with get_proper_type() and UnionType. Now passing runtest.py.
1 parent 4cc68e7 commit 9de6192

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

mypy/checkmember.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,20 @@ def analyze_member_access(
203203
no_deferral=no_deferral,
204204
is_self=is_self,
205205
)
206-
206+
207207
# Calling the original member access logic
208208
result = _analyze_member_access(name, typ, mx, override_info)
209-
209+
210210
# Handle TypeVar with Union bound and Self
211211
proper_typ = get_proper_type(typ)
212-
if isinstance(proper_typ, TypeVarType) and isinstance(proper_typ.upper_bound, UnionType):
213-
if is_self and name in ("clone",): # Specifically for methods like 'clone'
214-
# Narrowing return type to match the instance type
215-
bound_union = proper_typ.upper_bound
216-
if self_type and self_type in bound_union.items:
217-
return self_type
218-
212+
if isinstance(proper_typ, TypeVarType):
213+
upper_bound = get_proper_type(proper_typ.upper_bound) # Expand the upper bound
214+
if isinstance(upper_bound, UnionType): # Check if the expanded type is a UnionType
215+
if is_self and name in ("clone",): # Specifically for methods like 'clone'
216+
# Narrowing return type to match the instance type
217+
if self_type and self_type in upper_bound.items:
218+
return self_type
219+
219220
# Existing literal handling
220221
possible_literal = get_proper_type(result)
221222
if (

mypy/typeanal.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,21 @@ def analyze_type_alias(
181181
analyzer.in_dynamic_func = in_dynamic_func
182182
analyzer.global_scope = global_scope
183183
res = analyzer.anal_type(type, nested=False)
184-
184+
185185
# Handling TypeVar bound to a Union
186186
proper_type = get_proper_type(res)
187-
if isinstance(proper_type, TypeVarType) and isinstance(proper_type.upper_bound, UnionType):
188-
union_bound = proper_type.upper_bound
189-
190-
# Narrow to the specific member of the Union at runtime
191-
narrowed_types = []
192-
for item in union_bound.items:
193-
# Checking if the type matches a potential Self return context
194-
if "Self" in str(item):
195-
narrowed_types.append(item)
196-
if narrowed_types:
197-
res = UnionType.make_union(narrowed_types)
198-
187+
if isinstance(proper_type, TypeVarType):
188+
upper_bound = get_proper_type(proper_type.upper_bound) # Expand the upper bound
189+
if isinstance(upper_bound, UnionType):
190+
# Narrow to the specific member of the Union at runtime
191+
narrowed_types = []
192+
for item in upper_bound.items:
193+
# Checking if the type matches a potential Self return context
194+
if "Self" in str(item):
195+
narrowed_types.append(item)
196+
if narrowed_types:
197+
res = UnionType.make_union(narrowed_types)
198+
199199
return res, analyzer.aliases_used
200200

201201

test-data/unit/check-self-type-interface.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ def func_with_union(x: T) -> T:
4040
return x.clone() # E: Incompatible return value type (got "Union[A, B]", expected "T")
4141

4242
reveal_type(func_with_union(A())) # N: Revealed type is "__main__.A"
43-
reveal_type(func_with_union(B())) # N: Revealed type is "__main__.B"
43+
reveal_type(func_with_union(B())) # N: Revealed type is "__main__.B"

0 commit comments

Comments
 (0)