-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
--warn-return-any should not complain on NotImplemented when using abc #4534
Comments
Why do you think this is a standard pattern? I always thought that @abc.abstractmethod
def encode(self) -> bytes:
raise NotImplementedError("Not a valid encoder -- implement `encode` before use") and I hope mypy doesn't complain about this (if yes, then this should be fixed). |
You have a point there. I wonder if the correct type for `NotImplemented`
shouldn't be `object` instead of `Any`. Can you try this (it's a one-line
change in typeshed) and submit a PR?
|
@gvanrossum It is not possible because |
A simplest example is def __eq__(self, other: object) -> bool:
if isinstance(other, type(self)):
...
return NotImplemented will be flagged as an error. |
Right. Maybe |
Yes, this is easy, but would not be my priority TBH, if OP wants to make a PR then OK. |
@ilevkivskyi I've reread the docs and you are right, I shouldn't use Strangely abc docs doesn't say anything about what should a body of an abstract method look like. PEP 3119 doesn't either (and has an example with I guess I should raise an exception here, thank you. |
I made `_get_matching_entity_no_optional` as a workaround to enable `get_matching_entities` to construct its entity views without having to check for `None` every time-- I'm not even sure *how* it would do that, considering the structure of the dict comprehension. So that means `get_matching_entity` no longer takes `_checked` as a keyword argument because the code for checked entities has been delegated to `_get_matching_entity_no_optional`. I also made a bunch of other changes: - Various type annotation changes to accomodate the complaints of the type checker (mypy in this case) - Changed `Aspect.__eq__`s type annotation and implementation-- Turns out that I should return `NotImplemented` if the object doesn't support operations (==, >, etc.) with its input. BUT mypy complains when it sees that `NotImplemented` is able to be returned, rather than a value of type `bool`. So I made a hack to trick mypy into thinking that the operator (method) doesn't return `NotImplemented`, but actually does so at runtime-- though it seems that the mypy team recognises this and are working on a fix/whatever. Issue Reference: - python/mypy#4534
I'm using
abc.abstractmethod
:And I get a warning:
warning: Returning Any from function declared to return "bytes"
I don't think something is wrong with the code, it's totally normal way to declare and describe an interface and mypy shouldn't complain about it.
The text was updated successfully, but these errors were encountered: