python / python/typing

Finals and Literals: Spec contradiction

Offen
#2,351 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: conformance tests topic: typing spec
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beschreibung

This came up in python/typeshed#16301. I assume this is mostly a clerical/historical error, but let me know if we should follow the whole spec change process.


The typing spec says about Final:

Example:

ID: Final = 1

The typechecker should apply its usual type inference mechanisms to determine the type of ID (here, likely, int).

And then a bit later:

Type checkers should treat uses of a final name that was initialized with a literal as if it was replaced by the literal.

Both sentences are copied directly from PEP 591, which overlapped with the Literals PEP 586.

I assume that we should change the first sentence to "here, likely, Literal[1]" and change the second sentence to be a bit more concrete along these lines:

Type checkers should infer uses of a final name that was initialized with a literal X as Literal[X] if X is an allowed literal type (cf. to the literals section). Otherwise, they should infer the type of the X.

(And maybe extend the example after a bit.)

In fact, this is what the conformance tests already check:

https://github.com/python/typing/blob/cf943ccbea5596ef969eec6e2260097ff697b7ba/conformance/tests/qualifiers_final_annotation.py#L11-L13

(Although the tests could be extended a bit.)

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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

Beginne damit, die Syntax von Final und die Formulierung zur Verwendung von Literalen in der typing specification mit PEP 591 und PEP 586 zu vergleichen. Überprüfe conformance/tests/qualifiers_final_annotation.py, insbesondere die Zeilen 11-13, und erweitere die relevanten Fälle bei Bedarf. Als erledigt gilt die Aufgabe, wenn die Spezifikation intern konsistent ist und die conformance tests das präzisierte Verhalten widerspiegeln.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation, testing-qa
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
72/100

Neue Issues direkt in Ihr Postfach

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