File tree 1 file changed +8
-2
lines changed 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ When you're puzzled or when things are complicated
106
106
107
107
.. code-block :: python
108
108
109
- from typing import Union, Any
109
+ from typing import Union, Any, cast
110
110
111
111
# To find out what type mypy infers for an expression anywhere in
112
112
# your program, wrap it in reveal_type. Mypy will print an error
@@ -134,7 +134,13 @@ When you're puzzled or when things are complicated
134
134
# (in mypy, typeshed, or your own code) or an explanation of the issue.
135
135
x = confusing_function() # type: ignore # https://github.com/python/mypy/issues/1167
136
136
137
- # TODO : explain cast
137
+ # cast is a helper function for mypy that allows for guidance of how to convert types.
138
+ # it does not cast at runtime
139
+ a = [4 ]
140
+ b = cast(List[int ], a) # passes fine
141
+ c = cast(List[str ], a) # passes fine (no runtime check)
142
+ reveal_type(c) # -> error: Revealed type is 'builtins.list[builtins.str]'
143
+ print (c) # -> [4] the object is not cast
138
144
139
145
# TODO : explain "Need type annotation for variable" when
140
146
# initializing with None or an empty container
You can’t perform that action at this time.
0 commit comments