@@ -1220,3 +1220,41 @@ nc: Union[Container[str], int]
1220
1220
'x' in nc # E: Unsupported right operand type for in ("Union[Container[str], int]")
1221
1221
[builtins fixtures/tuple.pyi]
1222
1222
[typing fixtures/typing-full.pyi]
1223
+
1224
+ [case testDescriptorAccessForUnionOfTypes]
1225
+ from typing import overload, Generic, Any, TypeVar, List, Optional, Union, Type
1226
+
1227
+ _T_co = TypeVar("_T_co", bound=Any, covariant=True)
1228
+
1229
+ class Mapped(Generic[_T_co]):
1230
+ def __init__(self, value: _T_co):
1231
+ self.value = value
1232
+
1233
+ @overload
1234
+ def __get__(
1235
+ self, instance: None, owner: Any
1236
+ ) -> List[_T_co]:
1237
+ ...
1238
+
1239
+ @overload
1240
+ def __get__(self, instance: object, owner: Any) -> _T_co:
1241
+ ...
1242
+
1243
+ def __get__(
1244
+ self, instance: Optional[object], owner: Any
1245
+ ) -> Union[List[_T_co], _T_co]:
1246
+ return self.value
1247
+
1248
+ class A:
1249
+ field_1: Mapped[int] = Mapped(1)
1250
+ field_2: Mapped[str] = Mapped('1')
1251
+
1252
+ class B:
1253
+ field_1: Mapped[int] = Mapped(2)
1254
+ field_2: Mapped[str] = Mapped('2')
1255
+
1256
+ mix: Union[Type[A], Type[B]] = A
1257
+ reveal_type(mix) # N: Revealed type is "Union[Type[__main__.A], Type[__main__.B]]"
1258
+ reveal_type(mix.field_1) # N: Revealed type is "builtins.list[builtins.int]"
1259
+ reveal_type(mix().field_1) # N: Revealed type is "builtins.int"
1260
+ [builtins fixtures/list.pyi]
0 commit comments