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

Using classmethod of generic alias of a generic type and --disallow-any-generics #16982

Open
sobolevn opened this issue Mar 4, 2024 · 0 comments
Labels
topic-inference When to infer types or require explicit annotations topic-type-alias TypeAlias and other type alias issues

Comments

@sobolevn
Copy link
Member

sobolevn commented Mar 4, 2024

Code:

from typing import TypeVar, Generic, reveal_type

T = TypeVar('T')
N = TypeVar('N')

class My(Generic[T]):
    def __init__(self, arg: T) -> None:
        self.arg = arg
    @classmethod
    def from_arg(cls, arg: N) -> 'My[N]':
        return My(arg)
        

Alias = My[T]
        
reveal_type(My.from_arg(1)) 
# note: Revealed type is "__main__.My[builtins.int]"

reveal_type(Alias.from_arg(1))
# error: Missing type parameters for generic type "Alias"  [type-arg]
# note: Revealed type is "__main__.My[builtins.int]"

Link: https://mypy-play.net/?mypy=latest&python=3.12&flags=disallow-any-generics&gist=f657946226b54d0654952c6bcdb02a61

Problem: I think that [type-arg] error should not be raised in this case, because no type param is actually needed in this case. classmethod is often used as a constructor, duplicating type vars is verbose and unneeded.

@sobolevn sobolevn added topic-type-alias TypeAlias and other type alias issues topic-inference When to infer types or require explicit annotations labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-inference When to infer types or require explicit annotations topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

No branches or pull requests

1 participant