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 inference for attrs.fields #15688

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4594,6 +4594,12 @@ def analyze_iterable_item_type(self, expr: Expression) -> tuple[Type, Type]:

if isinstance(iterable, TupleType):
joined: Type = UninhabitedType()
if iterable.partial_fallback.type.fullname != "builtins.tuple":
# If we're some fancier tuple variant, join with the item type
item_type = echk.check_method_call_by_name("__next__", iterator, [], [], expr)[0]
if not isinstance(get_proper_type(item_type), AnyType):
joined = item_type
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

for item in iterable.items:
joined = join_types(joined, item)
return iterator, joined
Expand Down
3 changes: 3 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,9 @@ reveal_type(f(A)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
reveal_type(f(A).b) # N: Revealed type is "attr.Attribute[builtins.int]"
f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x"

for ff in f(A):
reveal_type(ff) # N: Revealed type is "attr.Attribute[Any]"

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsGenericFields]
Expand Down
11 changes: 9 additions & 2 deletions test-data/unit/fixtures/plugin_attrs.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Builtins stub used to support attrs plugin tests.
from typing import Union, overload
from typing import Union, overload, Generic, Sequence, TypeVar, Type, Iterable, Iterator

class object:
def __init__(self) -> None: pass
Expand All @@ -24,6 +24,13 @@ class complex:

class str: pass
class ellipsis: pass
class tuple: pass
class list: pass
class dict: pass

T = TypeVar("T")
Tco = TypeVar('Tco', covariant=True)
class tuple(Sequence[Tco], Generic[Tco]):
def __new__(cls: Type[T], iterable: Iterable[Tco] = ...) -> T: ...
def __iter__(self) -> Iterator[Tco]: pass
def __contains__(self, item: object) -> bool: pass
def __getitem__(self, x: int) -> Tco: pass