python / python/mypy

Calling `type[T]` returns the parent of `T` on Python 3.12+

Offen
#18,088 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report / To Reproduce
Ok so this is a weird one, and I've been completely unable to replicate it in an MRE. So here's the PR where this happened to us: https://github.com/pypa/setuptools/pull/4726

To replicate, clone the setuptools repo, install the [core,type] extras, then change the start of setuptools/command/install.py's install.do_egg_install to:

    def do_egg_install(self) -> None:
        easy_install = cast(
            type[easy_install_cls], self.distribution.get_command_class('easy_install')
        )

        cmd = easy_install(
            self.distribution,
            args="x",
            root=self.root,
            record=self.record,
        )
        reveal_type(cmd)

On Python 3.11

setuptools\command\install.py:150: note: Revealed type is "setuptools.command.easy_install.easy_install"
Success: no issues found in 143 source files

On Python 3.12 & 3.13

setuptools\command\install.py:150: note: Revealed type is "setuptools.Command"
setuptools\command\install.py:166: error: Unexpected keyword argument "show_deprecation" for "run" of "Command"  [call-arg]
Found 1 error in 1 file (checked 143 source files)

My first guess is this has something to do with distutils removal in 3.12, but the results I'm getting are still nonsensical and probably reveal a hidden bug in mypy.

Expected Behavior
cmd should be of type setuptools.command.easy_install or alias easy_install_cls

Actual Behavior

cmd is of type setuptools.Command (superclass of setuptools.command.easy_install)

Your Environment

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 damit, den Bericht in setuptools/command/install.py mit mypy unter Python 3.11, 3.12 und 3.13 nachzustellen, und vergleiche den angezeigten Typ für cmd. Prüfe den verlinkten setuptools-Pull-Request und die mypy.ini-Konfiguration; abgeschlossen ist es, wenn der Cast auf type[easy_install_cls] den erwarteten easy_install-Typ ohne den überflüssigen Command-Fehler anzeigt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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