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 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
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4592,7 +4592,10 @@ def analyze_iterable_item_type(self, expr: Expression) -> tuple[Type, Type]:
if int_type:
return iterator, int_type

if isinstance(iterable, TupleType):
if (
isinstance(iterable, TupleType)
and iterable.partial_fallback.type.fullname == "builtins.tuple"
):
joined: Type = UninhabitedType()
for item in iterable.items:
joined = join_types(joined, item)
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