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

Method get_expr_by_name

mypy/checkstrformat.py:514–539  ·  view source on GitHub ↗

Get named replacement expression from '{name}'.format(name=...) call. If the type is from **kwargs, return TempNode( ). Return None in case of an error.

(self, key: str, call: CallExpr)

Source from the content-addressed store, hash-verified

512 return TempNode(map_instance_to_supertype(varargs_type, iter_info).args[0])
513
514 def get_expr_by_name(self, key: str, call: CallExpr) -> Expression | None:
515 """Get named replacement expression from '{name}'.format(name=...) call.
516
517 If the type is from **kwargs, return TempNode(<item type>). Return None in case of
518 an error.
519 """
520 named_args = [
521 arg
522 for arg, kind, name in zip(call.args, call.arg_kinds, call.arg_names)
523 if kind == ARG_NAMED and name == key
524 ]
525 if named_args:
526 return named_args[0]
527 star_args_2 = [arg for arg, kind in zip(call.args, call.arg_kinds) if kind == ARG_STAR2]
528 if not star_args_2:
529 return None
530 star_arg_2 = star_args_2[0]
531 kwargs_type = get_proper_type(self.chk.lookup_type(star_arg_2))
532 if not isinstance(kwargs_type, Instance) or not kwargs_type.type.has_base(
533 "typing.Mapping"
534 ):
535 # Error should be already reported.
536 return TempNode(AnyType(TypeOfAny.special_form))
537 any_type = AnyType(TypeOfAny.special_form)
538 mapping_info = self.chk.named_generic_type("typing.Mapping", [any_type, any_type]).type
539 return TempNode(map_instance_to_supertype(kwargs_type, mapping_info).args[1])
540
541 def auto_generate_keys(self, all_specs: list[ConversionSpecifier], ctx: Context) -> bool:
542 """Translate '{} {name} {}' to '{0} {name} {1}'.

Callers 1

Calls 9

get_proper_typeFunction · 0.90
TempNodeClass · 0.90
AnyTypeClass · 0.90
zipFunction · 0.85
isinstanceFunction · 0.85
has_baseMethod · 0.80
lookup_typeMethod · 0.45
named_generic_typeMethod · 0.45

Tested by

no test coverage detected