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

Class ExternalSignatureGenerator

mypy/stubgenc.py:44–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42
43
44class ExternalSignatureGenerator(SignatureGenerator):
45 def __init__(
46 self, func_sigs: dict[str, str] | None = None, class_sigs: dict[str, str] | None = None
47 ) -> None:
48 """
49 Takes a mapping of function/method names to signatures and class name to
50 class signatures (usually corresponds to __init__).
51 """
52 self.func_sigs = func_sigs or {}
53 self.class_sigs = class_sigs or {}
54
55 @classmethod
56 def from_doc_dir(cls, doc_dir: str) -> ExternalSignatureGenerator:
57 """Instantiate from a directory of .rst files."""
58 all_sigs: list[Sig] = []
59 all_class_sigs: list[Sig] = []
60 for path in glob.glob(f"{doc_dir}/*.rst"):
61 with open(path) as f:
62 loc_sigs, loc_class_sigs = parse_all_signatures(f.readlines())
63 all_sigs += loc_sigs
64 all_class_sigs += loc_class_sigs
65 sigs = dict(find_unique_signatures(all_sigs))
66 class_sigs = dict(find_unique_signatures(all_class_sigs))
67 return ExternalSignatureGenerator(sigs, class_sigs)
68
69 def get_function_sig(
70 self, default_sig: FunctionSig, ctx: FunctionContext
71 ) -> list[FunctionSig] | None:
72 # method:
73 if (
74 ctx.class_info
75 and ctx.name in ("__new__", "__init__")
76 and ctx.name not in self.func_sigs
77 and ctx.class_info.name in self.class_sigs
78 ):
79 return [
80 FunctionSig(
81 name=ctx.name,
82 args=infer_arg_sig_from_anon_docstring(self.class_sigs[ctx.class_info.name]),
83 ret_type=infer_method_ret_type(ctx.name),
84 )
85 ]
86
87 # function:
88 if ctx.name not in self.func_sigs:
89 return None
90
91 inferred = [
92 FunctionSig(
93 name=ctx.name,
94 args=infer_arg_sig_from_anon_docstring(self.func_sigs[ctx.name]),
95 ret_type=None,
96 )
97 ]
98 if ctx.class_info:
99 return self.remove_self_type(inferred, ctx.class_info.self_var)
100 else:
101 return inferred

Callers 1

from_doc_dirMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…