-
-
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
TypeVars can't be assigned to other TypeVars #6541
Comments
The first example is not a bug but expected behavior. Also inability to specify a default for a generic argument is a known issue, see #3737 In your case however, mypy is correct, there is no way it can infer @overload
def format(x: T) -> T: ...
@overload
def format(x: T, formatter: Callable[[T], S]) -> S: ...
def format(x, formatter = lambda y: y):
# your implementation here
... |
@ilevkivskyi would you mind expanding on why the first example is expected behavior? If there exists an assignment to the variables such that the code type checks, shouldn't the code be valid? If such an assignment leaves free variables, then the expression remains generic. Both assignments
the error could either be with the argument Given that my first example is expected behavior, I can understand why my third example wouldn't work (even if #3737 was fixed). However, if something like my first example did type check (and the bug was fixed), then what would be the difference between the second and third example? One way to resolve
This works with the current state of mypy because the type of However, if we allow for type variables to be assigned to other type variables, then the resolution for the single argument
This resolution could have happened in the opposite order and resulted in the type |
Default values, and moreover function bodies are not part of the function API. Function type signatures form rigid interfaces between different namespaces and are not subject to inference. In your first example def format(x: T, formatter: Callable[[T], S] = ...) -> S: ... You can't infer |
Interesting, that makes sense and definitely explains the expected behavior. Seems like supporting the feature I was requesting would require a fundamental rewrite of mypy's internals. Thanks for the informative and quick replies |
I've found a few cases of mypy throwing errors saying that two TypeVars are incompatible instead of simply assigning one TypeVar to another. Here's the simplest example:
mypy says:
The error doesn't seem to appear if any of the TypeVars gets assigned a concrete type. mypy has no issues with this file:
However, if I make the lambda a default value:
mypy gives this error
I suspect this is related to #4814
The text was updated successfully, but these errors were encountered: