Skip to content

selftype in namedtuple methods #2408

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

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from mypy.errors import Errors, report_internal_error
from mypy.types import (
NoneTyp, CallableType, Overloaded, Instance, Type, TypeVarType, AnyType,
FunctionLike, UnboundType, TypeList, TypeVarDef,
FunctionLike, UnboundType, TypeList, TypeVarDef, TypeType,
TupleType, UnionType, StarType, EllipsisType, function_type)
from mypy.nodes import implicit_module_attrs
from mypy.typeanal import TypeAnalyser, TypeAnalyserPass3, analyze_type_alias
Expand Down Expand Up @@ -1803,31 +1803,41 @@ def add_field(var: Var, is_initialized_in_class: bool = False,
add_field(Var('_field_types', dictype), is_initialized_in_class=True)
add_field(Var('_source', strtype), is_initialized_in_class=True)

# TODO: SelfType should be bind to actual 'self'
this_type = fill_typevars(info)
tvd = TypeVarDef('NT', 1, [], fill_typevars(info))
selftype = TypeVarType(tvd)

def add_method(funcname: str, ret: Type, args: List[Argument], name=None,
is_classmethod=False) -> None:
if not is_classmethod:
args = [Argument(Var('self'), this_type, None, ARG_POS)] + args
if is_classmethod:
first = [Argument(Var('cls'), TypeType(selftype), None, ARG_POS)]
else:
first = [Argument(Var('self'), selftype, None, ARG_POS)]
args = first + args

types = [arg.type_annotation for arg in args]
items = [arg.variable.name() for arg in args]
arg_kinds = [arg.kind for arg in args]
signature = CallableType(types, arg_kinds, items, ret, function_type,
name=name or info.name() + '.' + funcname)
signature.is_classmethod_class = is_classmethod
signature.variables = [tvd]
func = FuncDef(funcname, args, Block([]), typ=signature)
func.info = info
func.is_class = is_classmethod
info.names[funcname] = SymbolTableNode(MDEF, func)
if is_classmethod:
v = Var(funcname, signature)
v.is_classmethod = True
v.info = info
dec = Decorator(func, [NameExpr('classmethod')], v)
info.names[funcname] = SymbolTableNode(MDEF, dec)
else:
info.names[funcname] = SymbolTableNode(MDEF, func)

add_method('_replace', ret=this_type,
add_method('_replace', ret=selftype,
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED) for var in vars])
add_method('__init__', ret=NoneTyp(), name=info.name(),
args=[Argument(var, var.type, None, ARG_POS) for var in vars])
add_method('_asdict', args=[], ret=ordereddictype)
# FIX: make it actual class method
add_method('_make', ret=this_type, is_classmethod=True,
add_method('_make', ret=selftype, is_classmethod=True,
args=[Argument(Var('iterable', iterable_type), iterable_type, None, ARG_POS),
Argument(Var('new'), AnyType(), EllipsisExpr(), ARG_NAMED),
Argument(Var('len'), AnyType(), EllipsisExpr(), ARG_NAMED)])
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class X(NamedTuple):
y: str

x: X
reveal_type(x._replace()) # E: Revealed type is 'Tuple[builtins.int, builtins.str, fallback=__main__.X]'
reveal_type(x._replace()) # E: Revealed type is '__main__.X*'
x._replace(x=5)
x._replace(y=5) # E: Argument 1 to X._replace has incompatible type "int"; expected "str"

Expand Down
9 changes: 4 additions & 5 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ from collections import namedtuple

X = namedtuple('X', ['x', 'y'])
x = None # type: X
reveal_type(x._replace()) # E: Revealed type is 'Tuple[Any, Any, fallback=__main__.X]'
reveal_type(x._replace()) # E: Revealed type is '__main__.X*'
x._replace(y=5)
x._replace(x=3)
x._replace(x=3, y=5)
Expand All @@ -279,19 +279,18 @@ from typing import NamedTuple

X = NamedTuple('X', [('x', int), ('y', str)])
x = None # type: X
reveal_type(x._replace()) # E: Revealed type is 'Tuple[builtins.int, builtins.str, fallback=__main__.X]'
reveal_type(x._replace()) # E: Revealed type is '__main__.X*'
x._replace(x=5)
x._replace(y=5) # E: Argument 1 to X._replace has incompatible type "int"; expected "str"


[case testNamedTupleMake]
from typing import NamedTuple

X = NamedTuple('X', [('x', int), ('y', str)])
reveal_type(X._make([5, 'a'])) # E: Revealed type is 'Tuple[builtins.int, builtins.str, fallback=__main__.X]'
reveal_type(X._make([5, 'a'])) # E: Revealed type is '__main__.X*'
X._make('a b') # E: Argument 1 to X._make has incompatible type "str"; expected Iterable[Any]

-- # FIX: not a proper class method
-- # FIX: not a proper class method
-- x = None # type: X
-- reveal_type(x._make([5, 'a'])) # E: Revealed type is 'Tuple[builtins.int, builtins.str, fallback=__main__.X]'
-- x._make('a b') # E: Argument 1 to X._make has incompatible type "str"; expected Iterable[Any]
Expand Down