Skip to content

Commit 8d9cfc1

Browse files
committed
attrs: remove fields type check
1 parent 010da0b commit 8d9cfc1

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

mypy/plugins/attrs.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,4 @@ def fields_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
11111111
assert ret_type is not None
11121112
return ctx.default_signature.copy_modified(arg_types=arg_types, ret_type=ret_type)
11131113

1114-
ctx.api.fail(
1115-
f'Argument 1 to "fields" has incompatible type "{format_type_bare(proper_type, ctx.api.options)}"; expected an attrs class',
1116-
ctx.context,
1117-
)
1118-
11191114
return ctx.default_signature

test-data/unit/check-plugin-attrs.test

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,16 +1596,15 @@ def f(t: TA) -> None:
15961596
[builtins fixtures/plugin_attrs.pyi]
15971597

15981598
[case testNonattrsFields]
1599-
# flags: --no-strict-optional
16001599
from typing import Any, cast, Type
16011600
from attrs import fields
16021601

16031602
class A:
16041603
b: int
16051604
c: str
16061605

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+
fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected "Type[AttrsInstance]"
1607+
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected "Type[AttrsInstance]"
16091608
fields(cast(Any, 42))
16101609
fields(cast(Type[Any], 43))
16111610

@@ -2167,7 +2166,8 @@ TA = TypeVar('TA', bound=A)
21672166
TB = TypeVar('TB', bound=B)
21682167

21692168
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
2169+
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 \
2170+
# E: Argument 1 to "evolve" has incompatible type "Union[TA, TB, int]" whose item "int" is not an attrs class
21712171

21722172

21732173
[builtins fixtures/plugin_attrs.pyi]
@@ -2216,7 +2216,8 @@ def h(t: TNone) -> None:
22162216
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TNone" not bound to an attrs class
22172217

22182218
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
2219+
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "str" is not an attrs class \
2220+
# E: Argument 1 to "evolve" has incompatible type "TUnion" whose item "int" is not an attrs class
22202221

22212222
[builtins fixtures/plugin_attrs.pyi]
22222223

test-data/unit/lib-stub/attrs/__init__.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, Generic
1+
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, Generic, \
2+
Protocol, ClassVar, Type, Dict, TypeGuard
23

34
from attr import Attribute as Attribute
45

6+
7+
class AttrsInstance(Protocol):
8+
__attrs_attrs__: ClassVar[Any]
9+
10+
511
_T = TypeVar('_T')
612
_C = TypeVar('_C', bound=type)
713

@@ -131,5 +137,5 @@ def field(
131137

132138
def evolve(inst: _T, **changes: Any) -> _T: ...
133139
def assoc(inst: _T, **changes: Any) -> _T: ...
134-
135-
def fields(cls: type) -> Any: ...
140+
def has(cls: type) -> TypeGuard[Type[AttrsInstance]]: ...
141+
def fields(cls: Type[AttrsInstance]) -> Any: ...

0 commit comments

Comments
 (0)