-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Description
Unsafe overrides are pretty common when annotating legacy codebases. The recommended way to deal with this is to add a # type: ignore comment. This has a few major issues, though:
- It's not intuitive. Users often don't realize that they should use it in cases like this.
- Users frequently misunderstand
# type: ignoreand believe that it ignores the type annotation, even though it only ignores the error message. - Users are sometimes confused about the line on which the comment should be placed. Also, it may be unclear what to do if there is an existing comment on the line.
- Some users don't like to litter their code with magic comments.
- A
# type: ignorecan ignore unrelated errors that the user doesn't want to be ignored.
I propose to add a decorator that can be used to silence this error, and only this error. Example:
from mypy_extensions import unsafe_override
class Result: ...
class Thing:
@unsafe_override
def __eq__(self, other: object) -> Result: # Result is an unsafe return type
# ... omittedThis would have these benefits:
- There is less room for misunderstanding, since the decorator is explicit about what it's used for.
- It's easier to use, since it doesn't have to be on a specific line.
- It doesn't look as magical/messy as a magic comment.
- It only ignores this specific error condition, so it won't hide unrelated errors.
- It would be easy to Google documentation for the decorator when reading code. Documentation can be explicit about why and when this should be used and provide extended discussion.
m-aciek, llchan, exhuma, Evpok and noamsglFreonius and noamsgl