-
-
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
Poor error message due to positional-or-keyword incompatibility #12013
Comments
Thanks for the report. Looks like Eric figured out what was going on in python/typing#1036 — the difference is that |
Ah sorry, should have recognised that that issue was also what was causing this. Thanks for updating! Should the label be updated as well? |
I made a simple diagnostic (ParamSpec-only and definitely improvable) because this behaviour + concatenate was confusing: #11847 (comment) However, something to wonder is if we should allow |
It's not just the positional-only issue. The original code is not type safe. from typing import Callable, Generator
class Parent:
def method_without(self) -> "Parent":
return Parent() # returning Parent here
def method_with(self, param: str, /) -> "Parent":
return Parent()
class Child(Parent):
method_without: Callable[["Child"], "Child"]
method_with: Callable[["Child", str], "Child"]
c = Child()
reveal_type(c.method_without()) # says it's a Child... |
@ktbarrett The return type seems type safe to me. I don't see a situation in which if you thought you were receiving a |
@hauntsaninja And if |
Maybe I'm being really dumb, but that seems fine to me too. |
c = Child()
d = c.method_without() # says it's a Child, it's actually a Parent
d.extra_method() # No error here, Child has extra_method |
But |
What method is it dispatching to at runtime? |
That's a different (and much broader) problem, where mypy doesn't check that declared instance attributes actually exist. You could get the same effect if the parent had |
Hmmm... Is it not reasonable to prevent users from defining overlapping and incompatible attributes in subclasses? If the superclass defined the attribute, the child class doesn't need to redefine it... it simply needs to make sure that it fills in an implementation. EDIT 1: I actually think there might not ever be a good reason to redefine an instance attribute without an initial value... |
IIUC, I think this is a simpler piece of code that illustrates this problem—I ran into this when trying to type 3rd party code that monkey-patches some stdlib functions: def foo(x: int) -> str | None:
return None
foo = lambda _: None # Incompatible types in assignment (expression has type "Callable[[int], None]", variable has type "Callable[[int], str | None]") [assignment] The error message seems rather confusing and doesn't even mention what's wrong—the named parameter Apparently there is some effort to be more helpful, because if I change
|
Bug Report
When using
Callable
to type a method instead of defining it (in my use case it is defined and assigned in another file) mypy doesn't seem to follow the same rules for child methods with and without parameters.I asked on gitter and was told this is probably a bug. I also searched and was unable to find another report for this, sorry if this is still a duplicate.
To Reproduce
Use the following code:
Expected Behavior
Not sure. Either both typings of the child methods should be rejected or both should be accepted.
Actual Behavior
test.py:14: error: Incompatible types in assignment (expression has type "Callable[[str], Child]", base class "Parent" defined the type as "Callable[[str], Parent]") [assignment]
Your Environment
--strict
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: