Decorators over `__init__()` make classes unconstructable until after the class is declared, even within functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beschreibung
Bug Report
It should be possible, within a function, to construct an instance of a class which is declared globally later in the module. Applying a decorator factory to a class's __init__() causes constructed instances to have type Any, but only before the class declaration[^1]. (A plain decorator doesn't trigger this issue.)
This probably explains #14519, and may be related to #11293.
To Reproduce
from __future__ import annotations
from typing import (
Any,
Callable,
TypeVar,
)
from typing_extensions import (
assert_type,
)
Tc = TypeVar('Tc', bound=Callable[..., Any])
def any_decorator_factory() -> Callable[[Tc], Tc]:
'''this one does nothing
'''
def inner(func: Tc) -> Tc:
return func
return inner
def function_pre() -> None:
'''this function is identical to 'function_post()',
but only this copy generates a mypy error
'''
instance = SomeClass()
assert_type(instance, SomeClass) # error: type is "Any", not "SomeClass"
class SomeClass:
@any_decorator_factory()
def __init__(self):
pass
def function_post() -> None:
'''this function is identical to 'function_pre()',
but only that copy generates a mypy error
'''
instance = SomeClass()
assert_type(instance, SomeClass) # no error
Expected Behavior
The tool should issue no error for function_pre(); it's just as valid as function_post().
Actual Behavior
Any construction of SomeClass before that type is declared (i.e., before the end of its body) has type Any instead of SomeClass.
Your Environment
- Mypy version used: master 2024-03-12, 1.9.0
- Mypy command-line flags: none necessary
- Mypy configuration options from
mypy.ini(and other config files): none necessary - Python version used: 3.8, 3.11, 3.12
[^1]: some functions within the class body are affected as well; I haven't narrowed down the exact point constructing the class starts to behave normally, but I can do upon request.
[Edited for clarity, and to simplify the reproduction case by removing vestigial code.]
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 bereitgestellten Reproducer und vergleiche function_pre() und function_post() im Zusammenhang mit SomeClass und dessen dekoriertem init. Verfolge, wie die Konstruktion vor der Klassendeklaration analysiert wird; fertig ist die Aufgabe, wenn beide assert_type-Prüfungen SomeClass statt Any erkennen und der Reproducer als Regressionstest erhalten bleibt.
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
- 45/100