(🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 25/100
Rechercherichtung
Beginnen Sie mit der Durchsicht der vorgeschlagenen Verwendung von StubOnlyBase und der verknüpften Diskussionen. Untersuchen Sie anschließend die genannten Stubs _collections_abc.pyi, typing.pyi und builtins.pyi. Bestimmen Sie die beabsichtigte Semantik des Decorators und sein versionsabhängiges Verhalten, bevor Sie vorschlagen, wie die Beispiele und zugehörigen Issues behandelt werden sollen; als abgeschlossen gilt die Aufgabe, wenn Einigkeit über das Design und seine Auswirkungen auf die betroffenen Stubs besteht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are not Generics, they define __class_getitem__) and some are still not subscribe-able but state they are in the stubs, for instance _collections_abc.dict_keys or any of the collections pre 3.9.
It could be used something like:
# _collections_abc.pyi
@StubOnlyBase(KeysView[_KT_co], Generic[_KT_co, _VT_co])
class dict_keys:
...
# typing.pyi
@StubOnlyBase(Generic[_T])
class Container:
if sys.version_info >= (3, 9):
def __class_getitem__(???) -> ???:
@StubOnlyBase(Collection[_KT], Generic[_KT, _VT_co])
class Mapping(Collection):
...
Mapping doesn't actually extend Generic, but Container defines __class_getitem__, so that would also need to be updated in the stubs.
This might help resolve https://github.com/python/mypy/issues/3186, but still indicate that it's not actually one of the bases.
# builtins.pyi
@StubOnlyBase(numbers.Integral)
class int:
...
I think a change like this would make the stubs much more understandable and closer to the actual implementation, also fixing issues about older python versions not working properly with the current stubs(https://github.com/python/mypy/issues/11529).
I personally find it very confusing when referencing the stubs that the bases are often false.
No idea how this would be used in a real life annotation, but a workaround that works is:
from typing import _alias # type: ignore[attr-defined]
from typing import TYPE_CHECKING
from _collections_abc import dict_keys
if not TYPE_CHECKING:
dict_keys = _alias(dict_keys, 2)
def foo(dk: dict_keys[str, int]) -> None: ...
https://github.com/python/typeshed/pull/6312#issuecomment-976036379
https://github.com/python/typeshed/issues/6257
Obligatory shill to #953, which I think would render this moot.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typing
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
OpenHands/extensions#626 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
CSCfi/sd-search-api#39 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
-
please add to porn list Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
StevenBlack/hosts#3255 ·