-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Implementation of annotated abstractmethod is typed as Any #15669
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
Comments
A case that does cause mypy to complain is if the concrete implementation is fully annotated: class DateValidatorABC(metaclass=ABCMeta):
@abstractmethod
def validate(self, context, value: date) -> date:
...
class DateOfBirthValidator3(DateValidatorABC):
name = "date_of_birth"
def validate(self, context, value: date) -> bool:
return value.year > 1900 this gives:
...which is exactly what I was hoping for. I'm just using https://mypy-play.net/ with default options (I assume equivalent to mypy with no cli args), I wondered now if there was an option that would affect this. Enabling
|
Pyright implements type inference for parameters in subclasses based on annotations in parent classes, so it is consistent with the "expected behavior" you've requested above. Mypy maintainers have previously stated that they think parameter types should always be explicit, even in subclasses. I don't know if their position has changed since then. Perhaps one of them could clarify. |
@erictraut thanks I am also in favour of explicit parameter types, but it seems weird to allow a (possibly unintended) implicit Any in subclass to override an explicit typing in the parent 🤔 seems like mypy needs to be run with |
#15669 (comment) The same example without the Maybe the issue could be generalized to any method and not just abstract ones like: |
Bug Report
If you add type annotations to an
@abstractmethod
in an ABC then I would expect that implementations of that method in concrete classes would be type-checked to match the types on the abstract method.But mypy treats the implemented method as
Any -> Any
To Reproduce
I was originally doing this with generics in the base class:
But it turns out that the problem also occurs in the simpler non-generic case too:
Expected Behavior
I would expect mypy to infer, and require, the type of the implemented method to match the annotated type of the abstract method.
So there should be a type error from our implementations above as
value
isdate
but the method returnsbool
.Actual Behavior
Instead,
reveal_type
shows:https://mypy-play.net/?mypy=latest&python=3.11&gist=9fc2dca490527a088fe7bc81601f1666
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: