python / python/mypy

Using `Never` as return type for `__new__` results in classmethods not being recognised. Inferred type for class is `Callable[[], NoReturn]`.

Aperta
#16,981 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

If the classmethod __new__ of a class is marked as returning Never, Mypy does not recognise the existence of attributes for the class (whose type it believes to be Callable[[], NoReturn]).

To Reproduce

from typing import Never, Self

class A:
    x: int

    @classmethod
    def _new(cls, x: int) -> Self:
        instance = super().__new__(cls)
        instance.x = x
        return instance

    def __new__(cls) -> Never:
        raise TypeError("Class 'A' should not be instanced directly.")

a = A._new(10) # Mypy Error: attr-defined
# "Callable[[], NoReturn]" has no attribute "_new"

Gist URL: https://gist.github.com/mypy-play/b40fec12cb83d7683c6832320aecc4d8
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=b40fec12cb83d7683c6832320aecc4d8

from typing import ClassVar, Never

class A:
    attr: ClassVar[int] = 20

    def __new__(cls) -> Never:
        raise TypeError("Class 'A' should not be instanced directly.")

a = A.attr # Mypy Error: attr-defined
# "Callable[[], NoReturn]" has no attribute "attr"

Gist URL: https://gist.github.com/mypy-play/46b45241159045b55d7dfd9d871280f1
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=46b45241159045b55d7dfd9d871280f1

Expected Behavior

I expect Mypy to recognise that the classmethod A._new() can be called to produce an instance of A, or that A has a class attribute attr.

Actual Behavior

Mypy does not recognise that classmethod A._new() exists, nor that the class attribute A.attr exists.

Your Environment

  • Mypy version used: 1.7.1 (checked with 1.8.0 on playground)
  • Mypy command-line flags: --strict
  • 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

Riproduci entrambi gli esempi dell’issue con mypy 1.8.0 o versioni successive, usando --strict e Python 3.12, e confrontali con i playground collegati. Traccia la gestione di una classe il cui new restituisce Never; il lavoro è considerato completato quando A._new() e A.attr vengono riconosciuti senza errori attr-defined.

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.