summaryrefslogtreecommitdiffstats
path: root/Doc/library/typing.rst
diff options
context:
space:
mode:
authorSteven Troxler <steven.troxler@gmail.com>2023-02-27 21:16:11 (GMT)
committerGitHub <noreply@github.com>2023-02-27 21:16:11 (GMT)
commit0f89acf6cc4d4790f7b7a82165d0a6e7e84e4b72 (patch)
treeb411d170426a326dc109e23e3cf0d42a6d770337 /Doc/library/typing.rst
parent4624987b296108c2dc1e6e3a24e65d2de7afd451 (diff)
downloadcpython-0f89acf6cc4d4790f7b7a82165d0a6e7e84e4b72.zip
cpython-0f89acf6cc4d4790f7b7a82165d0a6e7e84e4b72.tar.gz
cpython-0f89acf6cc4d4790f7b7a82165d0a6e7e84e4b72.tar.bz2
gh-101561: Add typing.override decorator (#101564)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Doc/library/typing.rst')
-rw-r--r--Doc/library/typing.rst38
1 files changed, 38 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index bbbf692..3395e4b 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -91,6 +91,8 @@ annotations. These include:
*Introducing* :data:`LiteralString`
* :pep:`681`: Data Class Transforms
*Introducing* the :func:`@dataclass_transform<dataclass_transform>` decorator
+* :pep:`698`: Adding an override decorator to typing
+ *Introducing* the :func:`@override<override>` decorator
.. _type-aliases:
@@ -2722,6 +2724,42 @@ Functions and decorators
This wraps the decorator with something that wraps the decorated
function in :func:`no_type_check`.
+
+.. decorator:: override
+
+ A decorator for methods that indicates to type checkers that this method
+ should override a method or attribute with the same name on a base class.
+ This helps prevent bugs that may occur when a base class is changed without
+ an equivalent change to a child class.
+
+ For example::
+
+ class Base:
+ def log_status(self)
+
+ class Sub(Base):
+ @override
+ def log_status(self) -> None: # Okay: overrides Base.log_status
+ ...
+
+ @override
+ def done(self) -> None: # Error reported by type checker
+ ...
+
+ There is no runtime checking of this property.
+
+ The decorator will set the ``__override__`` attribute to ``True`` on
+ the decorated object. Thus, a check like
+ ``if getattr(obj, "__override__", False)`` can be used at runtime to determine
+ whether an object ``obj`` has been marked as an override. If the decorated object
+ does not support setting attributes, the decorator returns the object unchanged
+ without raising an exception.
+
+ See :pep:`698` for more details.
+
+ .. versionadded:: 3.12
+
+
.. decorator:: type_check_only
Decorator to mark a class or function to be unavailable at runtime.