python / python/mypy

Decorating `__init__()` with a class decorator is not supported

Aperta
#18,513 2 commenti 1 reazione 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

I want to define a decorator as a class to decorate another class's methods. However, when __init__() constructor is decorated with that class, mypy reports Unsupported decorated constructor type error.

(For reference) Stackoverflow question I posted: https://stackoverflow.com/questions/79374167/mypy-unsupported-decorated-constructor-type-error-when-decorating-init-w

To Reproduce

from types import MethodType
from typing import TypeVar, Callable, Type, ParamSpec, Generic

T = TypeVar("T")
P = ParamSpec("P")
R = TypeVar("R")

class method_decorator(Generic[P, R]):
    def __init__(self, method: Callable[P, R]):
        self.method = method

    def __get__(self, instance: T | None, cls: Type[T]) -> Callable[..., R]:
        return self if instance is None else MethodType(self, instance)

    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
        return self.method(*args, **kwargs)

class Foo:
    @method_decorator # error: Unsupported decorated constructor type  [misc]
    def __init__(self, x: int):
        self.x = x

    @method_decorator
    def mul(self, a: int) -> int:
        return self.x * a

def gen_foo(x: int) -> Foo:
    return Foo(x) # error: Returning Any from function declared to return "Foo"  [no-any-return]

foo = gen_foo(2)
print(foo.mul(3))

Actual Behavior

$ uv run mypy --strict test.py
test.py:19: error: Unsupported decorated constructor type  [misc]
test.py:28: error: Returning Any from function declared to return "Foo"  [no-any-return]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

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

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 reproducer fornito ed esegui uv run mypy --strict test.py usando l’ambiente Python 3.11 mostrato. Traccia il modo in cui mypy gestisce il costruttore __init__() decorato e confrontalo con il metodo mul() decorato. Il lavoro è completato quando il costruttore non segnala più Unsupported decorated constructor type e gen_foo non restituisce più Any durante il controllo strict.

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
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.