a Type is not recognized properly for subclassing
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I'm explicitly typing a variable with Type (or type) and mypy thinks I should not subclass it.
To Reproduce
from typing import Type
class Base:
...
def get_base() -> Type[Base]:
return Base
BaseAlias: Type[Base] = get_base()
class Derived(BaseAlias):
...
Expected Behavior
There should be no error. I'm subclassing a type, which is ok. Furthermore, mypy has the information that it's a type so subclassing should not be questioned.
Actual Behavior
footypes.py:11: error: Variable "footypes.BaseAlias" is not valid as a type
footypes.py:11: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
footypes.py:11: error: Invalid base class "BaseAlias"
Found 2 errors in 1 file (checked 1 source file)
Same behavior if specifying BaseAlias: type = get_base() instead.
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.9.0
- Operating system and version:
python:3.9docker image
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 with the minimal reproducer shown in footypes.py and run mypy against it to confirm the two diagnostics for BaseAlias. Trace how variable annotations and base classes are analyzed, then add regression coverage showing that the example produces no errors.
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
- Mostly clear
- Newbie friendliness
- 42/100