-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Variance bug on TypeVar with value restriction #11379
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
Comments
Thanks, @JelleZijlstra! I will take a look 🙂 Related #11378 |
I think the problem is in this line: When defining a type variable with exact values, we want to match these values by type exactly. So, Related typing-sig discussion: https://mail.python.org/archives/list/[email protected]/thread/BQYZOF3DSCO3V5HHTWVSPK2PRUQPDIVL/ |
A constrained TypeVar doesn't require an exact match. The following is perfectly legit: T = TypeVar("T", int, str)
def func(a: T) -> T:
return a
func(True) The variance of a TypeVar applies only when it is used as a type parameter in a generic class, such as |
I think mypy is doing the right thing here. It shouldn't emit an error. Jelle references an error emitted by pyright, but that was due to a bug that I have since fixed. Pyright now generates no errors for the above code, so it's in sync with mypy. I think this can be closed. |
https://mypy-play.net/?mypy=latest&python=3.10&gist=5a4f16dd13bd4b18a7c2323ba464f627
Mypy allows this without errors, but pyright shows an error on the last line and I think it's right: T2 is invariant, so we shouldn't allow
bool
there:The text was updated successfully, but these errors were encountered: