langhelpers.py 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. # util/langhelpers.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """Routines to help with the creation, loading and introspection of
  9. modules, classes, hierarchies, attributes, functions, and methods.
  10. """
  11. from __future__ import annotations
  12. import collections
  13. import enum
  14. from functools import update_wrapper
  15. import inspect
  16. import itertools
  17. import operator
  18. import re
  19. import sys
  20. import textwrap
  21. import threading
  22. import types
  23. from types import CodeType
  24. from typing import Any
  25. from typing import Callable
  26. from typing import cast
  27. from typing import Dict
  28. from typing import FrozenSet
  29. from typing import Generic
  30. from typing import Iterator
  31. from typing import List
  32. from typing import Mapping
  33. from typing import NoReturn
  34. from typing import Optional
  35. from typing import overload
  36. from typing import Sequence
  37. from typing import Set
  38. from typing import Tuple
  39. from typing import Type
  40. from typing import TYPE_CHECKING
  41. from typing import TypeVar
  42. from typing import Union
  43. import warnings
  44. from . import _collections
  45. from . import compat
  46. from ._has_cy import HAS_CYEXTENSION
  47. from .typing import Literal
  48. from .. import exc
  49. _T = TypeVar("_T")
  50. _T_co = TypeVar("_T_co", covariant=True)
  51. _F = TypeVar("_F", bound=Callable[..., Any])
  52. _MP = TypeVar("_MP", bound="memoized_property[Any]")
  53. _MA = TypeVar("_MA", bound="HasMemoized.memoized_attribute[Any]")
  54. _HP = TypeVar("_HP", bound="hybridproperty[Any]")
  55. _HM = TypeVar("_HM", bound="hybridmethod[Any]")
  56. if compat.py314:
  57. # vendor a minimal form of get_annotations per
  58. # https://github.com/python/cpython/issues/133684#issuecomment-2863841891
  59. from annotationlib import call_annotate_function # type: ignore
  60. from annotationlib import Format
  61. def _get_and_call_annotate(obj, format): # noqa: A002
  62. annotate = getattr(obj, "__annotate__", None)
  63. if annotate is not None:
  64. ann = call_annotate_function(annotate, format, owner=obj)
  65. if not isinstance(ann, dict):
  66. raise ValueError(f"{obj!r}.__annotate__ returned a non-dict")
  67. return ann
  68. return None
  69. # this is ported from py3.13.0a7
  70. _BASE_GET_ANNOTATIONS = type.__dict__["__annotations__"].__get__ # type: ignore # noqa: E501
  71. def _get_dunder_annotations(obj):
  72. if isinstance(obj, type):
  73. try:
  74. ann = _BASE_GET_ANNOTATIONS(obj)
  75. except AttributeError:
  76. # For static types, the descriptor raises AttributeError.
  77. return {}
  78. else:
  79. ann = getattr(obj, "__annotations__", None)
  80. if ann is None:
  81. return {}
  82. if not isinstance(ann, dict):
  83. raise ValueError(
  84. f"{obj!r}.__annotations__ is neither a dict nor None"
  85. )
  86. return dict(ann)
  87. def _vendored_get_annotations(
  88. obj: Any, *, format: Format # noqa: A002
  89. ) -> Mapping[str, Any]:
  90. """A sparse implementation of annotationlib.get_annotations()"""
  91. try:
  92. ann = _get_dunder_annotations(obj)
  93. except Exception:
  94. pass
  95. else:
  96. if ann is not None:
  97. return dict(ann)
  98. # But if __annotations__ threw a NameError, we try calling __annotate__
  99. ann = _get_and_call_annotate(obj, format)
  100. if ann is None:
  101. # If that didn't work either, we have a very weird object:
  102. # evaluating
  103. # __annotations__ threw NameError and there is no __annotate__.
  104. # In that case,
  105. # we fall back to trying __annotations__ again.
  106. ann = _get_dunder_annotations(obj)
  107. if ann is None:
  108. if isinstance(obj, type) or callable(obj):
  109. return {}
  110. raise TypeError(f"{obj!r} does not have annotations")
  111. if not ann:
  112. return {}
  113. return dict(ann)
  114. def get_annotations(obj: Any) -> Mapping[str, Any]:
  115. # FORWARDREF has the effect of giving us ForwardRefs and not
  116. # actually trying to evaluate the annotations. We need this so
  117. # that the annotations act as much like
  118. # "from __future__ import annotations" as possible, which is going
  119. # away in future python as a separate mode
  120. return _vendored_get_annotations(obj, format=Format.FORWARDREF)
  121. elif compat.py310:
  122. def get_annotations(obj: Any) -> Mapping[str, Any]:
  123. return inspect.get_annotations(obj)
  124. else:
  125. def get_annotations(obj: Any) -> Mapping[str, Any]:
  126. # it's been observed that cls.__annotations__ can be non present.
  127. # it's not clear what causes this, running under tox py37/38 it
  128. # happens, running straight pytest it doesnt
  129. # https://docs.python.org/3/howto/annotations.html#annotations-howto
  130. if isinstance(obj, type):
  131. ann = obj.__dict__.get("__annotations__", None)
  132. else:
  133. ann = getattr(obj, "__annotations__", None)
  134. if ann is None:
  135. return _collections.EMPTY_DICT
  136. else:
  137. return cast("Mapping[str, Any]", ann)
  138. def md5_hex(x: Any) -> str:
  139. x = x.encode("utf-8")
  140. m = compat.md5_not_for_security()
  141. m.update(x)
  142. return cast(str, m.hexdigest())
  143. class safe_reraise:
  144. """Reraise an exception after invoking some
  145. handler code.
  146. Stores the existing exception info before
  147. invoking so that it is maintained across a potential
  148. coroutine context switch.
  149. e.g.::
  150. try:
  151. sess.commit()
  152. except:
  153. with safe_reraise():
  154. sess.rollback()
  155. TODO: we should at some point evaluate current behaviors in this regard
  156. based on current greenlet, gevent/eventlet implementations in Python 3, and
  157. also see the degree to which our own asyncio (based on greenlet also) is
  158. impacted by this. .rollback() will cause IO / context switch to occur in
  159. all these scenarios; what happens to the exception context from an
  160. "except:" block if we don't explicitly store it? Original issue was #2703.
  161. """
  162. __slots__ = ("_exc_info",)
  163. _exc_info: Union[
  164. None,
  165. Tuple[
  166. Type[BaseException],
  167. BaseException,
  168. types.TracebackType,
  169. ],
  170. Tuple[None, None, None],
  171. ]
  172. def __enter__(self) -> None:
  173. self._exc_info = sys.exc_info()
  174. def __exit__(
  175. self,
  176. type_: Optional[Type[BaseException]],
  177. value: Optional[BaseException],
  178. traceback: Optional[types.TracebackType],
  179. ) -> NoReturn:
  180. assert self._exc_info is not None
  181. # see #2703 for notes
  182. if type_ is None:
  183. exc_type, exc_value, exc_tb = self._exc_info
  184. assert exc_value is not None
  185. self._exc_info = None # remove potential circular references
  186. raise exc_value.with_traceback(exc_tb)
  187. else:
  188. self._exc_info = None # remove potential circular references
  189. assert value is not None
  190. raise value.with_traceback(traceback)
  191. def walk_subclasses(cls: Type[_T]) -> Iterator[Type[_T]]:
  192. seen: Set[Any] = set()
  193. stack = [cls]
  194. while stack:
  195. cls = stack.pop()
  196. if cls in seen:
  197. continue
  198. else:
  199. seen.add(cls)
  200. stack.extend(cls.__subclasses__())
  201. yield cls
  202. def string_or_unprintable(element: Any) -> str:
  203. if isinstance(element, str):
  204. return element
  205. else:
  206. try:
  207. return str(element)
  208. except Exception:
  209. return "unprintable element %r" % element
  210. def clsname_as_plain_name(
  211. cls: Type[Any], use_name: Optional[str] = None
  212. ) -> str:
  213. name = use_name or cls.__name__
  214. return " ".join(n.lower() for n in re.findall(r"([A-Z][a-z]+|SQL)", name))
  215. def method_is_overridden(
  216. instance_or_cls: Union[Type[Any], object],
  217. against_method: Callable[..., Any],
  218. ) -> bool:
  219. """Return True if the two class methods don't match."""
  220. if not isinstance(instance_or_cls, type):
  221. current_cls = instance_or_cls.__class__
  222. else:
  223. current_cls = instance_or_cls
  224. method_name = against_method.__name__
  225. current_method: types.MethodType = getattr(current_cls, method_name)
  226. return current_method != against_method
  227. def decode_slice(slc: slice) -> Tuple[Any, ...]:
  228. """decode a slice object as sent to __getitem__.
  229. takes into account the 2.5 __index__() method, basically.
  230. """
  231. ret: List[Any] = []
  232. for x in slc.start, slc.stop, slc.step:
  233. if hasattr(x, "__index__"):
  234. x = x.__index__()
  235. ret.append(x)
  236. return tuple(ret)
  237. def _unique_symbols(used: Sequence[str], *bases: str) -> Iterator[str]:
  238. used_set = set(used)
  239. for base in bases:
  240. pool = itertools.chain(
  241. (base,),
  242. map(lambda i: base + str(i), range(1000)),
  243. )
  244. for sym in pool:
  245. if sym not in used_set:
  246. used_set.add(sym)
  247. yield sym
  248. break
  249. else:
  250. raise NameError("exhausted namespace for symbol base %s" % base)
  251. def map_bits(fn: Callable[[int], Any], n: int) -> Iterator[Any]:
  252. """Call the given function given each nonzero bit from n."""
  253. while n:
  254. b = n & (~n + 1)
  255. yield fn(b)
  256. n ^= b
  257. _Fn = TypeVar("_Fn", bound="Callable[..., Any]")
  258. # this seems to be in flux in recent mypy versions
  259. def decorator(target: Callable[..., Any]) -> Callable[[_Fn], _Fn]:
  260. """A signature-matching decorator factory."""
  261. def decorate(fn: _Fn) -> _Fn:
  262. if not inspect.isfunction(fn) and not inspect.ismethod(fn):
  263. raise Exception("not a decoratable function")
  264. # Python 3.14 defer creating __annotations__ until its used.
  265. # We do not want to create __annotations__ now.
  266. annofunc = getattr(fn, "__annotate__", None)
  267. if annofunc is not None:
  268. fn.__annotate__ = None # type: ignore[union-attr]
  269. try:
  270. spec = compat.inspect_getfullargspec(fn)
  271. finally:
  272. fn.__annotate__ = annofunc # type: ignore[union-attr]
  273. else:
  274. spec = compat.inspect_getfullargspec(fn)
  275. # Do not generate code for annotations.
  276. # update_wrapper() copies the annotation from fn to decorated.
  277. # We use dummy defaults for code generation to avoid having
  278. # copy of large globals for compiling.
  279. # We copy __defaults__ and __kwdefaults__ from fn to decorated.
  280. empty_defaults = (None,) * len(spec.defaults or ())
  281. empty_kwdefaults = dict.fromkeys(spec.kwonlydefaults or ())
  282. spec = spec._replace(
  283. annotations={},
  284. defaults=empty_defaults,
  285. kwonlydefaults=empty_kwdefaults,
  286. )
  287. names = (
  288. tuple(cast("Tuple[str, ...]", spec[0]))
  289. + cast("Tuple[str, ...]", spec[1:3])
  290. + (fn.__name__,)
  291. )
  292. targ_name, fn_name = _unique_symbols(names, "target", "fn")
  293. metadata: Dict[str, Optional[str]] = dict(target=targ_name, fn=fn_name)
  294. metadata.update(format_argspec_plus(spec, grouped=False))
  295. metadata["name"] = fn.__name__
  296. if inspect.iscoroutinefunction(fn):
  297. metadata["prefix"] = "async "
  298. metadata["target_prefix"] = "await "
  299. else:
  300. metadata["prefix"] = ""
  301. metadata["target_prefix"] = ""
  302. # look for __ positional arguments. This is a convention in
  303. # SQLAlchemy that arguments should be passed positionally
  304. # rather than as keyword
  305. # arguments. note that apply_pos doesn't currently work in all cases
  306. # such as when a kw-only indicator "*" is present, which is why
  307. # we limit the use of this to just that case we can detect. As we add
  308. # more kinds of methods that use @decorator, things may have to
  309. # be further improved in this area
  310. if "__" in repr(spec[0]):
  311. code = (
  312. """\
  313. %(prefix)sdef %(name)s%(grouped_args)s:
  314. return %(target_prefix)s%(target)s(%(fn)s, %(apply_pos)s)
  315. """
  316. % metadata
  317. )
  318. else:
  319. code = (
  320. """\
  321. %(prefix)sdef %(name)s%(grouped_args)s:
  322. return %(target_prefix)s%(target)s(%(fn)s, %(apply_kw)s)
  323. """
  324. % metadata
  325. )
  326. env: Dict[str, Any] = {
  327. targ_name: target,
  328. fn_name: fn,
  329. "__name__": fn.__module__,
  330. }
  331. decorated = cast(
  332. types.FunctionType,
  333. _exec_code_in_env(code, env, fn.__name__),
  334. )
  335. decorated.__defaults__ = fn.__defaults__
  336. decorated.__kwdefaults__ = fn.__kwdefaults__ # type: ignore
  337. return update_wrapper(decorated, fn) # type: ignore[return-value]
  338. return update_wrapper(decorate, target) # type: ignore[return-value]
  339. def _exec_code_in_env(
  340. code: Union[str, types.CodeType], env: Dict[str, Any], fn_name: str
  341. ) -> Callable[..., Any]:
  342. exec(code, env)
  343. return env[fn_name] # type: ignore[no-any-return]
  344. _PF = TypeVar("_PF")
  345. _TE = TypeVar("_TE")
  346. class PluginLoader:
  347. def __init__(
  348. self, group: str, auto_fn: Optional[Callable[..., Any]] = None
  349. ):
  350. self.group = group
  351. self.impls: Dict[str, Any] = {}
  352. self.auto_fn = auto_fn
  353. def clear(self):
  354. self.impls.clear()
  355. def load(self, name: str) -> Any:
  356. if name in self.impls:
  357. return self.impls[name]()
  358. if self.auto_fn:
  359. loader = self.auto_fn(name)
  360. if loader:
  361. self.impls[name] = loader
  362. return loader()
  363. for impl in compat.importlib_metadata_get(self.group):
  364. if impl.name == name:
  365. self.impls[name] = impl.load
  366. return impl.load()
  367. raise exc.NoSuchModuleError(
  368. "Can't load plugin: %s:%s" % (self.group, name)
  369. )
  370. def register(self, name: str, modulepath: str, objname: str) -> None:
  371. def load():
  372. mod = __import__(modulepath)
  373. for token in modulepath.split(".")[1:]:
  374. mod = getattr(mod, token)
  375. return getattr(mod, objname)
  376. self.impls[name] = load
  377. def deregister(self, name: str) -> None:
  378. del self.impls[name]
  379. def _inspect_func_args(fn):
  380. try:
  381. co_varkeywords = inspect.CO_VARKEYWORDS
  382. except AttributeError:
  383. # https://docs.python.org/3/library/inspect.html
  384. # The flags are specific to CPython, and may not be defined in other
  385. # Python implementations. Furthermore, the flags are an implementation
  386. # detail, and can be removed or deprecated in future Python releases.
  387. spec = compat.inspect_getfullargspec(fn)
  388. return spec[0], bool(spec[2])
  389. else:
  390. # use fn.__code__ plus flags to reduce method call overhead
  391. co = fn.__code__
  392. nargs = co.co_argcount
  393. return (
  394. list(co.co_varnames[:nargs]),
  395. bool(co.co_flags & co_varkeywords),
  396. )
  397. @overload
  398. def get_cls_kwargs(
  399. cls: type,
  400. *,
  401. _set: Optional[Set[str]] = None,
  402. raiseerr: Literal[True] = ...,
  403. ) -> Set[str]: ...
  404. @overload
  405. def get_cls_kwargs(
  406. cls: type, *, _set: Optional[Set[str]] = None, raiseerr: bool = False
  407. ) -> Optional[Set[str]]: ...
  408. def get_cls_kwargs(
  409. cls: type, *, _set: Optional[Set[str]] = None, raiseerr: bool = False
  410. ) -> Optional[Set[str]]:
  411. r"""Return the full set of inherited kwargs for the given `cls`.
  412. Probes a class's __init__ method, collecting all named arguments. If the
  413. __init__ defines a \**kwargs catch-all, then the constructor is presumed
  414. to pass along unrecognized keywords to its base classes, and the
  415. collection process is repeated recursively on each of the bases.
  416. Uses a subset of inspect.getfullargspec() to cut down on method overhead,
  417. as this is used within the Core typing system to create copies of type
  418. objects which is a performance-sensitive operation.
  419. No anonymous tuple arguments please !
  420. """
  421. toplevel = _set is None
  422. if toplevel:
  423. _set = set()
  424. assert _set is not None
  425. ctr = cls.__dict__.get("__init__", False)
  426. has_init = (
  427. ctr
  428. and isinstance(ctr, types.FunctionType)
  429. and isinstance(ctr.__code__, types.CodeType)
  430. )
  431. if has_init:
  432. names, has_kw = _inspect_func_args(ctr)
  433. _set.update(names)
  434. if not has_kw and not toplevel:
  435. if raiseerr:
  436. raise TypeError(
  437. f"given cls {cls} doesn't have an __init__ method"
  438. )
  439. else:
  440. return None
  441. else:
  442. has_kw = False
  443. if not has_init or has_kw:
  444. for c in cls.__bases__:
  445. if get_cls_kwargs(c, _set=_set) is None:
  446. break
  447. _set.discard("self")
  448. return _set
  449. def get_func_kwargs(func: Callable[..., Any]) -> List[str]:
  450. """Return the set of legal kwargs for the given `func`.
  451. Uses getargspec so is safe to call for methods, functions,
  452. etc.
  453. """
  454. return compat.inspect_getfullargspec(func)[0]
  455. def get_callable_argspec(
  456. fn: Callable[..., Any], no_self: bool = False, _is_init: bool = False
  457. ) -> compat.FullArgSpec:
  458. """Return the argument signature for any callable.
  459. All pure-Python callables are accepted, including
  460. functions, methods, classes, objects with __call__;
  461. builtins and other edge cases like functools.partial() objects
  462. raise a TypeError.
  463. """
  464. if inspect.isbuiltin(fn):
  465. raise TypeError("Can't inspect builtin: %s" % fn)
  466. elif inspect.isfunction(fn):
  467. if _is_init and no_self:
  468. spec = compat.inspect_getfullargspec(fn)
  469. return compat.FullArgSpec(
  470. spec.args[1:],
  471. spec.varargs,
  472. spec.varkw,
  473. spec.defaults,
  474. spec.kwonlyargs,
  475. spec.kwonlydefaults,
  476. spec.annotations,
  477. )
  478. else:
  479. return compat.inspect_getfullargspec(fn)
  480. elif inspect.ismethod(fn):
  481. if no_self and (_is_init or fn.__self__):
  482. spec = compat.inspect_getfullargspec(fn.__func__)
  483. return compat.FullArgSpec(
  484. spec.args[1:],
  485. spec.varargs,
  486. spec.varkw,
  487. spec.defaults,
  488. spec.kwonlyargs,
  489. spec.kwonlydefaults,
  490. spec.annotations,
  491. )
  492. else:
  493. return compat.inspect_getfullargspec(fn.__func__)
  494. elif inspect.isclass(fn):
  495. return get_callable_argspec(
  496. fn.__init__, no_self=no_self, _is_init=True
  497. )
  498. elif hasattr(fn, "__func__"):
  499. return compat.inspect_getfullargspec(fn.__func__)
  500. elif hasattr(fn, "__call__"):
  501. if inspect.ismethod(fn.__call__):
  502. return get_callable_argspec(fn.__call__, no_self=no_self)
  503. else:
  504. raise TypeError("Can't inspect callable: %s" % fn)
  505. else:
  506. raise TypeError("Can't inspect callable: %s" % fn)
  507. def format_argspec_plus(
  508. fn: Union[Callable[..., Any], compat.FullArgSpec], grouped: bool = True
  509. ) -> Dict[str, Optional[str]]:
  510. """Returns a dictionary of formatted, introspected function arguments.
  511. A enhanced variant of inspect.formatargspec to support code generation.
  512. fn
  513. An inspectable callable or tuple of inspect getargspec() results.
  514. grouped
  515. Defaults to True; include (parens, around, argument) lists
  516. Returns:
  517. args
  518. Full inspect.formatargspec for fn
  519. self_arg
  520. The name of the first positional argument, varargs[0], or None
  521. if the function defines no positional arguments.
  522. apply_pos
  523. args, re-written in calling rather than receiving syntax. Arguments are
  524. passed positionally.
  525. apply_kw
  526. Like apply_pos, except keyword-ish args are passed as keywords.
  527. apply_pos_proxied
  528. Like apply_pos but omits the self/cls argument
  529. Example::
  530. >>> format_argspec_plus(lambda self, a, b, c=3, **d: 123)
  531. {'grouped_args': '(self, a, b, c=3, **d)',
  532. 'self_arg': 'self',
  533. 'apply_kw': '(self, a, b, c=c, **d)',
  534. 'apply_pos': '(self, a, b, c, **d)'}
  535. """
  536. if callable(fn):
  537. spec = compat.inspect_getfullargspec(fn)
  538. else:
  539. spec = fn
  540. args = compat.inspect_formatargspec(*spec)
  541. apply_pos = compat.inspect_formatargspec(
  542. spec[0], spec[1], spec[2], None, spec[4]
  543. )
  544. if spec[0]:
  545. self_arg = spec[0][0]
  546. apply_pos_proxied = compat.inspect_formatargspec(
  547. spec[0][1:], spec[1], spec[2], None, spec[4]
  548. )
  549. elif spec[1]:
  550. # I'm not sure what this is
  551. self_arg = "%s[0]" % spec[1]
  552. apply_pos_proxied = apply_pos
  553. else:
  554. self_arg = None
  555. apply_pos_proxied = apply_pos
  556. num_defaults = 0
  557. if spec[3]:
  558. num_defaults += len(cast(Tuple[Any], spec[3]))
  559. if spec[4]:
  560. num_defaults += len(spec[4])
  561. name_args = spec[0] + spec[4]
  562. defaulted_vals: Union[List[str], Tuple[()]]
  563. if num_defaults:
  564. defaulted_vals = name_args[0 - num_defaults :]
  565. else:
  566. defaulted_vals = ()
  567. apply_kw = compat.inspect_formatargspec(
  568. name_args,
  569. spec[1],
  570. spec[2],
  571. defaulted_vals,
  572. formatvalue=lambda x: "=" + str(x),
  573. )
  574. if spec[0]:
  575. apply_kw_proxied = compat.inspect_formatargspec(
  576. name_args[1:],
  577. spec[1],
  578. spec[2],
  579. defaulted_vals,
  580. formatvalue=lambda x: "=" + str(x),
  581. )
  582. else:
  583. apply_kw_proxied = apply_kw
  584. if grouped:
  585. return dict(
  586. grouped_args=args,
  587. self_arg=self_arg,
  588. apply_pos=apply_pos,
  589. apply_kw=apply_kw,
  590. apply_pos_proxied=apply_pos_proxied,
  591. apply_kw_proxied=apply_kw_proxied,
  592. )
  593. else:
  594. return dict(
  595. grouped_args=args,
  596. self_arg=self_arg,
  597. apply_pos=apply_pos[1:-1],
  598. apply_kw=apply_kw[1:-1],
  599. apply_pos_proxied=apply_pos_proxied[1:-1],
  600. apply_kw_proxied=apply_kw_proxied[1:-1],
  601. )
  602. def format_argspec_init(method, grouped=True):
  603. """format_argspec_plus with considerations for typical __init__ methods
  604. Wraps format_argspec_plus with error handling strategies for typical
  605. __init__ cases:
  606. .. sourcecode:: text
  607. object.__init__ -> (self)
  608. other unreflectable (usually C) -> (self, *args, **kwargs)
  609. """
  610. if method is object.__init__:
  611. grouped_args = "(self)"
  612. args = "(self)" if grouped else "self"
  613. proxied = "()" if grouped else ""
  614. else:
  615. try:
  616. return format_argspec_plus(method, grouped=grouped)
  617. except TypeError:
  618. grouped_args = "(self, *args, **kwargs)"
  619. args = grouped_args if grouped else "self, *args, **kwargs"
  620. proxied = "(*args, **kwargs)" if grouped else "*args, **kwargs"
  621. return dict(
  622. self_arg="self",
  623. grouped_args=grouped_args,
  624. apply_pos=args,
  625. apply_kw=args,
  626. apply_pos_proxied=proxied,
  627. apply_kw_proxied=proxied,
  628. )
  629. def create_proxy_methods(
  630. target_cls: Type[Any],
  631. target_cls_sphinx_name: str,
  632. proxy_cls_sphinx_name: str,
  633. classmethods: Sequence[str] = (),
  634. methods: Sequence[str] = (),
  635. attributes: Sequence[str] = (),
  636. use_intermediate_variable: Sequence[str] = (),
  637. ) -> Callable[[_T], _T]:
  638. """A class decorator indicating attributes should refer to a proxy
  639. class.
  640. This decorator is now a "marker" that does nothing at runtime. Instead,
  641. it is consumed by the tools/generate_proxy_methods.py script to
  642. statically generate proxy methods and attributes that are fully
  643. recognized by typing tools such as mypy.
  644. """
  645. def decorate(cls):
  646. return cls
  647. return decorate
  648. def getargspec_init(method):
  649. """inspect.getargspec with considerations for typical __init__ methods
  650. Wraps inspect.getargspec with error handling for typical __init__ cases:
  651. .. sourcecode:: text
  652. object.__init__ -> (self)
  653. other unreflectable (usually C) -> (self, *args, **kwargs)
  654. """
  655. try:
  656. return compat.inspect_getfullargspec(method)
  657. except TypeError:
  658. if method is object.__init__:
  659. return (["self"], None, None, None)
  660. else:
  661. return (["self"], "args", "kwargs", None)
  662. def unbound_method_to_callable(func_or_cls):
  663. """Adjust the incoming callable such that a 'self' argument is not
  664. required.
  665. """
  666. if isinstance(func_or_cls, types.MethodType) and not func_or_cls.__self__:
  667. return func_or_cls.__func__
  668. else:
  669. return func_or_cls
  670. def generic_repr(
  671. obj: Any,
  672. additional_kw: Sequence[Tuple[str, Any]] = (),
  673. to_inspect: Optional[Union[object, List[object]]] = None,
  674. omit_kwarg: Sequence[str] = (),
  675. ) -> str:
  676. """Produce a __repr__() based on direct association of the __init__()
  677. specification vs. same-named attributes present.
  678. """
  679. if to_inspect is None:
  680. to_inspect = [obj]
  681. else:
  682. to_inspect = _collections.to_list(to_inspect)
  683. missing = object()
  684. pos_args = []
  685. kw_args: _collections.OrderedDict[str, Any] = _collections.OrderedDict()
  686. vargs = None
  687. for i, insp in enumerate(to_inspect):
  688. try:
  689. spec = compat.inspect_getfullargspec(insp.__init__)
  690. except TypeError:
  691. continue
  692. else:
  693. default_len = len(spec.defaults) if spec.defaults else 0
  694. if i == 0:
  695. if spec.varargs:
  696. vargs = spec.varargs
  697. if default_len:
  698. pos_args.extend(spec.args[1:-default_len])
  699. else:
  700. pos_args.extend(spec.args[1:])
  701. else:
  702. kw_args.update(
  703. [(arg, missing) for arg in spec.args[1:-default_len]]
  704. )
  705. if default_len:
  706. assert spec.defaults
  707. kw_args.update(
  708. [
  709. (arg, default)
  710. for arg, default in zip(
  711. spec.args[-default_len:], spec.defaults
  712. )
  713. ]
  714. )
  715. output: List[str] = []
  716. output.extend(repr(getattr(obj, arg, None)) for arg in pos_args)
  717. if vargs is not None and hasattr(obj, vargs):
  718. output.extend([repr(val) for val in getattr(obj, vargs)])
  719. for arg, defval in kw_args.items():
  720. if arg in omit_kwarg:
  721. continue
  722. try:
  723. val = getattr(obj, arg, missing)
  724. if val is not missing and val != defval:
  725. output.append("%s=%r" % (arg, val))
  726. except Exception:
  727. pass
  728. if additional_kw:
  729. for arg, defval in additional_kw:
  730. try:
  731. val = getattr(obj, arg, missing)
  732. if val is not missing and val != defval:
  733. output.append("%s=%r" % (arg, val))
  734. except Exception:
  735. pass
  736. return "%s(%s)" % (obj.__class__.__name__, ", ".join(output))
  737. class portable_instancemethod:
  738. """Turn an instancemethod into a (parent, name) pair
  739. to produce a serializable callable.
  740. """
  741. __slots__ = "target", "name", "kwargs", "__weakref__"
  742. def __getstate__(self):
  743. return {
  744. "target": self.target,
  745. "name": self.name,
  746. "kwargs": self.kwargs,
  747. }
  748. def __setstate__(self, state):
  749. self.target = state["target"]
  750. self.name = state["name"]
  751. self.kwargs = state.get("kwargs", ())
  752. def __init__(self, meth, kwargs=()):
  753. self.target = meth.__self__
  754. self.name = meth.__name__
  755. self.kwargs = kwargs
  756. def __call__(self, *arg, **kw):
  757. kw.update(self.kwargs)
  758. return getattr(self.target, self.name)(*arg, **kw)
  759. def class_hierarchy(cls):
  760. """Return an unordered sequence of all classes related to cls.
  761. Traverses diamond hierarchies.
  762. Fibs slightly: subclasses of builtin types are not returned. Thus
  763. class_hierarchy(class A(object)) returns (A, object), not A plus every
  764. class systemwide that derives from object.
  765. """
  766. hier = {cls}
  767. process = list(cls.__mro__)
  768. while process:
  769. c = process.pop()
  770. bases = (_ for _ in c.__bases__ if _ not in hier)
  771. for b in bases:
  772. process.append(b)
  773. hier.add(b)
  774. if c.__module__ == "builtins" or not hasattr(c, "__subclasses__"):
  775. continue
  776. for s in [
  777. _
  778. for _ in (
  779. c.__subclasses__()
  780. if not issubclass(c, type)
  781. else c.__subclasses__(c)
  782. )
  783. if _ not in hier
  784. ]:
  785. process.append(s)
  786. hier.add(s)
  787. return list(hier)
  788. def iterate_attributes(cls):
  789. """iterate all the keys and attributes associated
  790. with a class, without using getattr().
  791. Does not use getattr() so that class-sensitive
  792. descriptors (i.e. property.__get__()) are not called.
  793. """
  794. keys = dir(cls)
  795. for key in keys:
  796. for c in cls.__mro__:
  797. if key in c.__dict__:
  798. yield (key, c.__dict__[key])
  799. break
  800. def monkeypatch_proxied_specials(
  801. into_cls,
  802. from_cls,
  803. skip=None,
  804. only=None,
  805. name="self.proxy",
  806. from_instance=None,
  807. ):
  808. """Automates delegation of __specials__ for a proxying type."""
  809. if only:
  810. dunders = only
  811. else:
  812. if skip is None:
  813. skip = (
  814. "__slots__",
  815. "__del__",
  816. "__getattribute__",
  817. "__metaclass__",
  818. "__getstate__",
  819. "__setstate__",
  820. )
  821. dunders = [
  822. m
  823. for m in dir(from_cls)
  824. if (
  825. m.startswith("__")
  826. and m.endswith("__")
  827. and not hasattr(into_cls, m)
  828. and m not in skip
  829. )
  830. ]
  831. for method in dunders:
  832. try:
  833. maybe_fn = getattr(from_cls, method)
  834. if not hasattr(maybe_fn, "__call__"):
  835. continue
  836. maybe_fn = getattr(maybe_fn, "__func__", maybe_fn)
  837. fn = cast(types.FunctionType, maybe_fn)
  838. except AttributeError:
  839. continue
  840. try:
  841. spec = compat.inspect_getfullargspec(fn)
  842. fn_args = compat.inspect_formatargspec(spec[0])
  843. d_args = compat.inspect_formatargspec(spec[0][1:])
  844. except TypeError:
  845. fn_args = "(self, *args, **kw)"
  846. d_args = "(*args, **kw)"
  847. py = (
  848. "def %(method)s%(fn_args)s: "
  849. "return %(name)s.%(method)s%(d_args)s" % locals()
  850. )
  851. env: Dict[str, types.FunctionType] = (
  852. from_instance is not None and {name: from_instance} or {}
  853. )
  854. exec(py, env)
  855. try:
  856. env[method].__defaults__ = fn.__defaults__
  857. except AttributeError:
  858. pass
  859. setattr(into_cls, method, env[method])
  860. def methods_equivalent(meth1, meth2):
  861. """Return True if the two methods are the same implementation."""
  862. return getattr(meth1, "__func__", meth1) is getattr(
  863. meth2, "__func__", meth2
  864. )
  865. def as_interface(obj, cls=None, methods=None, required=None):
  866. """Ensure basic interface compliance for an instance or dict of callables.
  867. Checks that ``obj`` implements public methods of ``cls`` or has members
  868. listed in ``methods``. If ``required`` is not supplied, implementing at
  869. least one interface method is sufficient. Methods present on ``obj`` that
  870. are not in the interface are ignored.
  871. If ``obj`` is a dict and ``dict`` does not meet the interface
  872. requirements, the keys of the dictionary are inspected. Keys present in
  873. ``obj`` that are not in the interface will raise TypeErrors.
  874. Raises TypeError if ``obj`` does not meet the interface criteria.
  875. In all passing cases, an object with callable members is returned. In the
  876. simple case, ``obj`` is returned as-is; if dict processing kicks in then
  877. an anonymous class is returned.
  878. obj
  879. A type, instance, or dictionary of callables.
  880. cls
  881. Optional, a type. All public methods of cls are considered the
  882. interface. An ``obj`` instance of cls will always pass, ignoring
  883. ``required``..
  884. methods
  885. Optional, a sequence of method names to consider as the interface.
  886. required
  887. Optional, a sequence of mandatory implementations. If omitted, an
  888. ``obj`` that provides at least one interface method is considered
  889. sufficient. As a convenience, required may be a type, in which case
  890. all public methods of the type are required.
  891. """
  892. if not cls and not methods:
  893. raise TypeError("a class or collection of method names are required")
  894. if isinstance(cls, type) and isinstance(obj, cls):
  895. return obj
  896. interface = set(methods or [m for m in dir(cls) if not m.startswith("_")])
  897. implemented = set(dir(obj))
  898. complies = operator.ge
  899. if isinstance(required, type):
  900. required = interface
  901. elif not required:
  902. required = set()
  903. complies = operator.gt
  904. else:
  905. required = set(required)
  906. if complies(implemented.intersection(interface), required):
  907. return obj
  908. # No dict duck typing here.
  909. if not isinstance(obj, dict):
  910. qualifier = complies is operator.gt and "any of" or "all of"
  911. raise TypeError(
  912. "%r does not implement %s: %s"
  913. % (obj, qualifier, ", ".join(interface))
  914. )
  915. class AnonymousInterface:
  916. """A callable-holding shell."""
  917. if cls:
  918. AnonymousInterface.__name__ = "Anonymous" + cls.__name__
  919. found = set()
  920. for method, impl in dictlike_iteritems(obj):
  921. if method not in interface:
  922. raise TypeError("%r: unknown in this interface" % method)
  923. if not callable(impl):
  924. raise TypeError("%r=%r is not callable" % (method, impl))
  925. setattr(AnonymousInterface, method, staticmethod(impl))
  926. found.add(method)
  927. if complies(found, required):
  928. return AnonymousInterface
  929. raise TypeError(
  930. "dictionary does not contain required keys %s"
  931. % ", ".join(required - found)
  932. )
  933. _GFD = TypeVar("_GFD", bound="generic_fn_descriptor[Any]")
  934. class generic_fn_descriptor(Generic[_T_co]):
  935. """Descriptor which proxies a function when the attribute is not
  936. present in dict
  937. This superclass is organized in a particular way with "memoized" and
  938. "non-memoized" implementation classes that are hidden from type checkers,
  939. as Mypy seems to not be able to handle seeing multiple kinds of descriptor
  940. classes used for the same attribute.
  941. """
  942. fget: Callable[..., _T_co]
  943. __doc__: Optional[str]
  944. __name__: str
  945. def __init__(self, fget: Callable[..., _T_co], doc: Optional[str] = None):
  946. self.fget = fget
  947. self.__doc__ = doc or fget.__doc__
  948. self.__name__ = fget.__name__
  949. @overload
  950. def __get__(self: _GFD, obj: None, cls: Any) -> _GFD: ...
  951. @overload
  952. def __get__(self, obj: object, cls: Any) -> _T_co: ...
  953. def __get__(self: _GFD, obj: Any, cls: Any) -> Union[_GFD, _T_co]:
  954. raise NotImplementedError()
  955. if TYPE_CHECKING:
  956. def __set__(self, instance: Any, value: Any) -> None: ...
  957. def __delete__(self, instance: Any) -> None: ...
  958. def _reset(self, obj: Any) -> None:
  959. raise NotImplementedError()
  960. @classmethod
  961. def reset(cls, obj: Any, name: str) -> None:
  962. raise NotImplementedError()
  963. class _non_memoized_property(generic_fn_descriptor[_T_co]):
  964. """a plain descriptor that proxies a function.
  965. primary rationale is to provide a plain attribute that's
  966. compatible with memoized_property which is also recognized as equivalent
  967. by mypy.
  968. """
  969. if not TYPE_CHECKING:
  970. def __get__(self, obj, cls):
  971. if obj is None:
  972. return self
  973. return self.fget(obj)
  974. class _memoized_property(generic_fn_descriptor[_T_co]):
  975. """A read-only @property that is only evaluated once."""
  976. if not TYPE_CHECKING:
  977. def __get__(self, obj, cls):
  978. if obj is None:
  979. return self
  980. obj.__dict__[self.__name__] = result = self.fget(obj)
  981. return result
  982. def _reset(self, obj):
  983. _memoized_property.reset(obj, self.__name__)
  984. @classmethod
  985. def reset(cls, obj, name):
  986. obj.__dict__.pop(name, None)
  987. # despite many attempts to get Mypy to recognize an overridden descriptor
  988. # where one is memoized and the other isn't, there seems to be no reliable
  989. # way other than completely deceiving the type checker into thinking there
  990. # is just one single descriptor type everywhere. Otherwise, if a superclass
  991. # has non-memoized and subclass has memoized, that requires
  992. # "class memoized(non_memoized)". but then if a superclass has memoized and
  993. # superclass has non-memoized, the class hierarchy of the descriptors
  994. # would need to be reversed; "class non_memoized(memoized)". so there's no
  995. # way to achieve this.
  996. # additional issues, RO properties:
  997. # https://github.com/python/mypy/issues/12440
  998. if TYPE_CHECKING:
  999. # allow memoized and non-memoized to be freely mixed by having them
  1000. # be the same class
  1001. memoized_property = generic_fn_descriptor
  1002. non_memoized_property = generic_fn_descriptor
  1003. # for read only situations, mypy only sees @property as read only.
  1004. # read only is needed when a subtype specializes the return type
  1005. # of a property, meaning assignment needs to be disallowed
  1006. ro_memoized_property = property
  1007. ro_non_memoized_property = property
  1008. else:
  1009. memoized_property = ro_memoized_property = _memoized_property
  1010. non_memoized_property = ro_non_memoized_property = _non_memoized_property
  1011. def memoized_instancemethod(fn: _F) -> _F:
  1012. """Decorate a method memoize its return value.
  1013. Best applied to no-arg methods: memoization is not sensitive to
  1014. argument values, and will always return the same value even when
  1015. called with different arguments.
  1016. """
  1017. def oneshot(self, *args, **kw):
  1018. result = fn(self, *args, **kw)
  1019. def memo(*a, **kw):
  1020. return result
  1021. memo.__name__ = fn.__name__
  1022. memo.__doc__ = fn.__doc__
  1023. self.__dict__[fn.__name__] = memo
  1024. return result
  1025. return update_wrapper(oneshot, fn) # type: ignore
  1026. class HasMemoized:
  1027. """A mixin class that maintains the names of memoized elements in a
  1028. collection for easy cache clearing, generative, etc.
  1029. """
  1030. if not TYPE_CHECKING:
  1031. # support classes that want to have __slots__ with an explicit
  1032. # slot for __dict__. not sure if that requires base __slots__ here.
  1033. __slots__ = ()
  1034. _memoized_keys: FrozenSet[str] = frozenset()
  1035. def _reset_memoizations(self) -> None:
  1036. for elem in self._memoized_keys:
  1037. self.__dict__.pop(elem, None)
  1038. def _assert_no_memoizations(self) -> None:
  1039. for elem in self._memoized_keys:
  1040. assert elem not in self.__dict__
  1041. def _set_memoized_attribute(self, key: str, value: Any) -> None:
  1042. self.__dict__[key] = value
  1043. self._memoized_keys |= {key}
  1044. class memoized_attribute(memoized_property[_T]):
  1045. """A read-only @property that is only evaluated once.
  1046. :meta private:
  1047. """
  1048. fget: Callable[..., _T]
  1049. __doc__: Optional[str]
  1050. __name__: str
  1051. def __init__(self, fget: Callable[..., _T], doc: Optional[str] = None):
  1052. self.fget = fget
  1053. self.__doc__ = doc or fget.__doc__
  1054. self.__name__ = fget.__name__
  1055. @overload
  1056. def __get__(self: _MA, obj: None, cls: Any) -> _MA: ...
  1057. @overload
  1058. def __get__(self, obj: Any, cls: Any) -> _T: ...
  1059. def __get__(self, obj, cls):
  1060. if obj is None:
  1061. return self
  1062. obj.__dict__[self.__name__] = result = self.fget(obj)
  1063. obj._memoized_keys |= {self.__name__}
  1064. return result
  1065. @classmethod
  1066. def memoized_instancemethod(cls, fn: _F) -> _F:
  1067. """Decorate a method memoize its return value.
  1068. :meta private:
  1069. """
  1070. def oneshot(self: Any, *args: Any, **kw: Any) -> Any:
  1071. result = fn(self, *args, **kw)
  1072. def memo(*a, **kw):
  1073. return result
  1074. memo.__name__ = fn.__name__
  1075. memo.__doc__ = fn.__doc__
  1076. self.__dict__[fn.__name__] = memo
  1077. self._memoized_keys |= {fn.__name__}
  1078. return result
  1079. return update_wrapper(oneshot, fn) # type: ignore
  1080. if TYPE_CHECKING:
  1081. HasMemoized_ro_memoized_attribute = property
  1082. else:
  1083. HasMemoized_ro_memoized_attribute = HasMemoized.memoized_attribute
  1084. class MemoizedSlots:
  1085. """Apply memoized items to an object using a __getattr__ scheme.
  1086. This allows the functionality of memoized_property and
  1087. memoized_instancemethod to be available to a class using __slots__.
  1088. """
  1089. __slots__ = ()
  1090. def _fallback_getattr(self, key):
  1091. raise AttributeError(key)
  1092. def __getattr__(self, key: str) -> Any:
  1093. if key.startswith("_memoized_attr_") or key.startswith(
  1094. "_memoized_method_"
  1095. ):
  1096. raise AttributeError(key)
  1097. # to avoid recursion errors when interacting with other __getattr__
  1098. # schemes that refer to this one, when testing for memoized method
  1099. # look at __class__ only rather than going into __getattr__ again.
  1100. elif hasattr(self.__class__, f"_memoized_attr_{key}"):
  1101. value = getattr(self, f"_memoized_attr_{key}")()
  1102. setattr(self, key, value)
  1103. return value
  1104. elif hasattr(self.__class__, f"_memoized_method_{key}"):
  1105. fn = getattr(self, f"_memoized_method_{key}")
  1106. def oneshot(*args, **kw):
  1107. result = fn(*args, **kw)
  1108. def memo(*a, **kw):
  1109. return result
  1110. memo.__name__ = fn.__name__
  1111. memo.__doc__ = fn.__doc__
  1112. setattr(self, key, memo)
  1113. return result
  1114. oneshot.__doc__ = fn.__doc__
  1115. return oneshot
  1116. else:
  1117. return self._fallback_getattr(key)
  1118. # from paste.deploy.converters
  1119. def asbool(obj: Any) -> bool:
  1120. if isinstance(obj, str):
  1121. obj = obj.strip().lower()
  1122. if obj in ["true", "yes", "on", "y", "t", "1"]:
  1123. return True
  1124. elif obj in ["false", "no", "off", "n", "f", "0"]:
  1125. return False
  1126. else:
  1127. raise ValueError("String is not true/false: %r" % obj)
  1128. return bool(obj)
  1129. def bool_or_str(*text: str) -> Callable[[str], Union[str, bool]]:
  1130. """Return a callable that will evaluate a string as
  1131. boolean, or one of a set of "alternate" string values.
  1132. """
  1133. def bool_or_value(obj: str) -> Union[str, bool]:
  1134. if obj in text:
  1135. return obj
  1136. else:
  1137. return asbool(obj)
  1138. return bool_or_value
  1139. def asint(value: Any) -> Optional[int]:
  1140. """Coerce to integer."""
  1141. if value is None:
  1142. return value
  1143. return int(value)
  1144. def coerce_kw_type(
  1145. kw: Dict[str, Any],
  1146. key: str,
  1147. type_: Type[Any],
  1148. flexi_bool: bool = True,
  1149. dest: Optional[Dict[str, Any]] = None,
  1150. ) -> None:
  1151. r"""If 'key' is present in dict 'kw', coerce its value to type 'type\_' if
  1152. necessary. If 'flexi_bool' is True, the string '0' is considered false
  1153. when coercing to boolean.
  1154. """
  1155. if dest is None:
  1156. dest = kw
  1157. if (
  1158. key in kw
  1159. and (not isinstance(type_, type) or not isinstance(kw[key], type_))
  1160. and kw[key] is not None
  1161. ):
  1162. if type_ is bool and flexi_bool:
  1163. dest[key] = asbool(kw[key])
  1164. else:
  1165. dest[key] = type_(kw[key])
  1166. def constructor_key(obj: Any, cls: Type[Any]) -> Tuple[Any, ...]:
  1167. """Produce a tuple structure that is cacheable using the __dict__ of
  1168. obj to retrieve values
  1169. """
  1170. names = get_cls_kwargs(cls)
  1171. return (cls,) + tuple(
  1172. (k, obj.__dict__[k]) for k in names if k in obj.__dict__
  1173. )
  1174. def constructor_copy(obj: _T, cls: Type[_T], *args: Any, **kw: Any) -> _T:
  1175. """Instantiate cls using the __dict__ of obj as constructor arguments.
  1176. Uses inspect to match the named arguments of ``cls``.
  1177. """
  1178. names = get_cls_kwargs(cls)
  1179. kw.update(
  1180. (k, obj.__dict__[k]) for k in names.difference(kw) if k in obj.__dict__
  1181. )
  1182. return cls(*args, **kw)
  1183. def counter() -> Callable[[], int]:
  1184. """Return a threadsafe counter function."""
  1185. lock = threading.Lock()
  1186. counter = itertools.count(1)
  1187. # avoid the 2to3 "next" transformation...
  1188. def _next():
  1189. with lock:
  1190. return next(counter)
  1191. return _next
  1192. def duck_type_collection(
  1193. specimen: Any, default: Optional[Type[Any]] = None
  1194. ) -> Optional[Type[Any]]:
  1195. """Given an instance or class, guess if it is or is acting as one of
  1196. the basic collection types: list, set and dict. If the __emulates__
  1197. property is present, return that preferentially.
  1198. """
  1199. if hasattr(specimen, "__emulates__"):
  1200. # canonicalize set vs sets.Set to a standard: the builtin set
  1201. if specimen.__emulates__ is not None and issubclass(
  1202. specimen.__emulates__, set
  1203. ):
  1204. return set
  1205. else:
  1206. return specimen.__emulates__ # type: ignore
  1207. isa = issubclass if isinstance(specimen, type) else isinstance
  1208. if isa(specimen, list):
  1209. return list
  1210. elif isa(specimen, set):
  1211. return set
  1212. elif isa(specimen, dict):
  1213. return dict
  1214. if hasattr(specimen, "append"):
  1215. return list
  1216. elif hasattr(specimen, "add"):
  1217. return set
  1218. elif hasattr(specimen, "set"):
  1219. return dict
  1220. else:
  1221. return default
  1222. def assert_arg_type(
  1223. arg: Any, argtype: Union[Tuple[Type[Any], ...], Type[Any]], name: str
  1224. ) -> Any:
  1225. if isinstance(arg, argtype):
  1226. return arg
  1227. else:
  1228. if isinstance(argtype, tuple):
  1229. raise exc.ArgumentError(
  1230. "Argument '%s' is expected to be one of type %s, got '%s'"
  1231. % (name, " or ".join("'%s'" % a for a in argtype), type(arg))
  1232. )
  1233. else:
  1234. raise exc.ArgumentError(
  1235. "Argument '%s' is expected to be of type '%s', got '%s'"
  1236. % (name, argtype, type(arg))
  1237. )
  1238. def dictlike_iteritems(dictlike):
  1239. """Return a (key, value) iterator for almost any dict-like object."""
  1240. if hasattr(dictlike, "items"):
  1241. return list(dictlike.items())
  1242. getter = getattr(dictlike, "__getitem__", getattr(dictlike, "get", None))
  1243. if getter is None:
  1244. raise TypeError("Object '%r' is not dict-like" % dictlike)
  1245. if hasattr(dictlike, "iterkeys"):
  1246. def iterator():
  1247. for key in dictlike.iterkeys():
  1248. assert getter is not None
  1249. yield key, getter(key)
  1250. return iterator()
  1251. elif hasattr(dictlike, "keys"):
  1252. return iter((key, getter(key)) for key in dictlike.keys())
  1253. else:
  1254. raise TypeError("Object '%r' is not dict-like" % dictlike)
  1255. class classproperty(property):
  1256. """A decorator that behaves like @property except that operates
  1257. on classes rather than instances.
  1258. The decorator is currently special when using the declarative
  1259. module, but note that the
  1260. :class:`~.sqlalchemy.ext.declarative.declared_attr`
  1261. decorator should be used for this purpose with declarative.
  1262. """
  1263. fget: Callable[[Any], Any]
  1264. def __init__(self, fget: Callable[[Any], Any], *arg: Any, **kw: Any):
  1265. super().__init__(fget, *arg, **kw)
  1266. self.__doc__ = fget.__doc__
  1267. def __get__(self, obj: Any, cls: Optional[type] = None) -> Any:
  1268. return self.fget(cls)
  1269. class hybridproperty(Generic[_T]):
  1270. def __init__(self, func: Callable[..., _T]):
  1271. self.func = func
  1272. self.clslevel = func
  1273. def __get__(self, instance: Any, owner: Any) -> _T:
  1274. if instance is None:
  1275. clsval = self.clslevel(owner)
  1276. return clsval
  1277. else:
  1278. return self.func(instance)
  1279. def classlevel(self, func: Callable[..., Any]) -> hybridproperty[_T]:
  1280. self.clslevel = func
  1281. return self
  1282. class rw_hybridproperty(Generic[_T]):
  1283. def __init__(self, func: Callable[..., _T]):
  1284. self.func = func
  1285. self.clslevel = func
  1286. self.setfn: Optional[Callable[..., Any]] = None
  1287. def __get__(self, instance: Any, owner: Any) -> _T:
  1288. if instance is None:
  1289. clsval = self.clslevel(owner)
  1290. return clsval
  1291. else:
  1292. return self.func(instance)
  1293. def __set__(self, instance: Any, value: Any) -> None:
  1294. assert self.setfn is not None
  1295. self.setfn(instance, value)
  1296. def setter(self, func: Callable[..., Any]) -> rw_hybridproperty[_T]:
  1297. self.setfn = func
  1298. return self
  1299. def classlevel(self, func: Callable[..., Any]) -> rw_hybridproperty[_T]:
  1300. self.clslevel = func
  1301. return self
  1302. class hybridmethod(Generic[_T]):
  1303. """Decorate a function as cls- or instance- level."""
  1304. def __init__(self, func: Callable[..., _T]):
  1305. self.func = self.__func__ = func
  1306. self.clslevel = func
  1307. def __get__(self, instance: Any, owner: Any) -> Callable[..., _T]:
  1308. if instance is None:
  1309. return self.clslevel.__get__(owner, owner.__class__) # type:ignore
  1310. else:
  1311. return self.func.__get__(instance, owner) # type:ignore
  1312. def classlevel(self, func: Callable[..., Any]) -> hybridmethod[_T]:
  1313. self.clslevel = func
  1314. return self
  1315. class symbol(int):
  1316. """A constant symbol.
  1317. >>> symbol("foo") is symbol("foo")
  1318. True
  1319. >>> symbol("foo")
  1320. <symbol 'foo>
  1321. A slight refinement of the MAGICCOOKIE=object() pattern. The primary
  1322. advantage of symbol() is its repr(). They are also singletons.
  1323. Repeated calls of symbol('name') will all return the same instance.
  1324. """
  1325. name: str
  1326. symbols: Dict[str, symbol] = {}
  1327. _lock = threading.Lock()
  1328. def __new__(
  1329. cls,
  1330. name: str,
  1331. doc: Optional[str] = None,
  1332. canonical: Optional[int] = None,
  1333. ) -> symbol:
  1334. with cls._lock:
  1335. sym = cls.symbols.get(name)
  1336. if sym is None:
  1337. assert isinstance(name, str)
  1338. if canonical is None:
  1339. canonical = hash(name)
  1340. sym = int.__new__(symbol, canonical)
  1341. sym.name = name
  1342. if doc:
  1343. sym.__doc__ = doc
  1344. # NOTE: we should ultimately get rid of this global thing,
  1345. # however, currently it is to support pickling. The best
  1346. # change would be when we are on py3.11 at a minimum, we
  1347. # switch to stdlib enum.IntFlag.
  1348. cls.symbols[name] = sym
  1349. else:
  1350. if canonical and canonical != sym:
  1351. raise TypeError(
  1352. f"Can't replace canonical symbol for {name!r} "
  1353. f"with new int value {canonical}"
  1354. )
  1355. return sym
  1356. def __reduce__(self):
  1357. return symbol, (self.name, "x", int(self))
  1358. def __str__(self):
  1359. return repr(self)
  1360. def __repr__(self):
  1361. return f"symbol({self.name!r})"
  1362. class _IntFlagMeta(type):
  1363. def __init__(
  1364. cls,
  1365. classname: str,
  1366. bases: Tuple[Type[Any], ...],
  1367. dict_: Dict[str, Any],
  1368. **kw: Any,
  1369. ) -> None:
  1370. items: List[symbol]
  1371. cls._items = items = []
  1372. for k, v in dict_.items():
  1373. if re.match(r"^__.*__$", k):
  1374. continue
  1375. if isinstance(v, int):
  1376. sym = symbol(k, canonical=v)
  1377. elif not k.startswith("_"):
  1378. raise TypeError("Expected integer values for IntFlag")
  1379. else:
  1380. continue
  1381. setattr(cls, k, sym)
  1382. items.append(sym)
  1383. cls.__members__ = _collections.immutabledict(
  1384. {sym.name: sym for sym in items}
  1385. )
  1386. def __iter__(self) -> Iterator[symbol]:
  1387. raise NotImplementedError(
  1388. "iter not implemented to ensure compatibility with "
  1389. "Python 3.11 IntFlag. Please use __members__. See "
  1390. "https://github.com/python/cpython/issues/99304"
  1391. )
  1392. class _FastIntFlag(metaclass=_IntFlagMeta):
  1393. """An 'IntFlag' copycat that isn't slow when performing bitwise
  1394. operations.
  1395. the ``FastIntFlag`` class will return ``enum.IntFlag`` under TYPE_CHECKING
  1396. and ``_FastIntFlag`` otherwise.
  1397. """
  1398. if TYPE_CHECKING:
  1399. from enum import IntFlag
  1400. FastIntFlag = IntFlag
  1401. else:
  1402. FastIntFlag = _FastIntFlag
  1403. _E = TypeVar("_E", bound=enum.Enum)
  1404. def parse_user_argument_for_enum(
  1405. arg: Any,
  1406. choices: Dict[_E, List[Any]],
  1407. name: str,
  1408. resolve_symbol_names: bool = False,
  1409. ) -> Optional[_E]:
  1410. """Given a user parameter, parse the parameter into a chosen value
  1411. from a list of choice objects, typically Enum values.
  1412. The user argument can be a string name that matches the name of a
  1413. symbol, or the symbol object itself, or any number of alternate choices
  1414. such as True/False/ None etc.
  1415. :param arg: the user argument.
  1416. :param choices: dictionary of enum values to lists of possible
  1417. entries for each.
  1418. :param name: name of the argument. Used in an :class:`.ArgumentError`
  1419. that is raised if the parameter doesn't match any available argument.
  1420. """
  1421. for enum_value, choice in choices.items():
  1422. if arg is enum_value:
  1423. return enum_value
  1424. elif resolve_symbol_names and arg == enum_value.name:
  1425. return enum_value
  1426. elif arg in choice:
  1427. return enum_value
  1428. if arg is None:
  1429. return None
  1430. raise exc.ArgumentError(f"Invalid value for '{name}': {arg!r}")
  1431. _creation_order = 1
  1432. def set_creation_order(instance: Any) -> None:
  1433. """Assign a '_creation_order' sequence to the given instance.
  1434. This allows multiple instances to be sorted in order of creation
  1435. (typically within a single thread; the counter is not particularly
  1436. threadsafe).
  1437. """
  1438. global _creation_order
  1439. instance._creation_order = _creation_order
  1440. _creation_order += 1
  1441. def warn_exception(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
  1442. """executes the given function, catches all exceptions and converts to
  1443. a warning.
  1444. """
  1445. try:
  1446. return func(*args, **kwargs)
  1447. except Exception:
  1448. warn("%s('%s') ignored" % sys.exc_info()[0:2])
  1449. def ellipses_string(value, len_=25):
  1450. try:
  1451. if len(value) > len_:
  1452. return "%s..." % value[0:len_]
  1453. else:
  1454. return value
  1455. except TypeError:
  1456. return value
  1457. class _hash_limit_string(str):
  1458. """A string subclass that can only be hashed on a maximum amount
  1459. of unique values.
  1460. This is used for warnings so that we can send out parameterized warnings
  1461. without the __warningregistry__ of the module, or the non-overridable
  1462. "once" registry within warnings.py, overloading memory,
  1463. """
  1464. _hash: int
  1465. def __new__(
  1466. cls, value: str, num: int, args: Sequence[Any]
  1467. ) -> _hash_limit_string:
  1468. interpolated = (value % args) + (
  1469. " (this warning may be suppressed after %d occurrences)" % num
  1470. )
  1471. self = super().__new__(cls, interpolated)
  1472. self._hash = hash("%s_%d" % (value, hash(interpolated) % num))
  1473. return self
  1474. def __hash__(self) -> int:
  1475. return self._hash
  1476. def __eq__(self, other: Any) -> bool:
  1477. return hash(self) == hash(other)
  1478. def warn(msg: str, code: Optional[str] = None) -> None:
  1479. """Issue a warning.
  1480. If msg is a string, :class:`.exc.SAWarning` is used as
  1481. the category.
  1482. """
  1483. if code:
  1484. _warnings_warn(exc.SAWarning(msg, code=code))
  1485. else:
  1486. _warnings_warn(msg, exc.SAWarning)
  1487. def warn_limited(msg: str, args: Sequence[Any]) -> None:
  1488. """Issue a warning with a parameterized string, limiting the number
  1489. of registrations.
  1490. """
  1491. if args:
  1492. msg = _hash_limit_string(msg, 10, args)
  1493. _warnings_warn(msg, exc.SAWarning)
  1494. _warning_tags: Dict[CodeType, Tuple[str, Type[Warning]]] = {}
  1495. def tag_method_for_warnings(
  1496. message: str, category: Type[Warning]
  1497. ) -> Callable[[_F], _F]:
  1498. def go(fn):
  1499. _warning_tags[fn.__code__] = (message, category)
  1500. return fn
  1501. return go
  1502. _not_sa_pattern = re.compile(r"^(?:sqlalchemy\.(?!testing)|alembic\.)")
  1503. def _warnings_warn(
  1504. message: Union[str, Warning],
  1505. category: Optional[Type[Warning]] = None,
  1506. stacklevel: int = 2,
  1507. ) -> None:
  1508. # adjust the given stacklevel to be outside of SQLAlchemy
  1509. try:
  1510. frame = sys._getframe(stacklevel)
  1511. except ValueError:
  1512. # being called from less than 3 (or given) stacklevels, weird,
  1513. # but don't crash
  1514. stacklevel = 0
  1515. except:
  1516. # _getframe() doesn't work, weird interpreter issue, weird,
  1517. # ok, but don't crash
  1518. stacklevel = 0
  1519. else:
  1520. stacklevel_found = warning_tag_found = False
  1521. while frame is not None:
  1522. # using __name__ here requires that we have __name__ in the
  1523. # __globals__ of the decorated string functions we make also.
  1524. # we generate this using {"__name__": fn.__module__}
  1525. if not stacklevel_found and not re.match(
  1526. _not_sa_pattern, frame.f_globals.get("__name__", "")
  1527. ):
  1528. # stop incrementing stack level if an out-of-SQLA line
  1529. # were found.
  1530. stacklevel_found = True
  1531. # however, for the warning tag thing, we have to keep
  1532. # scanning up the whole traceback
  1533. if frame.f_code in _warning_tags:
  1534. warning_tag_found = True
  1535. (_suffix, _category) = _warning_tags[frame.f_code]
  1536. category = category or _category
  1537. message = f"{message} ({_suffix})"
  1538. frame = frame.f_back # type: ignore[assignment]
  1539. if not stacklevel_found:
  1540. stacklevel += 1
  1541. elif stacklevel_found and warning_tag_found:
  1542. break
  1543. if category is not None:
  1544. warnings.warn(message, category, stacklevel=stacklevel + 1)
  1545. else:
  1546. warnings.warn(message, stacklevel=stacklevel + 1)
  1547. def only_once(
  1548. fn: Callable[..., _T], retry_on_exception: bool
  1549. ) -> Callable[..., Optional[_T]]:
  1550. """Decorate the given function to be a no-op after it is called exactly
  1551. once."""
  1552. once = [fn]
  1553. def go(*arg: Any, **kw: Any) -> Optional[_T]:
  1554. # strong reference fn so that it isn't garbage collected,
  1555. # which interferes with the event system's expectations
  1556. strong_fn = fn # noqa
  1557. if once:
  1558. once_fn = once.pop()
  1559. try:
  1560. return once_fn(*arg, **kw)
  1561. except:
  1562. if retry_on_exception:
  1563. once.insert(0, once_fn)
  1564. raise
  1565. return None
  1566. return go
  1567. _SQLA_RE = re.compile(r"sqlalchemy/([a-z_]+/){0,2}[a-z_]+\.py")
  1568. _UNITTEST_RE = re.compile(r"unit(?:2|test2?/)")
  1569. def chop_traceback(
  1570. tb: List[str],
  1571. exclude_prefix: re.Pattern[str] = _UNITTEST_RE,
  1572. exclude_suffix: re.Pattern[str] = _SQLA_RE,
  1573. ) -> List[str]:
  1574. """Chop extraneous lines off beginning and end of a traceback.
  1575. :param tb:
  1576. a list of traceback lines as returned by ``traceback.format_stack()``
  1577. :param exclude_prefix:
  1578. a regular expression object matching lines to skip at beginning of
  1579. ``tb``
  1580. :param exclude_suffix:
  1581. a regular expression object matching lines to skip at end of ``tb``
  1582. """
  1583. start = 0
  1584. end = len(tb) - 1
  1585. while start <= end and exclude_prefix.search(tb[start]):
  1586. start += 1
  1587. while start <= end and exclude_suffix.search(tb[end]):
  1588. end -= 1
  1589. return tb[start : end + 1]
  1590. NoneType = type(None)
  1591. def attrsetter(attrname):
  1592. code = "def set(obj, value): obj.%s = value" % attrname
  1593. env = locals().copy()
  1594. exec(code, env)
  1595. return env["set"]
  1596. _dunders = re.compile("^__.+__$")
  1597. class TypingOnly:
  1598. """A mixin class that marks a class as 'typing only', meaning it has
  1599. absolutely no methods, attributes, or runtime functionality whatsoever.
  1600. """
  1601. __slots__ = ()
  1602. def __init_subclass__(cls) -> None:
  1603. if TypingOnly in cls.__bases__:
  1604. remaining = {
  1605. name for name in cls.__dict__ if not _dunders.match(name)
  1606. }
  1607. if remaining:
  1608. raise AssertionError(
  1609. f"Class {cls} directly inherits TypingOnly but has "
  1610. f"additional attributes {remaining}."
  1611. )
  1612. super().__init_subclass__()
  1613. class EnsureKWArg:
  1614. r"""Apply translation of functions to accept \**kw arguments if they
  1615. don't already.
  1616. Used to ensure cross-compatibility with third party legacy code, for things
  1617. like compiler visit methods that need to accept ``**kw`` arguments,
  1618. but may have been copied from old code that didn't accept them.
  1619. """
  1620. ensure_kwarg: str
  1621. """a regular expression that indicates method names for which the method
  1622. should accept ``**kw`` arguments.
  1623. The class will scan for methods matching the name template and decorate
  1624. them if necessary to ensure ``**kw`` parameters are accepted.
  1625. """
  1626. def __init_subclass__(cls) -> None:
  1627. fn_reg = cls.ensure_kwarg
  1628. clsdict = cls.__dict__
  1629. if fn_reg:
  1630. for key in clsdict:
  1631. m = re.match(fn_reg, key)
  1632. if m:
  1633. fn = clsdict[key]
  1634. spec = compat.inspect_getfullargspec(fn)
  1635. if not spec.varkw:
  1636. wrapped = cls._wrap_w_kw(fn)
  1637. setattr(cls, key, wrapped)
  1638. super().__init_subclass__()
  1639. @classmethod
  1640. def _wrap_w_kw(cls, fn: Callable[..., Any]) -> Callable[..., Any]:
  1641. def wrap(*arg: Any, **kw: Any) -> Any:
  1642. return fn(*arg)
  1643. return update_wrapper(wrap, fn)
  1644. def wrap_callable(wrapper, fn):
  1645. """Augment functools.update_wrapper() to work with objects with
  1646. a ``__call__()`` method.
  1647. :param fn:
  1648. object with __call__ method
  1649. """
  1650. if hasattr(fn, "__name__"):
  1651. return update_wrapper(wrapper, fn)
  1652. else:
  1653. _f = wrapper
  1654. _f.__name__ = fn.__class__.__name__
  1655. if hasattr(fn, "__module__"):
  1656. _f.__module__ = fn.__module__
  1657. if hasattr(fn.__call__, "__doc__") and fn.__call__.__doc__:
  1658. _f.__doc__ = fn.__call__.__doc__
  1659. elif fn.__doc__:
  1660. _f.__doc__ = fn.__doc__
  1661. return _f
  1662. def quoted_token_parser(value):
  1663. """Parse a dotted identifier with accommodation for quoted names.
  1664. Includes support for SQL-style double quotes as a literal character.
  1665. E.g.::
  1666. >>> quoted_token_parser("name")
  1667. ["name"]
  1668. >>> quoted_token_parser("schema.name")
  1669. ["schema", "name"]
  1670. >>> quoted_token_parser('"Schema"."Name"')
  1671. ['Schema', 'Name']
  1672. >>> quoted_token_parser('"Schema"."Name""Foo"')
  1673. ['Schema', 'Name""Foo']
  1674. """
  1675. if '"' not in value:
  1676. return value.split(".")
  1677. # 0 = outside of quotes
  1678. # 1 = inside of quotes
  1679. state = 0
  1680. result: List[List[str]] = [[]]
  1681. idx = 0
  1682. lv = len(value)
  1683. while idx < lv:
  1684. char = value[idx]
  1685. if char == '"':
  1686. if state == 1 and idx < lv - 1 and value[idx + 1] == '"':
  1687. result[-1].append('"')
  1688. idx += 1
  1689. else:
  1690. state ^= 1
  1691. elif char == "." and state == 0:
  1692. result.append([])
  1693. else:
  1694. result[-1].append(char)
  1695. idx += 1
  1696. return ["".join(token) for token in result]
  1697. def add_parameter_text(params: Any, text: str) -> Callable[[_F], _F]:
  1698. params = _collections.to_list(params)
  1699. def decorate(fn):
  1700. doc = fn.__doc__ is not None and fn.__doc__ or ""
  1701. if doc:
  1702. doc = inject_param_text(doc, {param: text for param in params})
  1703. fn.__doc__ = doc
  1704. return fn
  1705. return decorate
  1706. def _dedent_docstring(text: str) -> str:
  1707. split_text = text.split("\n", 1)
  1708. if len(split_text) == 1:
  1709. return text
  1710. else:
  1711. firstline, remaining = split_text
  1712. if not firstline.startswith(" "):
  1713. return firstline + "\n" + textwrap.dedent(remaining)
  1714. else:
  1715. return textwrap.dedent(text)
  1716. def inject_docstring_text(
  1717. given_doctext: Optional[str], injecttext: str, pos: int
  1718. ) -> str:
  1719. doctext: str = _dedent_docstring(given_doctext or "")
  1720. lines = doctext.split("\n")
  1721. if len(lines) == 1:
  1722. lines.append("")
  1723. injectlines = textwrap.dedent(injecttext).split("\n")
  1724. if injectlines[0]:
  1725. injectlines.insert(0, "")
  1726. blanks = [num for num, line in enumerate(lines) if not line.strip()]
  1727. blanks.insert(0, 0)
  1728. inject_pos = blanks[min(pos, len(blanks) - 1)]
  1729. lines = lines[0:inject_pos] + injectlines + lines[inject_pos:]
  1730. return "\n".join(lines)
  1731. _param_reg = re.compile(r"(\s+):param (.+?):")
  1732. def inject_param_text(doctext: str, inject_params: Dict[str, str]) -> str:
  1733. doclines = collections.deque(doctext.splitlines())
  1734. lines = []
  1735. # TODO: this is not working for params like ":param case_sensitive=True:"
  1736. to_inject = None
  1737. while doclines:
  1738. line = doclines.popleft()
  1739. m = _param_reg.match(line)
  1740. if to_inject is None:
  1741. if m:
  1742. param = m.group(2).lstrip("*")
  1743. if param in inject_params:
  1744. # default indent to that of :param: plus one
  1745. indent = " " * len(m.group(1)) + " "
  1746. # but if the next line has text, use that line's
  1747. # indentation
  1748. if doclines:
  1749. m2 = re.match(r"(\s+)\S", doclines[0])
  1750. if m2:
  1751. indent = " " * len(m2.group(1))
  1752. to_inject = indent + inject_params[param]
  1753. elif m:
  1754. lines.extend(["\n", to_inject, "\n"])
  1755. to_inject = None
  1756. elif not line.rstrip():
  1757. lines.extend([line, to_inject, "\n"])
  1758. to_inject = None
  1759. elif line.endswith("::"):
  1760. # TODO: this still won't cover if the code example itself has
  1761. # blank lines in it, need to detect those via indentation.
  1762. lines.extend([line, doclines.popleft()])
  1763. continue
  1764. lines.append(line)
  1765. return "\n".join(lines)
  1766. def repr_tuple_names(names: List[str]) -> Optional[str]:
  1767. """Trims a list of strings from the middle and return a string of up to
  1768. four elements. Strings greater than 11 characters will be truncated"""
  1769. if len(names) == 0:
  1770. return None
  1771. flag = len(names) <= 4
  1772. names = names[0:4] if flag else names[0:3] + names[-1:]
  1773. res = ["%s.." % name[:11] if len(name) > 11 else name for name in names]
  1774. if flag:
  1775. return ", ".join(res)
  1776. else:
  1777. return "%s, ..., %s" % (", ".join(res[0:3]), res[-1])
  1778. def has_compiled_ext(raise_=False):
  1779. if HAS_CYEXTENSION:
  1780. return True
  1781. elif raise_:
  1782. raise ImportError(
  1783. "cython extensions were expected to be installed, "
  1784. "but are not present"
  1785. )
  1786. else:
  1787. return False
  1788. class _Missing(enum.Enum):
  1789. Missing = enum.auto()
  1790. Missing = _Missing.Missing
  1791. MissingOr = Union[_T, Literal[_Missing.Missing]]