8000
We read every piece of feedback, and take your input very seriously.
attrs.fields
1 parent 2ab8849 commit 3bf9fdcCopy full SHA for 3bf9fdc
mypy/plugins/attrs.py
@@ -69,7 +69,6 @@
69
TupleType,
70
Type,
71
TypeOfAny,
72
- TypeType,
73
TypeVarType,
74
UninhabitedType,
75
UnionType,
@@ -945,7 +944,7 @@ def add_method(
945
944
946
def _get_attrs_init_type(typ: Instance) -> CallableType | None:
947
"""
948
- If `typ` refers to an attrs class, get the type of its initializer method.
+ If `typ` refers to an attrs class, gets the type of its initializer method.
949
950
magic_attr = typ.type.get(MAGIC_ATTR_NAME)
951
if magic_attr is None or not magic_attr.plugin_generated:
@@ -1019,7 +1018,7 @@ def _get_expanded_attr_types(
1019
1018
1020
def _meet_fields(types: list[Mapping[str, Type]]) -> Mapping[str, Type]:
1021
1022
- "Meet" the fields of a list of attrs classes, i.e. for each field, its new type will be the lower bound.
+ "Meets" the fields of a list of attrs classes, i.e. for each field, its new type will be the lower bound.
1023
1024
field_to_types = defaultdict(list)
1025
for fields in types:
@@ -1036,7 +1035,7 @@ def _meet_fields(types: list[Mapping[str, Type]]) -> Mapping[str, Type]:
1036
1035
1037
def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
1038
1039
- Generate a signature for the 'attr.evolve' function that's specific to the call site
+ Generates a signature for the 'attr.evolve' function that's specific to the call site
1040
and dependent on the type of the first argument.
1041
1042
if len(ctx.args) != 2:
@@ -1070,48 +1069,3 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
1070
1069
fallback=ctx.default_signature.fallback,
1071
name=f"{ctx.default_signature.name} of {inst_type_str}",
1072
)
1073
-
1074
1075
-def fields_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
1076
- """Provide the signature for `attrs.fields`."""
1077
- if not ctx.args or len(ctx.args) != 1 or not ctx.args[0] or not ctx.args[0][0]:
1078
- return ctx.default_signature
1079
1080
- # <hack>
1081
- assert isinstance(ctx.api, TypeChecker)
1082
- inst_type = ctx.api.expr_checker.accept(ctx.args[0][0])
1083
- # </hack>
1084
- proper_type = get_proper_type(inst_type)
1085
1086
- # fields(Any) -> Any, fields(type[Any]) -> Any
1087
- if (
1088
- isinstance(proper_type, AnyType)
1089
- or isinstance(proper_type, TypeType)
1090
- and isinstance(proper_type.item, AnyType)
1091
- ):
1092
1093
1094
- cls = None
1095
- arg_types = ctx.default_signature.arg_types
1096
1097
- if isinstance(proper_type, TypeVarType):
1098
- inner = get_proper_type(proper_type.upper_bound)
1099
- if isinstance(inner, Instance):
1100
- # We need to work arg_types to compensate for the attrs stubs.
1101
- arg_types = [inst_type]
1102
- cls = inner.type
1103
- elif isinstance(proper_type, CallableType):
1104
- cls = proper_type.type_object()
1105
1106
- if cls is not None and MAGIC_ATTR_NAME in cls.names:
1107
- # This is a proper attrs class.
1108
- ret_type = cls.names[MAGIC_ATTR_NAME].type
1109
- assert ret_type is not None
1110
- return ctx.default_signature.copy_modified(arg_types=arg_types, ret_type=ret_type)
1111
1112
- ctx.api.fail(
1113
- f'Argument 1 to "fields" has incompatible type "{format_type_bare(proper_type, ctx.api.options)}"; expected an attrs class',
1114
- ctx.context,
1115
- )
1116
1117
mypy/plugins/default.py
@@ -45,7 +45,6 @@ def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type]
45
return ctypes.array_constructor_callback
46
elif fullname == "functools.singledispatch":
47
return singledispatch.create_singledispatch_function_callback
48
49
return None
50
51
def get_function_signature_hook(
@@ -55,8 +54,6 @@ def get_function_signature_hook(
55
54
56
if fullname in ("attr.evolve", "attrs.evolve", "attr.assoc", "attrs.assoc"):
57
return attrs.evolve_function_sig_callback
58
- elif fullname in ("attr.fields", "attrs.fields"):
59
- return attrs.fields_function_sig_callback
60
61
62
def get_method_signature_hook(
test-data/unit/check-plugin-attrs.test
@@ -1553,59 +1553,6 @@ takes_attrs_cls(A(1, "")) # E: Argument 1 to "takes_attrs_cls" has incompatible
1553
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
1554
[builtins fixtures/plugin_attrs.pyi]
1555
1556
-[case testAttrsFields]
1557
-import attr
1558
-from attrs import fields as f # Common usage.
1559
1560
-@attr.define
1561
-class A:
1562
- b: int
1563
- c: str
1564
1565
-reveal_type(f(A)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
1566
-reveal_type(f(A)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
1567
-reveal_type(f(A).b) # N: Revealed type is "attr.Attribute[builtins.int]"
1568
-f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x"
1569
1570
-[builtins fixtures/plugin_attrs.pyi]
1571
1572
-[case testAttrsGenericFields]
1573
-from typing import TypeVar
1574
1575
1576
-from attrs import fields
1577
1578
1579
1580
1581
1582
1583
-TA = TypeVar('TA', bound=A)
1584
1585
-def f(t: TA) -> None:
1586
- reveal_type(fields(t)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
1587
- reveal_type(fields(t)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
1588
- reveal_type(fields(t).b) # N: Revealed type is "attr.Attribute[builtins.int]"
1589
- fields(t).x # E: "____main___A_AttrsAttributes__" has no attribute "x"
1590
1591
1592
1593
1594
-[case testNonattrsFields]
1595
-from typing import Any, cast, Type
1596
1597
1598
1599
1600
1601
1602
-fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected an attrs class
1603
-fields(None) # E: Argument 1 to "fields" has incompatible type "None"; expected an attrs class
1604
-fields(cast(Any, 42))
1605
-fields(cast(Type[Any], 43))
1606
1607
1608
1609
[case testAttrsInitMethodAlwaysGenerates]
1610
from typing import Tuple
1611
import attr
test-data/unit/lib-stub/attr/__init__.pyi
@@ -247,5 +247,3 @@ def field(
247
248
def evolve(inst: _T, **changes: Any) -> _T: ...
249
def assoc(inst: _T, **changes: Any) -> _T: ...
250
251
-def fields(cls: type) -> Any: ...
test-data/unit/lib-stub/attrs/__init__.pyi
@@ -131,5 +131,3 @@ def field(
131
132
133
134
135