a Type is not recognized properly for subclassing
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem in footypes.py gezeigten minimalen Reproduzierer und führe mypy dagegen aus, um die beiden Diagnosen für BaseAlias zu bestätigen. Verfolge, wie Variablenannotationen und Basisklassen analysiert werden, und füge anschließend eine Regressionstestabdeckung hinzu, die zeigt, dass das Beispiel keine Fehler erzeugt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100