python / python/typeshed

Using typing.NoDefault instead of Ellipsis [3.13]

Offen
#12,102 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

ParamSpec, TypeVar, TypeVarTuple have a __default__ attribute, which is evaluated to NoDefault, when no default is provided in constructor :

from typing import ParamSpec, TypeVar, TypeVarTuple, Any

print(f'{TypeVar("Plop").__default__ = }')
...

# ❯ python3.13 /tmp/tmpe9xlgjg8.py
TypeVar("Plop").__default__ = typing.NoDefault
TypeVarTuple("Plop").__default__ = typing.NoDefault
ParamSpec("Plop").__default__ = typing.NoDefault

TypeVar("Plop", default=Any).__default__ = typing.Any
TypeVarTuple("Plop", default=Any).__default__ = typing.Any
ParamSpec("Plop", default=Any).__default__ = typing.Any

But this behaviour is not implemented in stubs, Ellipsis is used instead of typing.NoDefault which is already in stubs. I guess it could be fixed without breaking risks.

diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi
index f04b2d858..a84a517cc 100644
--- a/stdlib/typing.pyi
+++ b/stdlib/typing.pyi
@@ -161,7 +161,7 @@ class TypeVar:
             contravariant: bool = False,
             covariant: bool = False,
             infer_variance: bool = False,
-            default: Any = ...,
+            default: Any = NoDefault,
         ) -> None: ...
     elif sys.version_info >= (3, 12):
         def __init__(
@@ -231,7 +231,7 @@ if sys.version_info >= (3, 11):
             def __default__(self) -> Any: ...
             def has_default(self) -> bool: ...
         if sys.version_info >= (3, 13):
-            def __init__(self, name: str, *, default: Any = ...) -> None: ...
+            def __init__(self, name: str, *, default: Any = NoDefault) -> None: ...
         else:
             def __init__(self, name: str) -> None: ...
 
@@ -279,7 +279,7 @@ if sys.version_info >= (3, 10):
                 contravariant: bool = False,
                 covariant: bool = False,
                 infer_variance: bool = False,
-                default: Any = ...,
+                default: Any = NoDefault,
             ) -> None: ...
         elif sys.version_info >= (3, 12):
             def __init__(

Ps :

print(f'{TypeVar("Plop").__default__ is TypeVarTuple("Plop").__default__ is ParamSpec("Plop").__default__ = }')
❯ python3.13 /tmp/tmpe9xlgjg8.py
TypeVar("Plop").__default__ is TypeVarTuple("Plop").__default__ is ParamSpec("Plop").__default__ = True
TypeVar("Plop").__default__ is Ellipsis = False

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Überprüfe die Definitionen von TypeVar, TypeVarTuple und ParamSpec in stdlib/typing.pyi und konzentriere dich dabei auf ihre Standardparameter und den vorhandenen NoDefault-Stub. Aktualisiere die Signaturen für Python 3.13 so, dass ausgelassene Standardwerte typing.NoDefault entsprechen, und bestätige, dass der Stub sie nicht mehr als Ellipsis darstellt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.