-
-
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
Upper bound is missing from generic function signature in error report #17792
Comments
For reference, the reproducer from my mypy playground link above is: from collections.abc import Callable
from random import choice
from typing import Any, TypeVar
if choice((True, False)):
F = TypeVar('F')
def f(a: F) -> F:
return a
else:
def f[F_: Callable[..., Any]](a: F_) -> F_:
return a which currently outputs
The issue is that the bound for The root cause seems to be this part of Lines 2943 to 2949 in a646f33
The relevant bit is the A quick fix would be to simply add a check for if isinstance(tvar, TypeVarType):
upper_bound = get_proper_type(tvar.upper_bound)
if (
isinstance(upper_bound, Instance)
and upper_bound.type.fullname != "builtins.object"
- ):
+ ) or isinstance(upper_bound, CallableType):
tvars.append(f"{tvar.name}: {format_type_bare(upper_bound, options)}") which would change the output to
However, this wouldn't be a complete fix, since there are other |
Bug Report
The provided test defines the generic function
f()
in two ways which are incompatible with each other – one has an upper bound, and the other has not.However, when reporting this incompatibility,
mypy
prints two signatures that look equivalent because the upper bound is not mentioned in one of them.This makes it very difficult to understand why
mypy
reports an error and how to fix it. Moreover, the error report looks like a bug inmypy
.To Reproduce
https://mypy-play.net/?mypy=1.11.2&python=3.12&enable-incomplete-feature=NewGenericSyntax&gist=06224b5d473a6be86cae73f00e4643f1
The text was updated successfully, but these errors were encountered: