Skip to content

Commit

Permalink
attrs: remove fields type check
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonst committed Aug 29, 2023
1 parent 010da0b commit 8d9cfc1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
5 changes: 0 additions & 5 deletions mypy/plugins/attrs.py
Expand Up @@ -1111,9 +1111,4 @@ def fields_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
assert ret_type is not None
return ctx.default_signature.copy_modified(arg_types=arg_types, ret_type=ret_type)

ctx.api.fail(
f'Argument 1 to "fields" has incompatible type "{format_type_bare(proper_type, ctx.api.options)}"; expected an attrs class',
ctx.context,
)

return ctx.default_signature
11 changes: 6 additions & 5 deletions test-data/unit/check-plugin-attrs.test
Expand Up @@ -1596,16 +1596,15 @@ def f(t: TA) -> None:
[builtins fixtures/plugin_attrs.pyi]

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

class A:
b: int
c: str

fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected an attrs class
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected an attrs class
fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected "Type[AttrsInstance]"
fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected "Type[AttrsInstance]"
fields(cast(Any, 42))
fields(cast(Type[Any], 43))

Expand Down Expand Up @@ -2167,7 +2166,8 @@ TA = TypeVar('TA', bound=A)
TB = TypeVar('TB', bound=B)

def f(b_or_t: TA | TB | int) -> None:
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
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


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

def x(t: TUnion) -> None:
_ = 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
_ = 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

[builtins fixtures/plugin_attrs.pyi]

Expand Down
12 changes: 9 additions & 3 deletions test-data/unit/lib-stub/attrs/__init__.pyi
@@ -1,7 +1,13 @@
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, Generic
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, Generic, \
Protocol, ClassVar, Type, Dict, TypeGuard

from attr import Attribute as Attribute


class AttrsInstance(Protocol):
__attrs_attrs__: ClassVar[Any]


_T = TypeVar('_T')
_C = TypeVar('_C', bound=type)

Expand Down Expand Up @@ -131,5 +137,5 @@ def field(

def evolve(inst: _T, **changes: Any) -> _T: ...
def assoc(inst: _T, **changes: Any) -> _T: ...

def fields(cls: type) -> Any: ...
def has(cls: type) -> TypeGuard[Type[AttrsInstance]]: ...
def fields(cls: Type[AttrsInstance]) -> Any: ...

0 comments on commit 8d9cfc1

Please sign in to comment.