aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Automat/py3/automat/_runtimeproto.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-09-02 00:01:09 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-09-02 00:09:17 +0300
commitb5c4ec42ac2cc59dc3b104277ce2e85f5f77c88e (patch)
tree9b37605b78a3d2398da4addca3ee37899621c38e /contrib/python/Automat/py3/automat/_runtimeproto.py
parent88fb7b5c334c3afdffacd104ee20efda4400d1bc (diff)
downloadydb-b5c4ec42ac2cc59dc3b104277ce2e85f5f77c88e.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python/Automat/py3/automat/_runtimeproto.py')
-rw-r--r--contrib/python/Automat/py3/automat/_runtimeproto.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/contrib/python/Automat/py3/automat/_runtimeproto.py b/contrib/python/Automat/py3/automat/_runtimeproto.py
new file mode 100644
index 00000000000..c9c7409f5ab
--- /dev/null
+++ b/contrib/python/Automat/py3/automat/_runtimeproto.py
@@ -0,0 +1,62 @@
+"""
+Workaround for U{the lack of TypeForm
+<https://github.com/python/mypy/issues/9773>}.
+"""
+
+from __future__ import annotations
+
+import sys
+
+from typing import TYPE_CHECKING, Callable, Protocol, TypeVar
+
+from inspect import signature, Signature
+
+T = TypeVar("T")
+
+ProtocolAtRuntime = Callable[[], T]
+
+
+def runtime_name(x: ProtocolAtRuntime[T]) -> str:
+ return x.__name__
+
+
+from inspect import getmembers, isfunction
+
+emptyProtocolMethods: frozenset[str]
+if not TYPE_CHECKING:
+ emptyProtocolMethods = frozenset(
+ name
+ for name, each in getmembers(type("Example", tuple([Protocol]), {}), isfunction)
+ )
+
+
+def actuallyDefinedProtocolMethods(protocol: object) -> frozenset[str]:
+ """
+ Attempt to ignore implementation details, and get all the methods that the
+ protocol actually defines.
+
+ that includes locally defined methods and also those defined in inherited
+ superclasses.
+ """
+ return (
+ frozenset(name for name, each in getmembers(protocol, isfunction))
+ - emptyProtocolMethods
+ )
+
+
+def _fixAnnotation(method: Callable[..., object], it: object, ann: str) -> None:
+ annotation = getattr(it, ann)
+ if isinstance(annotation, str):
+ setattr(it, ann, eval(annotation, method.__globals__))
+
+
+def _liveSignature(method: Callable[..., object]) -> Signature:
+ """
+ Get a signature with evaluated annotations.
+ """
+ # TODO: could this be replaced with get_type_hints?
+ result = signature(method)
+ for param in result.parameters.values():
+ _fixAnnotation(method, param, "_annotation")
+ _fixAnnotation(method, result, "_return_annotation")
+ return result