Skip to content

Commit e50ba7e

Browse files
authored
Edits to discussion of unreachable code in the docs (python#8997)
Make it closer the style used elsewhere. It's also more concise now.
1 parent 9354d4a commit e50ba7e

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

docs/source/common_issues.rst

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -809,40 +809,46 @@ not necessary:
809809
def test(self, t: List[int]) -> Sequence[str]: # type: ignore[override]
810810
...
811811
812-
Unreachable code during typechecking
813-
------------------------------------
812+
Unreachable code
813+
----------------
814814

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:
818818

819819
.. code-block:: python
820820
821821
class Foo:
822-
bar:str = ''
822+
bar: str = ''
823823
824824
def bar() -> None:
825825
foo: Foo = Foo()
826826
return
827-
x:int = 'abc'
827+
x: int = 'abc' # Unreachable -- no error
828828
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:
831832

832833
.. code-block:: python
833834
834835
class Foo:
835-
bar:str = ''
836+
bar: str = ''
836837
837838
def bar() -> None:
838839
foo: Foo = Foo()
839840
assert foo.bar is None
840-
x:int = 'abc'
841+
x: int = 'abc' # Unreachable -- no error
841842
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:
846852

847853
.. code-block:: python
848854
@@ -853,10 +859,7 @@ also be demonstrated by the following:
853859
foo: Foo = Foo()
854860
if not foo.bar:
855861
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
859863
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy