Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typing spec chapter focused on exception behavior #1718

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/spec/exceptions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Exceptions
==========

Some type checking behaviors, such as type narrowing and reachability analysis,
erictraut marked this conversation as resolved.
Show resolved Hide resolved
require a type checker to understand code flow. Code flow normally proceeds
from one statement to the next, but some statements such as ``for``, ``while``
and ``return`` can change the code flow. Similarly, ``try``/``except``/``finally``
statements affect code flow and therefore can affect type evaluation. For example::

x = None
try:
some_function()
x = 1
except NotImplementedError:
pass

# The type of `x` at this point could be None if `some_function` raises
# an exception or `Literal[1]` if it doesn't, so a type checker may
# choose to narrow its type based on this analysis.
reveal_type(x) # Literal[1] | None


Context Managers
----------------

A context manager may optionally "suppress" exceptions by returning ``True``
(or some other truthy value) from its ``__exit__`` method. When such a context
manager is used, any exceptions that are raised and otherwise uncaught within
the ``with`` block are caught by the context manager, and control continues
immediately after the ``with`` block. If a context manager does not suppress
exceptions (as is typically the case), any exceptions that are raised and
otherwise uncaught within the ``with`` block propagate beyond the ``with``
block.

Type checkers that employ code flow analysis must be able to distinguish
between these two cases. This is done by examining the return type
annotation of the ``__exit__`` method of the context manager.

If the return type of the ``__exit__`` method is specifically ``bool`` or
``Literal[True]``, a type checker should assume that exceptions *can be*
suppressed. For any other return type, a type checker should assume that
exceptions *are not* suppressed. Examples include: ``Any``, ``Literal[False]``,
``None``, and ``bool | None``.

This convention was chosen because most context managers do not suppress
exceptions, and it is common for their ``__exit__`` method to be annotated as
returning ``bool | None``. Context managers that suppress exceptions are
relatively rare, so they are considered a special case.

For example, the following context manager suppresses exceptions::

class Suppress:
def __enter__(self) -> None:
pass

def __exit__(self, exc_type, exc_value, traceback) -> bool:
return True

with Suppress():
raise ValueError("This exception is suppressed")

# The exception is suppressed, so this line is reachable.
print("Code is reachable")
1 change: 1 addition & 0 deletions docs/spec/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Specification for the Python type system
callables
constructors
overload
exceptions
dataclasses
typeddict
tuples
Expand Down