From c2f15e81ee568ac8817389f5564a0333b9377db2 Mon Sep 17 00:00:00 2001 From: Tin Tvrtkovic Date: Thu, 8 Jun 2023 23:11:04 +0200 Subject: [PATCH 1/2] Try tweaking `attrs.fields()` --- mypy/plugins/attrs.py | 4 ++++ test-data/unit/check-plugin-attrs.test | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index afd9423d6820..4a1b93e6bcde 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -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 diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 9aa31c1ed10b..a7035856581f 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -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 From 6b2585db36329ea65762210647787da3026121dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Jun 2023 21:15:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test-data/unit/check-plugin-attrs.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index a7035856581f..0b5b27b7ebb6 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -1579,7 +1579,7 @@ class A: c: str for a in f(A): - reveal_type(a) # N: Revealed type is "attr.Attribute[Any]" + reveal_type(a) # N: Revealed type is "attr.Attribute[Any]" [builtins fixtures/tuple.pyi]