Skip to content

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

Open
@sobolevn

Description

@sobolevn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-inferenceWhen to infer types or require explicit annotationstopic-type-aliasTypeAlias and other type alias issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions