-
-
Notifications
You must be signed in to change notification settings - Fork 119
Add @typing.override #78
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
Changes from all commits
86b933a
62918a2
9b21216
116785c
c6a73ea
a540e16
ef932b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -62,6 +62,7 @@ | |||||||||
'Literal', | ||||||||||
'NewType', | ||||||||||
'overload', | ||||||||||
'override', | ||||||||||
'Protocol', | ||||||||||
'reveal_type', | ||||||||||
'runtime', | ||||||||||
|
@@ -2078,6 +2079,36 @@ def decorator(cls_or_fn): | |||||||||
return decorator | ||||||||||
|
||||||||||
|
||||||||||
if hasattr(typing, "override"): | ||||||||||
override = typing.override | ||||||||||
else: | ||||||||||
_F = typing.TypeVar("_F", bound=typing.Callable[..., typing.Any]) | ||||||||||
|
||||||||||
def override(__arg: _F) -> _F: | ||||||||||
Comment on lines
+2085
to
+2087
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just asking, my understanding was that types should not be added here and only provided via typeshed. I can't find a reference for it unfortunately. Did I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is true that type checkers will not look at this code. But types have value as documentation and perhaps some niche value for dynamic introspection. |
||||||||||
"""Indicate that a method is intended to override a method in a base class. | ||||||||||
|
||||||||||
Usage: | ||||||||||
|
||||||||||
class Base: | ||||||||||
def method(self) -> None: ... | ||||||||||
pass | ||||||||||
|
||||||||||
class Child(Base): | ||||||||||
@override | ||||||||||
def method(self) -> None: | ||||||||||
super().method() | ||||||||||
|
||||||||||
When this decorator is applied to a method, the type checker will | ||||||||||
validate that it overrides a method 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. | ||||||||||
Comment on lines
+2103
to
+2104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the current more general wording. For example, the base class method could be removed instead of renamed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, good point. Ignore this then :) |
||||||||||
|
||||||||||
See PEP 698 for details. | ||||||||||
|
||||||||||
""" | ||||||||||
return __arg | ||||||||||
|
||||||||||
|
||||||||||
# We have to do some monkey patching to deal with the dual nature of | ||||||||||
# Unpack/TypeVarTuple: | ||||||||||
# - We want Unpack to be a kind of TypeVar so it gets accepted in | ||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.