Skip to content

Commit eeba30f

Browse files
committed
Revert "Add support for attrs.fields (python#15021)"
This reverts commit 391ed85.
1 parent 9e1f4df commit eeba30f

File tree

5 files changed

+3
-109
lines changed

5 files changed

+3
-109
lines changed

mypy/plugins/attrs.py

+3-45
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
TupleType,
6969
Type,
7070
TypeOfAny,
71-
TypeType,
7271
TypeVarType,
7372
UninhabitedType,
7473
UnionType,
@@ -952,7 +951,7 @@ def add_method(
952951

953952
def _get_attrs_init_type(typ: Instance) -> CallableType | None:
954953
"""
955-
If `typ` refers to an attrs class, get the type of its initializer method.
954+
If `typ` refers to an attrs class, gets the type of its initializer method.
956955
"""
957956
magic_attr = typ.type.get(MAGIC_ATTR_NAME)
958957
if magic_attr is None or not magic_attr.plugin_generated:
@@ -1026,7 +1025,7 @@ def _get_expanded_attr_types(
10261025

10271026
def _meet_fields(types: list[Mapping[str, Type]]) -> Mapping[str, Type]:
10281027
"""
1029-
"Meet" the fields of a list of attrs classes, i.e. for each field, its new type will be the lower bound.
1028+
"Meets" the fields of a list of attrs classes, i.e. for each field, its new type will be the lower bound.
10301029
"""
10311030
field_to_types = defaultdict(list)
10321031
for fields in types:
@@ -1043,7 +1042,7 @@ def _meet_fields(types: list[Mapping[str, Type]]) -> Mapping[str, Type]:
10431042

10441043
def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
10451044
"""
1046-
Generate a signature for the 'attr.evolve' function that's specific to the call site
1045+
Generates a signature for the 'attr.evolve' function that's specific to the call site
10471046
and dependent on the type of the first argument.
10481047
"""
10491048
if len(ctx.args) != 2:
@@ -1071,44 +1070,3 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
10711070
fallback=ctx.default_signature.fallback,
10721071
name=f"{ctx.default_signature.name} of {inst_type_str}",
10731072
)
1074-
1075-
1076-
def fields_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
1077-
"""Provide the signature for `attrs.fields`."""
1078-
if len(ctx.args) != 1 or len(ctx.args[0]) != 1:
1079-
return ctx.default_signature
1080-
1081-
proper_type = get_proper_type(ctx.api.get_expression_type(ctx.args[0][0]))
1082-
1083-
# fields(Any) -> Any, fields(type[Any]) -> Any
1084-
if (
1085-
isinstance(proper_type, AnyType)
1086-
or isinstance(proper_type, TypeType)
1087-
and isinstance(proper_type.item, AnyType)
1088-
):
1089-
return ctx.default_signature
1090-
1091-
cls = None
1092-
arg_types = ctx.default_signature.arg_types
1093-
1094-
if isinstance(proper_type, TypeVarType):
1095-
inner = get_proper_type(proper_type.upper_bound)
1096-
if isinstance(inner, Instance):
1097-
# We need to work arg_types to compensate for the attrs stubs.
1098-
arg_types = [proper_type]
1099-
cls = inner.type
1100-
elif isinstance(proper_type, CallableType):
1101-
cls = proper_type.type_object()
1102-
1103-
if cls is not None and MAGIC_ATTR_NAME in cls.names:
1104-
# This is a proper attrs class.
1105-
ret_type = cls.names[MAGIC_ATTR_NAME].type
1106-
assert ret_type is not None
1107-
return ctx.default_signature.copy_modified(arg_types=arg_types, ret_type=ret_type)
1108-
1109-
ctx.api.fail(
1110-
f'Argument 1 to "fields" has incompatible type "{format_type_bare(proper_type, ctx.api.options)}"; expected an attrs class',
1111-
ctx.context,
1112-
)
1113-
1114-
return ctx.default_signature

mypy/plugins/default.py

-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type]
4747
return ctypes.array_constructor_callback
4848
elif fullname == "functools.singledispatch":
4949
return singledispatch.create_singledispatch_function_callback
50-
5150
return None
5251

5352
def get_function_signature_hook(
@@ -57,8 +56,6 @@ def get_function_signature_hook(
5756

5857
if fullname in ("attr.evolve", "attrs.evolve", "attr.assoc", "attrs.assoc"):
5958
return attrs.evolve_function_sig_callback
60-
elif fullname in ("attr.fields", "attrs.fields"):
61-
return attrs.fields_function_sig_callback
6259
elif fullname == "dataclasses.replace":
6360
return dataclasses.replace_function_sig_callback
6461
return None

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

-57
Original file line numberDiff line numberDiff line change
@@ -1554,63 +1554,6 @@ takes_attrs_cls(A(1, "")) # E: Argument 1 to "takes_attrs_cls" has incompatible
15541554
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
15551555
[builtins fixtures/plugin_attrs.pyi]
15561556

1557-
[case testAttrsFields]
1558-
import attr
1559-
from attrs import fields as f # Common usage.
1560-
1561-
@attr.define
1562-
class A:
1563-
b: int
1564-
c: str
1565-
1566-
reveal_type(f(A)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
1567-
reveal_type(f(A)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
1568-
reveal_type(f(A).b) # N: Revealed type is "attr.Attribute[builtins.int]"
1569-
f(A).x # E: "____main___A_AttrsAttributes__" has no attribute "x"
1570-
1571-
for ff in f(A):
1572-
reveal_type(ff) # N: Revealed type is "attr.Attribute[Any]"
1573-
1574-
[builtins fixtures/plugin_attrs.pyi]
1575-
1576-
[case testAttrsGenericFields]
1577-
from typing import TypeVar
1578-
1579-
import attr
1580-
from attrs import fields
1581-
1582-
@attr.define
1583-
class A:
1584-
b: int
1585-
c: str
1586-
1587-
TA = TypeVar('TA', bound=A)
1588-
1589-
def f(t: TA) -> None:
1590-
reveal_type(fields(t)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str], fallback=__main__.A.____main___A_AttrsAttributes__]"
1591-
reveal_type(fields(t)[0]) # N: Revealed type is "attr.Attribute[builtins.int]"
1592-
reveal_type(fields(t).b) # N: Revealed type is "attr.Attribute[builtins.int]"
1593-
fields(t).x # E: "____main___A_AttrsAttributes__" has no attribute "x"
1594-
1595-
1596-
[builtins fixtures/plugin_attrs.pyi]
1597-
1598-
[case testNonattrsFields]
1599-
# flags: --no-strict-optional
1600-
from typing import Any, cast, Type
1601-
from attrs import fields
1602-
1603-
class A:
1604-
b: int
1605-
c: str
1606-
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
1609-
fields(cast(Any, 42))
1610-
fields(cast(Type[Any], 43))
1611-
1612-
[builtins fixtures/plugin_attrs.pyi]
1613-
16141557
[case testAttrsInitMethodAlwaysGenerates]
16151558
from typing import Tuple
16161559
import attr

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

-2
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,3 @@ def field(
247247

248248
def evolve(inst: _T, **changes: Any) -> _T: ...
249249
def assoc(inst: _T, **changes: Any) -> _T: ...
250-
251-
def fields(cls: type) -> Any: ...

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

-2
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,3 @@ def field(
131131

132132
def evolve(inst: _T, **changes: Any) -> _T: ...
133133
def assoc(inst: _T, **changes: Any) -> _T: ...
134-
135-
def fields(cls: type) -> Any: ...

0 commit comments

Comments
 (0)