Skip to content

Commit 9cb4872

Browse files
authored
Handle type[TypeVar] (#14756)
Fixes #14755. This case is already covered by `check-classes.test::testTypeUsingTypeCClassMethodFromTypeVarUnionBound`, now we just remove from it errors that shouldn't be reported.
1 parent 730ba8a commit 9cb4872

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

mypy/checkmember.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ def analyze_type_type_member_access(
413413
upper_bound = get_proper_type(typ.item.upper_bound)
414414
if isinstance(upper_bound, Instance):
415415
item = upper_bound
416+
elif isinstance(upper_bound, UnionType):
417+
return _analyze_member_access(
418+
name,
419+
TypeType.make_normalized(upper_bound, line=typ.line, column=typ.column),
420+
mx,
421+
override_info,
422+
)
416423
elif isinstance(upper_bound, TupleType):
417424
item = tuple_fallback(upper_bound)
418425
elif isinstance(upper_bound, AnyType):

mypy/messages.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ def has_no_attr(
516516
context,
517517
code=codes.UNION_ATTR,
518518
)
519+
else:
520+
self.fail(
521+
'{} has no attribute "{}"{}'.format(format_type(original_type), member, extra),
522+
context,
523+
code=codes.ATTR_DEFINED,
524+
)
519525
return AnyType(TypeOfAny.from_error)
520526

521527
def unsupported_operand_types(

test-data/unit/check-classes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,9 +3464,9 @@ class ProUser(User): pass
34643464
class BasicUser(User): pass
34653465
U = TypeVar('U', bound=Union[ProUser, BasicUser])
34663466
def process(cls: Type[U]):
3467-
cls.foo() # E: "Type[U]" has no attribute "foo"
3467+
cls.foo()
34683468
obj = cls()
3469-
cls.bar(obj) # E: "Type[U]" has no attribute "bar"
3469+
cls.bar(obj)
34703470
cls.mro() # Defined in class type
34713471
cls.error # E: "Type[U]" has no attribute "error"
34723472
[builtins fixtures/classmethod.pyi]

0 commit comments

Comments
 (0)