8000 Support additional args to namedtuple() by JelleZijlstra · Pull Request #5215 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Support additional args to namedtuple() #5215

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 11 commits into from
Aug 3, 2018
Prev Previous commit
fix CI; remove obsolete comment
  • Loading branch information
JelleZijlstra committed Aug 3, 2018
commit ff63b1ba24e2d1c1c4bac176e9440ea91c650210
20 changes: 9 additions & 11 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ def check_namedtuple(self,
name += '@' + str(call.line)
if len(defaults) > 0:
default_items = {
# We don't have the actual argument expressions any more, so we
# just use Ellipsis for all defaults.
arg_name: default
for arg_name, default in zip(items[-len(defaults):], defaults)
}
Expand All @@ -174,8 +172,8 @@ def check_namedtuple(self,
call.analyzed.set_line(call.line, call.column)
return info

def parse_namedtuple_args(self, call: CallExpr,
fullname: str) -> Tuple[List[str], List[Type], List[Expression], bool]:
def parse_namedtuple_args(self, call: CallExpr, fullname: str
) -> Tuple[List[str], List[Type], List[Expression], bool]:
"""Parse a namedtuple() call into data needed to construct a type.

Returns a 4-tuple:
Expand Down Expand Up @@ -246,9 +244,9 @@ def parse_namedtuple_args(self, call: CallExpr,
defaults = defaults[:len(items)]
return items, types, defaults, ok

def parse_namedtuple_fields_with_types(
self, nodes: List[Expression], context: Context
) -> Tuple[List[str], List[Type], int, bool]:
def parse_namedtuple_fields_with_types(self, nodes: List[Expression], context: Context
) -> Tuple[List[str], List[Type], List[Expression],
bool]:
items = [] # type: List[str]
types = [] # type: List[Type]
for item in nodes:
Expand All @@ -268,12 +266,12 @@ def parse_namedtuple_fields_with_types(
types.append(self.api.anal_type(type))
else:
return self.fail_namedtuple_arg("Tuple expected as NamedTuple() field", item)
return items, types, 0, True
return items, types, [], True

def fail_namedtuple_arg(self, message: str,
context: Context) -> Tuple[List[str], List[Type], int, bool]:
def fail_namedtuple_arg(self, message: str, context: Context
) -> Tuple[List[str], List[Type], List[Expression], bool]:
self.fail(message, context)
return [], [], 0, False
return [], [], [], False

def build_namedtuple_typeinfo(self, name: str, items: List[str], types: List[Type],
default_items: Mapping[str, Expression]) -> TypeInfo:
Expand Down
0