Skip to content

Commit 9dd0d39

Browse files
authored
Manually revert "Add support for attrs.fields (#15021)" (#15674)
This reverts commit 391ed85 for release-1.5. Similar to the revert for release-1.4 in commit 3bf9fdc. Had to do it manually since it won't revert cleanly.
1 parent 45e1bf7 commit 9dd0d39

File tree

5 files changed

+0
-102
lines changed

5 files changed

+0
-102
lines changed

mypy/plugins/attrs.py

-42
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,
@@ -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

-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def get_function_signature_hook(
5757

5858
if fullname in ("attr.evolve", "attrs.evolve", "attr.assoc", "attrs.assoc"):
5959
return attrs.evolve_function_sig_callback
60-
elif fullname in ("attr.fields", "attrs.fields"):
61-
return attrs.fields_function_sig_callback
6260
elif fullname == "dataclasses.replace":
6361
return dataclasses.replace_function_sig_callback
6462
return None

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

-54
Original file line numberDiff line numberDiff line change
@@ -1556,60 +1556,6 @@ takes_attrs_cls(A(1, "")) # E: Argument 1 to "takes_attrs_cls" has incompatible
15561556
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
15571557
[builtins fixtures/plugin_attrs.pyi]
15581558

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