Skip to content

Try tweaking attrs.fields() #15396

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ def _add_attrs_magic_attribute(
if isinstance(proper_type, Instance):
var.info = proper_type.type
ti.names[name] = SymbolTableNode(MDEF, var, plugin_generated=True)

# We fix up the `__iter__` name so these tuples are iterable as attr.Attributes
ti.names["__iter__"] = fallback_type.type.names["__iter__"]

attributes_type = Instance(ti, [])

# We need to stash the type of the magic attribute so it can be
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,20 @@ f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x"

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsFieldsIteration]
import attr
from attrs import fields as f # Common usage.

@attr.define
class A:
b: int
c: str

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

[builtins fixtures/tuple.pyi]

[case testAttrsGenericFields]
from typing import TypeVar

Expand Down