diff --git a/mypy/messages.py b/mypy/messages.py index 820e15eb0581..92a5487162fc 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -345,6 +345,16 @@ def has_no_attr(self, self.fail('Item {} of {} has no attribute "{}"{}'.format( typ_format, orig_type_format, member, extra), context, code=codes.UNION_ATTR) + elif isinstance(original_type, TypeVarType): + bound = get_proper_type(original_type.upper_bound) + if isinstance(bound, UnionType): + typ_fmt, bound_fmt = format_type_distinctly(typ, bound) + original_type_fmt = format_type(original_type) + self.fail( + 'Item {} of the upper bound {} of type variable {} has no ' + 'attribute "{}"{}'.format( + typ_fmt, bound_fmt, original_type_fmt, member, extra), + context, code=codes.UNION_ATTR) return AnyType(TypeOfAny.from_error) def unsupported_operand_types(self, diff --git a/test-data/unit/check-generic-subtyping.test b/test-data/unit/check-generic-subtyping.test index 5a7c0b8624f7..d1420a43f9e4 100644 --- a/test-data/unit/check-generic-subtyping.test +++ b/test-data/unit/check-generic-subtyping.test @@ -817,4 +817,28 @@ class Y(Generic[T]): def f(self) -> T: return U() # E: Incompatible return value type (got "U", expected "T") + +[case testTypeVarBoundToUnionAttributeAccess] +from typing import Union, TypeVar + +class U: + a: float +class V: + b: float +class W: + c: float + +T = TypeVar("T", bound=Union[U, V, W]) + +def f(x: T) -> None: + x.a # E + x.b = 1.0 # E + del x.c # E + [out] +main:13: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a" +main:13: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a" +main:14: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b" +main:14: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b" +main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c" +main:15: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"