|
21 | 21 | deserialize_and_fixup_type |
22 | 22 | ) |
23 | 23 | from mypy.types import ( |
24 | | - Type, AnyType, TypeOfAny, CallableType, NoneType, TypeVarType, |
25 | | - Overloaded, UnionType, FunctionLike, get_proper_type |
| 24 | + TupleType, Type, AnyType, TypeOfAny, CallableType, NoneType, TypeVarType, |
| 25 | + Overloaded, UnionType, FunctionLike, get_proper_type, |
26 | 26 | ) |
27 | 27 | from mypy.typeops import make_simplified_union, map_type_from_supertype |
28 | 28 | from mypy.typevars import fill_typevars |
@@ -300,6 +300,8 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext', |
300 | 300 | ctx.api.defer() |
301 | 301 | return |
302 | 302 |
|
| 303 | + _add_attrs_magic_attribute(ctx, raw_attr_types=[info[attr.name].type for attr in attributes]) |
| 304 | + |
303 | 305 | # Save the attributes so that subclasses can reuse them. |
304 | 306 | ctx.cls.info.metadata['attrs'] = { |
305 | 307 | 'attributes': [attr.serialize() for attr in attributes], |
@@ -703,6 +705,27 @@ def _add_init(ctx: 'mypy.plugin.ClassDefContext', attributes: List[Attribute], |
703 | 705 | adder.add_method('__init__', args, NoneType()) |
704 | 706 |
|
705 | 707 |
|
| 708 | +def _add_attrs_magic_attribute(ctx: 'mypy.plugin.ClassDefContext', |
| 709 | + raw_attr_types: 'List[Optional[Type]]') -> None: |
| 710 | + attr_name = '__attrs_attrs__' |
| 711 | + any_type = AnyType(TypeOfAny.explicit) |
| 712 | + attributes_types: 'List[Type]' = [ |
| 713 | + ctx.api.named_type_or_none('attr.Attribute', [attr_type or any_type]) or any_type |
| 714 | + for attr_type in raw_attr_types |
| 715 | + ] |
| 716 | + fallback_type = ctx.api.named_type('__builtins__.tuple', [ |
| 717 | + ctx.api.named_type_or_none('attr.Attribute', [any_type]) or any_type, |
| 718 | + ]) |
| 719 | + var = Var(name=attr_name, type=TupleType(attributes_types, fallback=fallback_type)) |
| 720 | + var.info = ctx.cls.info |
| 721 | + var._fullname = ctx.cls.info.fullname + '.' + attr_name |
| 722 | + ctx.cls.info.names[attr_name] = SymbolTableNode( |
| 723 | + kind=MDEF, |
| 724 | + node=var, |
| 725 | + plugin_generated=True, |
| 726 | + ) |
| 727 | + |
| 728 | + |
706 | 729 | class MethodAdder: |
707 | 730 | """Helper to add methods to a TypeInfo. |
708 | 731 |
|
|
0 commit comments