python / python/mypy

`self.__class__` produces a type check with a constructor where a concrete reference to the class works fine

Aperta
#18,279 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I'm not sure how this bug generalizes, but I discovered this with an HTTP headers abstraction with a clone method and the example minifies pretty far down, as shown below

To Reproduce

from typing import Mapping, Sequence, AnyStr, Self

class c:
    _v: dict[bytes, list[bytes]]
    def __init__(self, v: Mapping[AnyStr, Sequence[AnyStr]]):
        pass

    def clonecls(self) -> Self:
        return self.__class__(self._v)

    def clone(self) -> c:
        return c(self._v)

https://mypy-play.net/?mypy=latest&python=3.12&gist=0be4308fb57a3685fafab2ac51a97c85

Expected Behavior

No errors.

Actual Behavior

main.py:9: error: Argument 1 to "c" has incompatible type "dict[bytes, list[bytes]]"; expected "Mapping[AnyStr, Sequence[AnyStr]]"  [arg-type]

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il riproduttore minimo in main.py o con l’esempio mypy-play collegato, confrontando clonecls() che usa self.class con clone() che usa direttamente c. Esegui mypy con la configurazione Python 3.12 indicata e aggiungi un test di regressione che registri l’assenza prevista di un errore di tipo dell’argomento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.