@overload declarations in stubs - required to be directly adjacent?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Currently, mypy requires @overload declarations in stubs to be directly adjacent, with no declarations (e.g. type vars) in between. However, I don't find that requirement in PEP 484 [1], nor in the typing module documentation [2].
[1] https://www.python.org/dev/peps/pep-0484/#function-method-overloading
[2] https://docs.python.org/3/library/typing.html#typing.overload
To Reproduce
Consider the following type stub:
import typing
@typing.overload
def foo() -> None: ...
K = typing.TypeVar('K')
@typing.overload
def foo(k: K) -> K: ...
Then run stubtest on it.
Expected Behavior
According to the documentation, I would expect this stub to be valid.
Actual Behavior
$ python -m mypy.stubtest teststub
error: failed mypy build.
teststub.pyi:4: error: Single overload definition, multiple required
teststub.pyi:8: error: Name 'foo' already defined on line 4
teststub.pyi:8: error: Single overload definition, multiple required
Pulling the type var declaration up fixes it:
import typing
K = typing.TypeVar('K')
@typing.overload
def foo() -> None: ...
@typing.overload
def foo(k: K) -> K: ...
Your Environment
Python 3.7.1 (anaconda) on Linux, latest master of mypy.
I also tested with mypy 0.790 on Windows, with the same result.
$ python --version
Python 3.7.1
$ python -m mypy --version
mypy 0.820+dev.497556f466dcda90d850b23e86c55ec4082be3f5
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 damit, den Fehler mit dem gezeigten teststub und python -m mypy.stubtest teststub zu reproduzieren. Verfolge, wie mypy benachbarte @typing.overload-Deklarationen validiert, und füge einen Regressionstest hinzu, der den dazwischenliegenden TypeVar abdeckt; fertig ist die Änderung, wenn der Stub akzeptiert wird und die bestehenden Overload-Diagnosen weiterhin korrekt sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100