@@ -46,64 +46,59 @@ def f():
4646y: Any = " not an Any" # error: [invalid-assignment]
4747```
4848
49- ## Subclass
49+ ## Subclasses if ` Any `
5050
5151The spec allows you to define subclasses of ` Any ` .
5252
53- ` Subclass ` has an unknown superclass, which might be ` int ` . The assignment to ` x ` should not be
53+ ` SubclassOfAny ` has an unknown superclass, which might be ` int ` . The assignment to ` x ` should not be
5454allowed, even when the unknown superclass is ` int ` . The assignment to ` y ` should be allowed, since
5555` Subclass ` might have ` int ` as a superclass, and is therefore assignable to ` int ` .
5656
5757``` py
5858from typing import Any
5959
60- class Subclass (Any ): ...
60+ class SubclassOfAny (Any ): ...
6161
62- reveal_type(Subclass .__mro__ ) # revealed: tuple[Literal[Subclass ], Any, Literal[object]]
62+ reveal_type(SubclassOfAny .__mro__ ) # revealed: tuple[Literal[SubclassOfAny ], Any, Literal[object]]
6363
64- x: Subclass = 1 # error: [invalid-assignment]
65- y: int = Subclass()
66-
67- def _ (s : Subclass):
68- reveal_type(s) # revealed: Subclass
64+ x: SubclassOfAny = 1 # error: [invalid-assignment]
65+ y: int = SubclassOfAny()
6966```
7067
71- ` Subclass ` should not be assignable to a final class though, because ` Subclass ` could not possibly
72- be a subclass of ` FinalClass ` :
68+ ` SubclassOfAny ` should not be assignable to a final class though, because ` SubclassOfAny ` could not
69+ possibly be a subclass of ` FinalClass ` :
7370
7471``` py
7572from typing import final
7673
7774@final
7875class FinalClass : ...
7976
80- f: FinalClass = Subclass() # error: [invalid-assignment]
81- ```
82-
83- A use case where this comes up is with mocking libraries, where the mock object should be assignable
84- to any type:
77+ f: FinalClass = SubclassOfAny() # error: [invalid-assignment]
8578
86- ``` py
87- from unittest.mock import MagicMock
79+ @final
80+ class OtherFinalClass : ...
8881
89- x: int = MagicMock()
82+ f: FinalClass | OtherFinalClass = SubclassOfAny() # error: [invalid-assignment]
9083```
9184
92- A subclass of ` Any ` can be assigned to a ` Callable ` and called.
85+ A subclass of ` Any ` can also be assigned to arbitrary ` Callable ` types:
9386
9487``` py
9588from typing import Callable, Any
9689
97- class MockCallable (Any ):
98- pass
99-
100- def takes_callable (f : Callable):
90+ def takes_callable1 (f : Callable):
10191 f()
10292
103- takes_callable(MockCallable())
93+ takes_callable1(SubclassOfAny())
94+
95+ def takes_callable2 (f : Callable[[int ], None ]):
96+ f(1 )
97+
98+ takes_callable2(SubclassOfAny())
10499```
105100
106- But ` Any ` cannot be assigned to ` Final ` or ` Literal `
101+ A subclass of ` Any ` cannot be assigned to literal types, since those can not be subclassed:
107102
108103``` py
109104from typing import Any, Literal
@@ -114,22 +109,15 @@ class MockAny(Any):
114109x: Literal[1 ] = MockAny() # error: [invalid-assignment]
115110```
116111
112+ A use case where subclasses of ` Any ` come up is in mocking libraries, where the mock object should
113+ be assignable to (almost) any type:
117114
118115``` py
119- from typing import final, Any
120-
121- @final
122- class FinalA : ...
123-
124- @final
125- class FinalB : ...
126-
127- class MockAny (Any ): ...
116+ from unittest.mock import MagicMock
128117
129- value: FinalA | FinalB = MockAny() # error: [invalid-assignment]
118+ x: int = MagicMock()
130119```
131120
132-
133121## Invalid
134122
135123` Any ` cannot be parameterized:
0 commit comments