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

Add support for attrs.fields #15021

Merged
merged 10 commits into from
May 20, 2023
40 changes: 40 additions & 0 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,43 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
fallback=ctx.default_signature.fallback,
name=f"{ctx.default_signature.name} of {inst_type_str}",
)


def fields_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
"""Provide the proper signature for `attrs.fields`."""
Tinche marked this conversation as resolved.
Show resolved Hide resolved
if ctx.args and len(ctx.args) == 1 and ctx.args[0] and ctx.args[0][0]:
Tinche marked this conversation as resolved.
Show resolved Hide resolved
# <hack>
assert isinstance(ctx.api, TypeChecker)
inst_type = ctx.api.expr_checker.accept(ctx.args[0][0])
# </hack>
proper_type = get_proper_type(inst_type)

if isinstance(proper_type, AnyType): # fields(Any) -> Any
Tinche marked this conversation as resolved.
Show resolved Hide resolved
return ctx.default_signature

cls = None
arg_types = ctx.default_signature.arg_types

if isinstance(proper_type, TypeVarType):
inner = get_proper_type(proper_type.upper_bound)
if isinstance(inner, Instance):
# We need to work arg_types to compensate for the attrs stubs.
arg_types = [inst_type]
Tinche marked this conversation as resolved.
Show resolved Hide resolved
cls = inner.type
elif isinstance(proper_type, CallableType):
cls = proper_type.type_object()

if cls is not None:
if MAGIC_ATTR_NAME in cls.names:
Tinche marked this conversation as resolved.
Show resolved Hide resolved
# This is a proper attrs class.
ret_type = cls.names[MAGIC_ATTR_NAME].type
if ret_type is not None:
Tinche marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 3 additions & 0 deletions mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type]
return ctypes.array_constructor_callback
elif fullname == "functools.singledispatch":
return singledispatch.create_singledispatch_function_callback

return None

def get_function_signature_hook(
Expand All @@ -54,6 +55,8 @@ def get_function_signature_hook(

if fullname in ("attr.evolve", "attrs.evolve", "attr.assoc", "attrs.assoc"):
return attrs.evolve_function_sig_callback
elif fullname in ("attr.fields", "attrs.fields"):
return attrs.fields_function_sig_callback
return None

def get_method_signature_hook(
Expand Down
52 changes: 52 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,58 @@ takes_attrs_cls(A(1, "")) # E: Argument 1 to "takes_attrs_cls" has incompatible
takes_attrs_instance(A) # E: Argument 1 to "takes_attrs_instance" has incompatible type "Type[A]"; expected "AttrsInstance" # N: ClassVar protocol member AttrsInstance.__attrs_attrs__ can never be matched by a class object
[builtins fixtures/plugin_attrs.pyi]

[case testAttrsFields]
import attr
from attrs import fields as f # Common usage.

@attr.define
class A:
b: int
c: str

reveal_type(f(A)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
reveal_type(f(A)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
reveal_type(f(A).b) # N: Revealed type is "attr.Attribute[builtins.int]"
f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x"

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsGenericFields]
from typing import TypeVar

import attr
from attrs import fields

@attr.define
class A:
b: int
c: str

TA = TypeVar('TA', bound=A)

def f(t: TA) -> None:
reveal_type(fields(t)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
reveal_type(fields(t)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
reveal_type(fields(t).b) # N: Revealed type is "attr.Attribute[builtins.int]"
fields(t).x # E: "____main___A_AttrsAttributes__" has no attribute "x"


[builtins fixtures/plugin_attrs.pyi]

[case testNonattrsFields]
from typing import Any, cast
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(cast(Any, 42))

[builtins fixtures/plugin_attrs.pyi]

[case testAttrsInitMethodAlwaysGenerates]
from typing import Tuple
import attr
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/attr/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,5 @@ def field(

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

def fields(cls: type) -> Any: ...
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/attrs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ def field(

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

def fields(cls: type) -> Any: ...