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

Handle type[TypeVar] (fixes #14755) #14756

Merged
Merged
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
7 changes: 7 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ def analyze_type_type_member_access(
upper_bound = get_proper_type(typ.item.upper_bound)
if isinstance(upper_bound, Instance):
item = upper_bound
elif isinstance(upper_bound, UnionType):
return _analyze_member_access(
name,
TypeType.make_normalized(upper_bound, line=typ.line, column=typ.column),
mx,
override_info,
)
elif isinstance(upper_bound, TupleType):
item = tuple_fallback(upper_bound)
elif isinstance(upper_bound, AnyType):
Expand Down
6 changes: 6 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ def has_no_attr(
context,
code=codes.UNION_ATTR,
)
else:
self.fail(
'{} has no attribute "{}"{}'.format(format_type(original_type), member, extra),
context,
code=codes.ATTR_DEFINED,
)
return AnyType(TypeOfAny.from_error)

def unsupported_operand_types(
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3464,9 +3464,9 @@ class ProUser(User): pass
class BasicUser(User): pass
U = TypeVar('U', bound=Union[ProUser, BasicUser])
def process(cls: Type[U]):
cls.foo() # E: "Type[U]" has no attribute "foo"
cls.foo()
obj = cls()
cls.bar(obj) # E: "Type[U]" has no attribute "bar"
cls.bar(obj)
cls.mro() # Defined in class type
cls.error # E: "Type[U]" has no attribute "error"
[builtins fixtures/classmethod.pyi]
Expand Down