summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>2022-04-05 02:35:29 (GMT)
committerGitHub <noreply@github.com>2022-04-05 02:35:29 (GMT)
commit5a4973e29f2f5c4ee8c086f40325786c62381540 (patch)
tree299614c1035acc20242c9e73156b70b40c3cc200 /Lib/typing.py
parentbb86d1d9fbd1888524e04475383f4ea764277f67 (diff)
downloadcpython-5a4973e29f2f5c4ee8c086f40325786c62381540.zip
cpython-5a4973e29f2f5c4ee8c086f40325786c62381540.tar.gz
cpython-5a4973e29f2f5c4ee8c086f40325786c62381540.tar.bz2
bpo-46998: Allow subclassing Any at runtime (GH-31841)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 36f9ece..4636798 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -429,8 +429,17 @@ class _LiteralSpecialForm(_SpecialForm, _root=True):
return self._getitem(self, *parameters)
-@_SpecialForm
-def Any(self, parameters):
+class _AnyMeta(type):
+ def __instancecheck__(self, obj):
+ if self is Any:
+ raise TypeError("typing.Any cannot be used with isinstance()")
+ return super().__instancecheck__(obj)
+
+ def __repr__(self):
+ return "typing.Any"
+
+
+class Any(metaclass=_AnyMeta):
"""Special type indicating an unconstrained type.
- Any is compatible with every type.
@@ -439,9 +448,13 @@ def Any(self, parameters):
Note that all the above statements are true from the point of view of
static type checkers. At runtime, Any should not be used with instance
- or class checks.
+ checks.
"""
- raise TypeError(f"{self} is not subscriptable")
+ def __new__(cls, *args, **kwargs):
+ if cls is Any:
+ raise TypeError("Any cannot be instantiated")
+ return super().__new__(cls, *args, **kwargs)
+
@_SpecialForm
def NoReturn(self, parameters):