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

Method get_expr_by_position

mypy/checkstrformat.py:488–512  ·  view source on GitHub ↗

Get positional replacement expression from '{0}, {1}'.format(x, y, ...) call. If the type is from *args, return TempNode( ). Return None in case of an error.

(self, pos: int, call: CallExpr)

Source from the content-addressed store, hash-verified

486 return result
487
488 def get_expr_by_position(self, pos: int, call: CallExpr) -> Expression | None:
489 """Get positional replacement expression from '{0}, {1}'.format(x, y, ...) call.
490
491 If the type is from *args, return TempNode(<item type>). Return None in case of
492 an error.
493 """
494 pos_args = [arg for arg, kind in zip(call.args, call.arg_kinds) if kind == ARG_POS]
495 if pos < len(pos_args):
496 return pos_args[pos]
497 star_args = [arg for arg, kind in zip(call.args, call.arg_kinds) if kind == ARG_STAR]
498 if not star_args:
499 return None
500
501 # Fall back to *args when present in call.
502 star_arg = star_args[0]
503 varargs_type = get_proper_type(self.chk.lookup_type(star_arg))
504 if not isinstance(varargs_type, Instance) or not varargs_type.type.has_base(
505 "typing.Sequence"
506 ):
507 # Error should be already reported.
508 return TempNode(AnyType(TypeOfAny.special_form))
509 iter_info = self.chk.named_generic_type(
510 "typing.Sequence", [AnyType(TypeOfAny.special_form)]
511 ).type
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.

Callers 1

Calls 10

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

Tested by

no test coverage detected