Generic Type[T] function argument rejected as base class
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Since https://github.com/python/mypy/issues/5865 was fixed, the following code now type checks:
from typing import Any, Type
def f(typ: Type[Any]) -> Type[Any]:
class C(typ):
pass
return C
However if we make the superclass generic, it doesn't type check anymore which I would expect it to do.
To Reproduce
from typing import Type, TypeVar
T = TypeVar("T")
def f(typ: Type[T]) -> Type[T]:
class C(typ):
pass
return C
Actual Behavior
mypy_return_type.py:7: error: Variable "typ" is not valid as a type [valid-type]
mypy_return_type.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
mypy_return_type.py:7: error: Invalid base class "typ" [misc]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
1.0.0+dev.e9f5858c44b61c2d02819940debe3c31d099f9d5 - Mypy command-line flags:
mypy mypy_return_type.py - Mypy configuration options from
mypy.ini(and other config files): None - Python version used:
3.10.9
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
Reproduce the report with the provided generic Type[T] example using mypy mypy_return_type.py, then trace the type-checking path for variable expressions used as base classes and compare it with the non-generic Type[Any] case. Done means the generic example type-checks without the valid-type and Invalid base class errors while retaining its Type[T] return type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100