Skip to content

Commit

Permalink
Fix inference for attrs.fields (#15688)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jul 29, 2023
1 parent 710ad44 commit 002502a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4632,7 +4632,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 @@ -1568,6 +1568,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

0 comments on commit 002502a

Please sign in to comment.