Enforce PEP 698 (override decorator) #131
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PEP 698 has introduced a new
typing.override
decorator, and suggested that type-checkers add an option to enable strictly enforcing the usage of this decorator for any method override in all classes.This decorator does 3 things for us:
Foo
, that defines some methodmy_method
, and a classBar
, which inherits fromFoo
, and overrides this method with some other custom implementation. What if the parentFoo
class suddenly renamed themy_method
method tomy_cool_method
. Once this happened, theBar
class would no longer be overriding themy_method
, but rather defining it as a completely new method, and inheriting the renamedmy_cool_method
fromFoo
. Whattyping.override
does is ensure that methods marked with it are in fact overrides, so in this scenario, ifBar.my_method
was marked as override, there would be a typing error, informing you thatmy_method
is not actually inherited from anywhere, and so the override is invalid.typing.override
will inherit the original docstring, unless another one is explicitly set. This allows us to reduce the repetition of docstrings fairly significantly. This is even recognized by ruff's flake8-docstrings, so no ugly noqas everywhere.While the 1st point may be nice, it's not hugely important, however the 2nd point this feature brings could be pretty nice to have enforced, and the 3rd point is also quite neat.
This PR was originally only made to see how enforcing this rule would look in the code-base, and I wasn't really sure whether or not to actually enable this enforcement. However, right now, I feel pretty strongly that enabling this is a good idea, and so, the
do-not-merge
tag from this PR was removed.