Using `classmethod` of generic alias of a generic type and `--disallow-any-generics`
Open
Nobody has claimed this yet.
topic-inference
topic-type-alias
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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]"
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example in the linked mypy-play case with --disallow-any-generics, then trace how generic aliases and classmethod calls are analyzed. Done means Alias.from_arg(1) no longer raises the type-arg error while its revealed type remains My[int].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100