Skip to content

Commit fa2aefc

Browse files
authored
Fix for bug with descriptors in non-strict-optional (#17293)
Fixes #17289.
1 parent 66b48cb commit fa2aefc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

mypy/checkmember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
654654
analyze_descriptor_access(
655655
descriptor_type, mx.copy_modified(original_type=original_type)
656656
)
657-
for original_type in instance_type.items
657+
for original_type in instance_type.relevant_items()
658658
]
659659
)
660660
elif not isinstance(descriptor_type, Instance):

test-data/unit/check-unions.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,3 +1258,34 @@ reveal_type(mix) # N: Revealed type is "Union[Type[__main__.A], Type[__main__.B
12581258
reveal_type(mix.field_1) # N: Revealed type is "builtins.list[builtins.int]"
12591259
reveal_type(mix().field_1) # N: Revealed type is "builtins.int"
12601260
[builtins fixtures/list.pyi]
1261+
1262+
1263+
[case testDescriptorAccessForUnionOfTypesWithNoStrictOptional]
1264+
# mypy: no-strict-optional
1265+
from typing import overload, Generic, Any, TypeVar, List, Optional, Union, Type
1266+
1267+
class Descriptor:
1268+
@overload
1269+
def __get__(
1270+
self, instance: None, owner: type
1271+
) -> str:
1272+
...
1273+
1274+
@overload
1275+
def __get__(self, instance: object, owner: type) -> int:
1276+
...
1277+
1278+
def __get__(
1279+
self, instance: Optional[object], owner: type
1280+
) -> Union[str, int]:
1281+
...
1282+
1283+
class A:
1284+
field = Descriptor()
1285+
1286+
a_class_or_none: Optional[Type[A]]
1287+
x: str = a_class_or_none.field
1288+
1289+
a_or_none: Optional[A]
1290+
y: int = a_or_none.field
1291+
[builtins fixtures/list.pyi]

0 commit comments

Comments
 (0)