MCPcopy Index your code
hub / github.com/python/mypy / _get_namedtuple_fields

Method _get_namedtuple_fields

mypy/stubgen.py:1025–1059  ·  view source on GitHub ↗
(self, call: CallExpr)

Source from the content-addressed store, hash-verified

1023 return self.get_fullname(expr.callee) in TYPED_NAMEDTUPLE_NAMES
1024
1025 def _get_namedtuple_fields(self, call: CallExpr) -> list[tuple[str, str]] | None:
1026 if self.is_namedtuple(call):
1027 fields_arg = call.args[1]
1028 if isinstance(fields_arg, StrExpr):
1029 field_names = fields_arg.value.replace(",", " ").split()
1030 elif isinstance(fields_arg, (ListExpr, TupleExpr)):
1031 field_names = []
1032 for field in fields_arg.items:
1033 if not isinstance(field, StrExpr):
1034 return None
1035 field_names.append(field.value)
1036 else:
1037 return None # Invalid namedtuple fields type
1038 if field_names:
1039 incomplete = self.add_name("_typeshed.Incomplete")
1040 return [(field_name, incomplete) for field_name in field_names]
1041 else:
1042 return []
1043
1044 elif self.is_typed_namedtuple(call):
1045 fields_arg = call.args[1]
1046 if not isinstance(fields_arg, (ListExpr, TupleExpr)):
1047 return None
1048 fields: list[tuple[str, str]] = []
1049 p = AliasPrinter(self)
1050 for field in fields_arg.items:
1051 if not (isinstance(field, TupleExpr) and len(field.items) == 2):
1052 return None
1053 field_name, field_type = field.items
1054 if not isinstance(field_name, StrExpr):
1055 return None
1056 fields.append((field_name.value, field_type.accept(p)))
1057 return fields
1058 else:
1059 return None # Not a named tuple call
1060
1061 def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
1062 if self._state == CLASS:

Callers 2

get_base_typesMethod · 0.95
process_namedtupleMethod · 0.95

Calls 10

is_namedtupleMethod · 0.95
is_typed_namedtupleMethod · 0.95
isinstanceFunction · 0.85
AliasPrinterClass · 0.85
lenFunction · 0.85
splitMethod · 0.80
replaceMethod · 0.80
appendMethod · 0.80
add_nameMethod · 0.80
acceptMethod · 0.45

Tested by

no test coverage detected