Skip to content
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

attrs: remove fields type check #15983

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
5 changes: 0 additions & 5 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
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
16 changes: 10 additions & 6 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1596,16 +1596,18 @@ 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
from attrs import fields, has

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
if has(A):
fields(A)
else:
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 +2169,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 +2219,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
13 changes: 10 additions & 3 deletions test-data/unit/lib-stub/attrs/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, Generic
from typing import TypeVar, overload, Callable, Any, Optional, Union, Sequence, Mapping, \
Protocol, ClassVar, Type
from typing_extensions import 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 +138,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: ...