python / python/mypy

Self is not rejected when used as a PEP 695 type parameter bound

Offen
#21,960 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-self-types
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Self is only valid in the specific locations listed in PEP 673 and Mypy already rejects several invalid locations.
See: https://github.com/python/mypy/blob/ae39cdb2fba8908eeff6ec8d6b88879c62495fcc/test-data/unit/check-selftype.test#L1480

It seems one location wasn't covered: using Self as the bound (or constraint) of a PEP 695 type parameter.

To Reproduce:
from typing import Self
class Foo:
      def fails[T: Self](self: T) -> None: pass
Expected Behavior

Mypy should reject Self here with an error.

From a quick look at other type checkers: pyright and ty treat it as an invalid/generic TypeVar bound (TypeVar upper bound cannot be generic), while zuban rejects it with the same message mypy already uses elsewhere,
Self type is only allowed in annotations within class definition.

Actual Behavior

No error is reported.

Your Environment

Mypy version: https://github.com/python/mypy/commit/ae39cdb2fba8908eeff6ec8d6b88879c62495fcc
Python version: 3.12

TLDR; originally spotted while working on #21928 See: https://github.com/python/mypy/pull/21928#issuecomment-5557910054

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

Beginne mit test-data/unit/check-selftype.test etwa bei Zeile 1480 und reproduziere das PEP-695-Beispiel aus dem Issue. Verfolge die bestehende Self-Validierung, die für andere ungültige Stellen verwendet wird, und füge dann Tests für eine Bindung oder Einschränkung eines Typparameters hinzu. Erledigt bedeutet, dass mypy für das Beispiel einen Fehler meldet, während bestehende Self-Tests weiterhin erfolgreich sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
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.