MCPcopy
hub / github.com/python/mypy / __str__

Method __str__

mypy/stubtest.py:918–957  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

916 self.varkw: T | None = None
917
918 def __str__(self) -> str:
919 def get_name(arg: Any) -> str:
920 if isinstance(arg, inspect.Parameter):
921 return arg.name
922 if isinstance(arg, nodes.Argument):
923 return arg.variable.name
924 raise AssertionError
925
926 def get_type(arg: Any) -> str | None:
927 if isinstance(arg, inspect.Parameter):
928 return None
929 if isinstance(arg, nodes.Argument):
930 return str(arg.variable.type or arg.type_annotation)
931 raise AssertionError
932
933 def has_default(arg: Any) -> bool:
934 if isinstance(arg, inspect.Parameter):
935 return arg.default is not inspect.Parameter.empty
936 if isinstance(arg, nodes.Argument):
937 return arg.kind.is_optional()
938 raise AssertionError
939
940 def get_desc(arg: Any) -> str:
941 arg_type = get_type(arg)
942 return (
943 get_name(arg)
944 + (f": {arg_type}" if arg_type else "")
945 + (" = ..." if has_default(arg) else "")
946 )
947
948 kw_only = sorted(self.kwonly.values(), key=lambda a: (has_default(a), get_name(a)))
949 ret = "def ("
950 ret += ", ".join(
951 [get_desc(arg) for arg in self.pos]
952 + (["*" + get_name(self.varpos)] if self.varpos else (["*"] if self.kwonly else []))
953 + [get_desc(arg) for arg in kw_only]
954 + (["**" + get_name(self.varkw)] if self.varkw else [])
955 )
956 ret += ")"
957 return ret
958
959 @staticmethod
960 def from_funcitem(stub: nodes.FuncItem) -> Signature[nodes.Argument]:

Callers

nothing calls this directly

Calls 3

sortedFunction · 0.85
valuesMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected