-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
mypy doesn't infer **kwargs types from superclass #10079
Comments
Currently I think this is officially not supported, but I do agree it would be a nice feature to have (type checking any kwargs passthrough, not just for super() calls but anywhere they're used) |
Curious, has there been a principled decision on whether (and when) mypy should infer types? |
I think the principle has been to never infer types for callables. This is the boundary used to achieve gradual typing and also this helps keep performance good. |
re: gradual typing, I assume the interest here would not be in inferring an untyped function, but rather when given a fat hint, e.g. -def spam(**kwargs):
+def spam(**kwargs: Any) -> None:
ham(**kwargs) or something that doesn't exist a'la -def spam(**kwargs):
+def spam(**kwargs: typing.InferMe) -> None:
ham(**kwargs) Would performance (needing to analyze function's body?) be the deterrent then? |
I wouldn't add inference if a user explicitly provides a hint, but something like the second would be interesting, or giving mypy a mode where it doesn't care about gradual typing and does its best to infer anything it can. But yeah, for the general case performance and implementation complexity are the deterrents. For OP's specific case, some type checkers do use a limited amount of inference on subclasses based on superclass types, e.g. I think if your signature is exactly the same, just missing types, pyright will use superclass types. Another thing that could be useful is a "typeof" kind of operator, so you could also imagine |
Bug Report
If a subclass
__init__
passes**kwargs
to it's superclass, the superclasses parameter types should be used to validate the subclass arguments.To Reproduce
Run mypy on this file:
Expected Behavior
Something like
error: Argument "b" to "Baz" has incompatible type "int"; expected "str"
Actual Behavior
mypy found no errors
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: