|
71 | 71 | from mypy.errors import Errors, report_internal_error
|
72 | 72 | from mypy.types import (
|
73 | 73 | NoneTyp, CallableType, Overloaded, Instance, Type, TypeVarType, AnyType,
|
74 |
| - FunctionLike, UnboundType, TypeList, TypeVarDef, |
| 74 | + FunctionLike, UnboundType, TypeList, TypeVarDef, TypeType, |
75 | 75 | TupleType, UnionType, StarType, EllipsisType, function_type)
|
76 | 76 | from mypy.nodes import implicit_module_attrs
|
77 | 77 | from mypy.typeanal import TypeAnalyser, TypeAnalyserPass3, analyze_type_alias
|
@@ -1803,31 +1803,41 @@ def add_field(var: Var, is_initialized_in_class: bool = False,
|
1803 | 1803 | add_field(Var('_field_types', dictype), is_initialized_in_class=True)
|
1804 | 1804 | add_field(Var('_source', strtype), is_initialized_in_class=True)
|
1805 | 1805 |
|
1806 |
| - # TODO: SelfType should be bind to actual 'self' |
1807 |
| - this_type = fill_typevars(info) |
| 1806 | + tvd = TypeVarDef('NT', 1, [], fill_typevars(info)) |
| 1807 | + selftype = TypeVarType(tvd) |
1808 | 1808 |
|
1809 | 1809 | def add_method(funcname: str, ret: Type, args: List[Argument], name=None,
|
1810 | 1810 | is_classmethod=False) -> None:
|
1811 |
| - if not is_classmethod: |
1812 |
| - args = [Argument(Var('self'), this_type, None, ARG_POS)] + args |
| 1811 | + if is_classmethod: |
| 1812 | + first = [Argument(Var('cls'), TypeType(selftype), None, ARG_POS)] |
| 1813 | + else: |
| 1814 | + first = [Argument(Var('self'), selftype, None, ARG_POS)] |
| 1815 | + args = first + args |
| 1816 | + |
1813 | 1817 | types = [arg.type_annotation for arg in args]
|
1814 | 1818 | items = [arg.variable.name() for arg in args]
|
1815 | 1819 | arg_kinds = [arg.kind for arg in args]
|
1816 | 1820 | signature = CallableType(types, arg_kinds, items, ret, function_type,
|
1817 | 1821 | name=name or info.name() + '.' + funcname)
|
1818 |
| - signature.is_classmethod_class = is_classmethod |
| 1822 | + signature.variables = [tvd] |
1819 | 1823 | func = FuncDef(funcname, args, Block([]), typ=signature)
|
1820 | 1824 | func.info = info
|
1821 | 1825 | func.is_class = is_classmethod
|
1822 |
| - info.names[funcname] = SymbolTableNode(MDEF, func) |
| 1826 | + if is_classmethod: |
| 1827 | + v = Var(funcname, signature) |
| 1828 | + v.is_classmethod = True |
| 1829 | + v.info = info |
| 1830 | + dec = Decorator(func, [NameExpr('classmethod')], v) |
| 1831 | + info.names[funcname] = SymbolTableNode(MDEF, dec) |
| 1832 | + else: |
| 1833 | + info.names[funcname] = SymbolTableNode(MDEF, func) |
1823 | 1834 |
|
1824 |
| - add_method('_replace', ret=this_type, |
| 1835 | + add_method('_replace', ret=selftype, |
1825 | 1836 | args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED) for var in vars])
|
1826 | 1837 | add_method('__init__', ret=NoneTyp(), name=info.name(),
|
1827 | 1838 | args=[Argument(var, var.type, None, ARG_POS) for var in vars])
|
1828 | 1839 | add_method('_asdict', args=[], ret=ordereddictype)
|
1829 |
| - # FIX: make it actual class method |
1830 |
| - add_method('_make', ret=this_type, is_classmethod=True, |
| 1840 | + add_method('_make', ret=selftype, is_classmethod=True, |
1831 | 1841 | args=[Argument(Var('iterable', iterable_type), iterable_type, None, ARG_POS),
|
1832 | 1842 | Argument(Var('new'), AnyType(), EllipsisExpr(), ARG_NAMED),
|
1833 | 1843 | Argument(Var('len'), AnyType(), EllipsisExpr(), ARG_NAMED)])
|
|
0 commit comments