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

Type alias over generic parameter in generic class produces an error #17956

Open
Dunes opened this issue Oct 15, 2024 · 2 comments
Open

Type alias over generic parameter in generic class produces an error #17956

Dunes opened this issue Oct 15, 2024 · 2 comments
Labels
bug mypy got something wrong topic-pep-695 Issues related to PEP 695 syntax

Comments

@Dunes
Copy link

Dunes commented Oct 15, 2024

Bug Report

Given a class context (eg. class class Foo[T]), mypy fails to infer the generic parameter when used as part of a type alias that is declared in the class context.

Using type aliases within a class can help avoid repetition when complex generics are used throughout the class.

To Reproduce

class X[Y]:
    type Z = list[Y] # produces: error: All type parameters should be declared ("Y" not declared)  [valid-type]
    
    def __init__(self, val: Y) -> None:
        self.val = val

    def make_list(self) -> Z:
        return [self.val]

reveal_type(X(123).make_list())  # Revealed type *expected* to be "builtins.list[builtins.int]"
                                 # is instead "builtins.list[Any]"

https://mypy-play.net/?mypy=latest&python=3.12&gist=845d9b12620d3febc625406e2a093ca2

Expected Behavior

The generic parameter Y should be inferred from the context of the class where it was specified.

It should be noted that pyright is able to deduce Y and therefore Z. See https://pyright-play.net/?code=MYGwhgzhAEAaDaBNAugLgFDS9ALgTwAcBTaALWgF5oQBLCHJZTbZrAEyIDNoB9HmgHY0cfABQQiITgBpoANzAhU0RAEpoAWgB80AHIB7AUQzZT0CVIB0CkJXmL0raB24BbMAGsiPWvXGTOdW0yEzMsACciHABXcIFoeAtOa0UmdEi5IkUefGJRWFEARgAmAGZVS3cvHzocUVVVIA

Actual Behavior

mytypes.py:2: error: All type parameters should be declared ("Y" not declared)  [valid-type]
mytypes.py:10: note: Revealed type is "builtins.list[Any]"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.12
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12
@Dunes Dunes added the bug mypy got something wrong label Oct 15, 2024
@hauntsaninja hauntsaninja added the topic-pep-695 Issues related to PEP 695 syntax label Oct 15, 2024
@ilevkivskyi
Copy link
Member

Hm, FWIW such use case is explicitly prohibited for old-style type alias syntax (because in old syntax the intent was ambiguous). With new-style syntax this may be OK. In the meantime you can use an easy workaround:

class Gen[T]:
    type Alias[S] = Some[Complex[Type[Gen[S]]]]
    var: Alias[T]  # OK

cc @JukkaL

@Dunes
Copy link
Author

Dunes commented Oct 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-695 Issues related to PEP 695 syntax
Projects
None yet
Development

No branches or pull requests

3 participants