-
-
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
untyped defs on derived classes #3903
Comments
I believe we probably don't want to support this unless |
I think this should be a separate, new flag. Then it sounds worth
considering.
|
+1 from me for a separate flag. |
Also for reference, a very similar discussion happened in the typing tracker, see python/typing#269. |
This can't be enabled by default, since a method override can have a different signature from the base class, and blindly inheriting the signature can generate false positives. A separate flag is worth considering, since similar things have been proposed several times. |
Thinking more about it, I suppose an override should require a new type signature, as the method body is not inherited with an override, thus it is likely a signature not to be. So Im not sure I think this is such a good feature after all. If you are overriding, it is a different method body, thus inheriting the type signature seems wrong (at least to me). However, if we do support this, I think a flag is too global, as these changes are likely to be only wanted on a single class or method even, but that is my personal experience on this. |
You could inherit the type signature (which I do think is the common case
for method overrides) but still check the new body against that signature.
|
Yes, I think that this only makes sense if we check the body against the inherited signature. This is another reason why this needs to be opt-in -- unannotated methods are not type checked according to PEP 484. |
+1 this will be relay nice. Please add this for class variables too. |
Whats real bad is that previous activity in this tread was about a ear ago, and nothing go on( |
Okay, here's a more detailed spec of how this could work in case somebody wishes to contribute an implementation:
|
May I ask how is the progress on this proposal? I was just about to add new issue duplicationg this one when I found it, here's a code that I wanted to include to mine:
Need to duplicate all signatures in derived classes feels very unhandy to me, for two reasons:
On the other hand, I see that sometimes we would override the method dynamically and don't want the type checker to be concerned about it. Would it better option that instead of passing argument to mypy ( |
I am currently using PyCharm for editing Python code, and it has a nice feature: copy signature from superclass. It works well if you first defined a method (with all types) in the superclass and then want to override it in a subclass. |
Ok, this is easy to use and worth knowing (besides that actually I am using vscode 😂), but it doesn't address my primary concern that this kind of error (which by the way seems a severe violation of Liskov substitution principle to me) would pass undetected by mypy type checker till runtime fail. |
After considering it for a while, I am pretty sure that mypy should generate at least some warning in this case (for example "overriden method return type doesn't conform to base class method signature") |
Mypy generally doesn't find errors caused by unannotated code, so unannotated method overrides aren't special. There are strictness options to enforce the presence function annotations ( |
Ok, I generally see that the best solution is to annotate as much as possible (in addition, this is also better for reading code, as I don't have to search for the base class to find method signature). But still, this would be nice to have a choince - annotate or inherit signatures. So, when can I expect this feature to be introduced? Is it being implemented at the moment? |
We don't know when/if this will be available. |
Is this really a desirable feature for a type checker to have, or is it rather something that would make much more sense as a code completion feature? The PyCharm feature mentioned by @ilevkivskyi might not cover all use cases, but in the use cases where it works, it seems the superior option. So perhaps instead of a feature request for mypy, this should be a feature request for IDE developers? Some thoughts:
|
parent class signature for methods which are overriden in the subclass. This means we sadly need to rely on annotations from the base class. See python/mypy#3903 for details.
Any news on this? I get it that it's not desirable to enforce this globally. Maybe a compromise would be to only enforce this when inheriting from abstract classes that are likely to have type hinting. |
Is this just waiting for a contributor? Pyright and pylance seems to already have it supported as discussed in microsoft/pyright#3094 with documentation here Can some maintainer give green light so others (or maybe me) can step in? Would be great |
Currently all arguments and return values on untyped defs are set to
Any
.It would be nice, if arguments and return values on derived classes would take the type annotations from the parent class. For example:
In this example the revealed type is
Any
but I would expect it to bestr
.Of cause, if the entire project has type annotations, than this would not be an issue, but when you add type annotations on a big project and start with the base classes, it would be nice, if you could benefit from that.
The text was updated successfully, but these errors were encountered: