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

Fix for bug with descriptors in non-strict-optional #17293

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
use relevant_items() instead of items
  • Loading branch information
koogoro committed May 25, 2024
commit 527ebe94c844ae2cf1e981f88184d0581fec76df
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]
Loading