|
15 | 15 | from unittest.mock import patch
|
16 | 16 | from test import ann_module, ann_module2, ann_module3
|
17 | 17 | import typing
|
18 |
| -from typing import TypeVar, Optional, Union, Any, AnyStr |
| 18 | +from typing import TypeVar, Optional, Union, AnyStr |
19 | 19 | from typing import T, KT, VT # Not in __all__.
|
20 | 20 | from typing import Tuple, List, Dict, Iterable, Iterator, Callable
|
21 | 21 | from typing import Generic
|
22 | 22 | from typing import no_type_check
|
23 | 23 | import typing_extensions
|
24 |
| -from typing_extensions import NoReturn, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict, Self |
| 24 | +from typing_extensions import NoReturn, Any, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict, Self |
25 | 25 | from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs, TypeGuard
|
26 | 26 | from typing_extensions import Awaitable, AsyncIterator, AsyncContextManager, Required, NotRequired
|
27 | 27 | from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, final, is_typeddict
|
@@ -160,6 +160,48 @@ def test_exception(self):
|
160 | 160 | assert_never(None)
|
161 | 161 |
|
162 | 162 |
|
| 163 | +class AnyTests(BaseTestCase): |
| 164 | + def test_can_subclass(self): |
| 165 | + class Mock(Any): pass |
| 166 | + self.assertTrue(issubclass(Mock, Any)) |
| 167 | + self.assertIsInstance(Mock(), Mock) |
| 168 | + |
| 169 | + class Something: pass |
| 170 | + self.assertFalse(issubclass(Something, Any)) |
| 171 | + self.assertNotIsInstance(Something(), Mock) |
| 172 | + |
| 173 | + class MockSomething(Something, Mock): pass |
| 174 | + self.assertTrue(issubclass(MockSomething, Any)) |
| 175 | + ms = MockSomething() |
| 176 | + self.assertIsInstance(ms, MockSomething) |
| 177 | + self.assertIsInstance(ms, Something) |
| 178 | + self.assertIsInstance(ms, Mock) |
| 179 | + |
| 180 | + class SubclassesAny(Any): |
| 181 | + ... |
| 182 | + |
| 183 | + def test_repr(self): |
| 184 | + if sys.version_info >= (3, 11): |
| 185 | + mod_name = 'typing' |
| 186 | + else: |
| 187 | + mod_name = 'typing_extensions' |
| 188 | + self.assertEqual(repr(Any), f"{mod_name}.Any") |
| 189 | + if sys.version_info < (3, 11): # skip for now on 3.11+ see python/cpython#95987 |
| 190 | + self.assertEqual(repr(self.SubclassesAny), "<class 'test_typing_extensions.AnyTests.SubclassesAny'>") |
| 191 | + |
| 192 | + def test_instantiation(self): |
| 193 | + with self.assertRaises(TypeError): |
| 194 | + Any() |
| 195 | + |
| 196 | + self.SubclassesAny() |
| 197 | + |
| 198 | + def test_isinstance(self): |
| 199 | + with self.assertRaises(TypeError): |
| 200 | + isinstance(object(), Any) |
| 201 | + |
| 202 | + isinstance(object(), self.SubclassesAny) |
| 203 | + |
| 204 | + |
163 | 205 | class ClassVarTests(BaseTestCase):
|
164 | 206 |
|
165 | 207 | def test_basics(self):
|
@@ -3018,7 +3060,7 @@ def test_typing_extensions_defers_when_possible(self):
|
3018 | 3060 | if sys.version_info < (3, 10):
|
3019 | 3061 | exclude |= {'get_args', 'get_origin'}
|
3020 | 3062 | if sys.version_info < (3, 11):
|
3021 |
| - exclude |= {'final', 'NamedTuple'} |
| 3063 | + exclude |= {'final', 'NamedTuple', 'Any'} |
3022 | 3064 | for item in typing_extensions.__all__:
|
3023 | 3065 | if item not in exclude and hasattr(typing, item):
|
3024 | 3066 | self.assertIs(
|
|
0 commit comments