Skip to content
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

Generalization of fixed-length tuples to variadic tuples in self-typed generic class constructors #16253

Open
insilications opened this issue Oct 12, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@insilications
Copy link

I understand that Mypy tends to be conservative with its type inferences. However, I'm unclear as to why it doesn't utilize the information from the type variables to infer the type as Signal[builtins.tuple[str, int]] (a fixed-length tuple) like Pyright does.
Instead, it infers it as Signal[builtins.tuple[_T_co1, ...]] (a variadic tuple). Why does it prefer this generalization?

from __future__ import annotations
from typing import TypeVar, Generic, reveal_type

T1_signal = TypeVar('T1_signal')


class Signal(Generic[T1_signal]):

    def __init__(self: Signal[T1_signal], __t1: type[T1_signal]) -> None:
        return
   
   
   
test = Signal(tuple[str, int])
reveal_type(test)  
# mypy: Signal[builtins.tuple[_T_co1, ...]]
# pyright: Signal[builtins.tuple[str, int]]

https://mypy-play.net/?mypy=latest&python=3.11&flags=new-type-inference%2Cstrict&gist=5ee508bb32ae745f6a4e41415d345c2b
https://pyright-playground.decorator-factory.su/?gzip=H4sIAK65J2UC_02PMQuDQAyF9_yKbCrYwVVo125dKl1EjsPm5ODMyV0s-O97iooZAkm-PN4zwY-olJllDqQU2nHyQVAze9FiPUcwKyLLZHk4zs0y0UeHEp_EFGxfYqAfaacSRgBNpaIdWDu8H2iencusAIDe6RjxvS3yXaU9ka6oATDVl0xyZ9mKUnkkZ-r958KWiZCqXi3SVQJvD3x5pnpTWitQSsnbeGlCUZLR3YvMk6M2SgpnWboCLsnyFS3gD1XOPu80AQAA

Expected Behavior

A more direct inference when it sees a fixed-tuple as a type argument, without generalizing for the variadic nature of tuple.

Your Environment

  • Mypy version used: master branch
  • Mypy command-line flags: --strict
  • Python version used: 3.11
@insilications insilications added the bug mypy got something wrong label Oct 12, 2023
@ilevkivskyi
Copy link
Member

Not 100% sure, but fixing this TODO https://github.com/python/mypy/blob/master/mypy/checkexpr.py#L4670-L4672 should do it.

I was looking at it recently, and was not sure if it actually worth it (since it is quite niche use case). Maybe this is a sign...

@insilications
Copy link
Author

insilications commented Oct 14, 2023

ilevkivskyi

Thanks for the response. Isn't this related to #9003 and the more general #9773 (so called new special form TypeForm[T])?
I had this in the back of my mind, but I didn't think Pyright was addressing this specific use case, instead of returning whatever the definition of tuple in builtins.pyi is (which is what I assume is happening with mypy here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants