MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / inspect_formatargspec

Function inspect_formatargspec

lib/sqlalchemy/util/compat.py:187–256  ·  view source on GitHub ↗

Copy formatargspec from python 3.7 standard library. Python 3 has deprecated formatargspec and requested that Signature be used instead, however this requires a full reimplementation of formatargspec() in terms of creating Parameter objects and such. Instead of introducing all the o

(
    args: List[str],
    varargs: Optional[str] = None,
    varkw: Optional[str] = None,
    defaults: Optional[Sequence[Any]] = None,
    kwonlyargs: Optional[Sequence[str]] = (),
    kwonlydefaults: Optional[Mapping[str, Any]] = {},
    annotations: Mapping[str, Any] = {},
    formatarg: Callable[[str], str] = str,
    formatvarargs: Callable[[str], str] = lambda name: "*" + name,
    formatvarkw: Callable[[str], str] = lambda name: "**" + name,
    formatvalue: Callable[[Any], str] = lambda value: "=" + repr(value),
    formatreturns: Callable[[Any], str] = lambda text: " -> " + str(text),
    formatannotation: Callable[[Any], str] = _formatannotation,
)

Source from the content-addressed store, hash-verified

185
186
187def inspect_formatargspec(
188 args: List[str],
189 varargs: Optional[str] = None,
190 varkw: Optional[str] = None,
191 defaults: Optional[Sequence[Any]] = None,
192 kwonlyargs: Optional[Sequence[str]] = (),
193 kwonlydefaults: Optional[Mapping[str, Any]] = {},
194 annotations: Mapping[str, Any] = {},
195 formatarg: Callable[[str], str] = str,
196 formatvarargs: Callable[[str], str] = lambda name: "*" + name,
197 formatvarkw: Callable[[str], str] = lambda name: "**" + name,
198 formatvalue: Callable[[Any], str] = lambda value: "=" + repr(value),
199 formatreturns: Callable[[Any], str] = lambda text: " -> " + str(text),
200 formatannotation: Callable[[Any], str] = _formatannotation,
201) -> str:
202 """Copy formatargspec from python 3.7 standard library.
203
204 Python 3 has deprecated formatargspec and requested that Signature
205 be used instead, however this requires a full reimplementation
206 of formatargspec() in terms of creating Parameter objects and such.
207 Instead of introducing all the object-creation overhead and having
208 to reinvent from scratch, just copy their compatibility routine.
209
210 Ultimately we would need to rewrite our "decorator" routine completely
211 which is not really worth it right now, until all Python 2.x support
212 is dropped.
213
214 """
215
216 kwonlydefaults = kwonlydefaults or {}
217 annotations = annotations or {}
218
219 def formatargandannotation(arg):
220 result = formatarg(arg)
221 if arg in annotations:
222 result += ": " + formatannotation(annotations[arg])
223 return result
224
225 specs = []
226 if defaults:
227 firstdefault = len(args) - len(defaults)
228 else:
229 firstdefault = -1
230
231 for i, arg in enumerate(args):
232 spec = formatargandannotation(arg)
233 if defaults and i >= firstdefault:
234 spec = spec + formatvalue(defaults[i - firstdefault])
235 specs.append(spec)
236
237 if varargs is not None:
238 specs.append(formatvarargs(formatargandannotation(varargs)))
239 else:
240 if kwonlyargs:
241 specs.append("*")
242
243 if kwonlyargs:
244 for kwonlyarg in kwonlyargs:

Callers

nothing calls this directly

Calls 3

formatargandannotationFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected