Skip to content

Commit

Permalink
Fix for bug with descriptors in non-strict-optional (#17293)
Browse files Browse the repository at this point in the history
Fixes #17289.
  • Loading branch information
koogoro authored May 25, 2024
1 parent 66b48cb commit fa2aefc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
analyze_descriptor_access(
descriptor_type, mx.copy_modified(original_type=original_type)
)
for original_type in instance_type.items
for original_type in instance_type.relevant_items()
]
)
elif not isinstance(descriptor_type, Instance):
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,34 @@ reveal_type(mix) # N: Revealed type is "Union[Type[__main__.A], Type[__main__.B
reveal_type(mix.field_1) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(mix().field_1) # N: Revealed type is "builtins.int"
[builtins fixtures/list.pyi]


[case testDescriptorAccessForUnionOfTypesWithNoStrictOptional]
# mypy: no-strict-optional
from typing import overload, Generic, Any, TypeVar, List, Optional, Union, Type

class Descriptor:
@overload
def __get__(
self, instance: None, owner: type
) -> str:
...

@overload
def __get__(self, instance: object, owner: type) -> int:
...

def __get__(
self, instance: Optional[object], owner: type
) -> Union[str, int]:
...

class A:
field = Descriptor()

a_class_or_none: Optional[Type[A]]
x: str = a_class_or_none.field

a_or_none: Optional[A]
y: int = a_or_none.field
[builtins fixtures/list.pyi]

0 comments on commit fa2aefc

Please sign in to comment.