@@ -809,40 +809,46 @@ not necessary:
809
809
def test (self , t : List[int ]) -> Sequence[str ]: # type: ignore [ override ]
810
810
...
811
811
812
- Unreachable code during typechecking
813
- ------------------------------------
812
+ Unreachable code
813
+ ----------------
814
814
815
- Sometimes a part of the code can become unreachable, even if not immediately obvious.
816
- It is important to note that in such cases, that part of the code will *not * be type-checked
817
- by mypy anymore . Consider the following code snippet :
815
+ Mypy may consider some code as * unreachable * , even if it might not be
816
+ immediately obvious why. It's important to note that mypy will *not *
817
+ type check such code . Consider this example :
818
818
819
819
.. code-block :: python
820
820
821
821
class Foo :
822
- bar:str = ' '
822
+ bar: str = ' '
823
823
824
824
def bar () -> None :
825
825
foo: Foo = Foo()
826
826
return
827
- x:int = ' abc'
827
+ x: int = ' abc' # Unreachable -- no error
828
828
829
- It is trivial to notice that any statement after ``return `` is unreachable and hence mypy will
830
- not complain about the mis-typed code below it. For a more subtle example, consider:
829
+ It's easy to see that any statement after ``return `` is unreachable,
830
+ and hence mypy will not complain about the mis-typed code below
831
+ it. For a more subtle example, consider this code:
831
832
832
833
.. code-block :: python
833
834
834
835
class Foo :
835
- bar:str = ' '
836
+ bar: str = ' '
836
837
837
838
def bar () -> None :
838
839
foo: Foo = Foo()
839
840
assert foo.bar is None
840
- x:int = ' abc'
841
+ x: int = ' abc' # Unreachable -- no error
841
842
842
- Again, mypy will not throw any errors because the type of ``foo.bar `` says it's ``str `` and never ``None ``.
843
- Hence the ``assert `` statement will always fail and the statement below will never be executed.
844
- Note that in Python, ``None `` is not a null-reference but an object of type ``NoneType ``. This can
845
- also be demonstrated by the following:
843
+ Again, mypy will not report any errors. The type of ``foo.bar `` is
844
+ ``str ``, and mypy reasons that it can never be ``None ``. Hence the
845
+ ``assert `` statement will always fail and the statement below will
846
+ never be executed. (Note that in Python, ``None `` is not an empty
847
+ reference but an object of type ``None ``.)
848
+
849
+ In this example mypy will go on to check the last line and report an
850
+ error, since mypy thinks that the condition could be either True or
851
+ False:
846
852
847
853
.. code-block :: python
848
854
@@ -853,10 +859,7 @@ also be demonstrated by the following:
853
859
foo: Foo = Foo()
854
860
if not foo.bar:
855
861
return
856
- x:int = ' abc'
857
-
858
- Here mypy will go on to check the last line as well and report ``Incompatible types in assignment ``.
862
+ x: int = ' abc' # Reachable -- error
859
863
860
- If we want to let mypy warn us of such unreachable code blocks, we can use the ``--warn-unreachable ``
861
- option. With this mypy will throw ``Statement is unreachable `` error along with the line number from
862
- where the unreachable block starts.
864
+ If you use the :option: `--warn-unreachable <mypy --warn-unreachable> ` flag, mypy will generate
865
+ an error about each unreachable code block.
0 commit comments