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 __dataclass_fields__ and __attrs_attrs__ to dataclasses #8578

Merged
merged 18 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ def named_type(self, qualified_name: str, args: Optional[List[Type]] = None) ->
"""Construct an instance of a builtin type with given type arguments."""
raise NotImplementedError

@abstractmethod
def named_type_or_none(self,
qualified_name: str,
args: Optional[List[Type]] = None) -> Optional[Instance]:
"""Construct an instance of a builtin type with given type arguments."""
tkukushkin marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError

@abstractmethod
def parse_bool(self, expr: Expression) -> Optional[bool]:
"""Parse True/False literals."""
Expand Down
18 changes: 18 additions & 0 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
ctx.api.defer()
return

_add_attrs_magic_attribute(ctx)

# Save the attributes so that subclasses can reuse them.
ctx.cls.info.metadata['attrs'] = {
'attributes': [attr.serialize() for attr in attributes],
Expand Down Expand Up @@ -709,6 +711,22 @@ def _add_init(ctx: 'mypy.plugin.ClassDefContext', attributes: List[Attribute],
adder.add_method('__init__', args, NoneType())


def _add_attrs_magic_attribute(ctx: 'mypy.plugin.ClassDefContext') -> None:
attr_name = '__attrs_attrs__'
any_type = AnyType(TypeOfAny.explicit)
attribute_type = ctx.api.named_type_or_none('attr.Attribute', [any_type]) or any_type
var = Var(name=attr_name, type=ctx.api.named_type('__builtins__.tuple', [
tkukushkin marked this conversation as resolved.
Show resolved Hide resolved
attribute_type,
]))
var.info = ctx.cls.info
var._fullname = ctx.cls.info.fullname + '.' + attr_name
ctx.cls.info.names[attr_name] = SymbolTableNode(
kind=MDEF,
node=var,
plugin_generated=True,
)


class MethodAdder:
"""Helper to add methods to a TypeInfo.

Expand Down
24 changes: 23 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
add_method, _get_decorator_bool_argument, deserialize_and_fixup_type,
)
from mypy.typeops import map_type_from_supertype
from mypy.types import Type, Instance, NoneType, TypeVarDef, TypeVarType, get_proper_type
from mypy.types import (
Type, Instance, NoneType, TypeVarDef, TypeVarType, get_proper_type,
AnyType, TypeOfAny,
)
from mypy.server.trigger import make_wildcard_trigger

# The set of decorators that generate dataclasses.
Expand Down Expand Up @@ -173,6 +176,8 @@ def transform(self) -> None:

self.reset_init_only_vars(info, attributes)

self._add_dataclass_fields_magic_attribute()

info.metadata['dataclass'] = {
'attributes': [attr.serialize() for attr in attributes],
'frozen': decorator_arguments['frozen'],
Expand Down Expand Up @@ -353,6 +358,23 @@ def _freeze(self, attributes: List[DataclassAttribute]) -> None:
var._fullname = info.fullname + '.' + var.name
info.names[var.name] = SymbolTableNode(MDEF, var)

def _add_dataclass_fields_magic_attribute(self) -> None:
attr_name = '__dataclass_fields__'
any_type = AnyType(TypeOfAny.explicit)
field_type = self._ctx.api.named_type_or_none('dataclasses.Field', [any_type]) or any_type
attr_type = self._ctx.api.named_type('__builtins__.dict', [
self._ctx.api.named_type('__builtins__.str'),
field_type,
])
var = Var(name=attr_name, type=attr_type)
var.info = self._ctx.cls.info
var._fullname = self._ctx.cls.info.fullname + '.' + attr_name
self._ctx.cls.info.names[attr_name] = SymbolTableNode(
kind=MDEF,
node=var,
plugin_generated=True,
)


def dataclass_class_maker_callback(ctx: ClassDefContext) -> None:
"""Hooks into the class typechecking process to add support for dataclasses.
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1390,3 +1390,14 @@ class B(A):

reveal_type(B) # N: Revealed type is 'def (foo: builtins.int) -> __main__.B'
[builtins fixtures/bool.pyi]

[case testAttrsClassHasAttributeWithAttributes]
import attr

@attr.s
class A:
pass

reveal_type(A.__attrs_attrs__) # N: Revealed type is 'builtins.tuple[attr.Attribute[Any]]'

[builtins fixtures/attr.pyi]