You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since python-attrs/attrs#890 (≥ 22.1.0)
`attrs.fields` is typed to accept a protocol.
Since python-attrs/attrs#997 (≥ 22.2.0)
`attrs.has` is a type-guard.
Support both by removing the explicit error reporting and letting it
fall through to the type stub.
Fixes#15980.
Copy file name to clipboardExpand all lines: test-data/unit/check-plugin-attrs.test
+10-6Lines changed: 10 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1596,16 +1596,18 @@ def f(t: TA) -> None:
1596
1596
[builtins fixtures/plugin_attrs.pyi]
1597
1597
1598
1598
[case testNonattrsFields]
1599
-
# flags: --no-strict-optional
1600
1599
from typing import Any, cast, Type
1601
-
from attrs import fields
1600
+
from attrs import fields, has
1602
1601
1603
1602
class A:
1604
1603
b: int
1605
1604
c: str
1606
1605
1607
-
fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected an attrs class
1608
-
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected an attrs class
1606
+
if has(A):
1607
+
fields(A)
1608
+
else:
1609
+
fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected "Type[AttrsInstance]"
1610
+
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected "Type[AttrsInstance]"
1609
1611
fields(cast(Any, 42))
1610
1612
fields(cast(Type[Any], 43))
1611
1613
@@ -2167,7 +2169,8 @@ TA = TypeVar('TA', bound=A)
2167
2169
TB = TypeVar('TB', bound=B)
2168
2170
2169
2171
def f(b_or_t: TA | TB | int) -> None:
2170
-
a2 = attrs.evolve(b_or_t) # E: Argument 1 to "evolve" has type "Union[TA, TB, int]" whose item "TB" is not bound to an attrs class # E: Argument 1 to "evolve" has incompatible type "Union[TA, TB, int]" whose item "int" is not an attrs class
2172
+
a2 = attrs.evolve(b_or_t) # E: Argument 1 to "evolve" has type "Union[TA, TB, int]" whose item "TB" is not bound to an attrs class \
2173
+
# E: Argument 1 to "evolve" has incompatible type "Union[TA, TB, int]" whose item "int" is not an attrs class
2171
2174
2172
2175
2173
2176
[builtins fixtures/plugin_attrs.pyi]
@@ -2216,7 +2219,8 @@ def h(t: TNone) -> None:
2216
2219
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TNone" not bound to an attrs class
2217
2220
2218
2221
def x(t: TUnion) -> None:
2219
-
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "str" is not an attrs class # E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "int" is not an attrs class
2222
+
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "str" is not an attrs class \
2223
+
# E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "int" is not an attrs class
0 commit comments