python / python/mypy

Errors from class decorators returning descriptors

Aperta
#13,862 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

When using a decorator on a nested class that returns a descriptor, mypy is not able to properly distinguish between the class and the instance. The full code is in the gist with the snippets below trying to highlight the problem.

https://gist.github.com/adam-ring/86dc9239fd54e2eafe07829329f39a0d

@overload
def add_register(cls: None = ...) -> RegDescpr[Register]:
    ...
@overload
def add_register(cls: type[T]) -> RegDescpr[T]:
    ...
def add_register(cls: type[T] | None = None) -> RegDescpr[T]:
    return RegDescpr[T](cls)


class FooBlock(RegBlock):

    BAR = add_register()

    @add_register
    class BAZ(Register):
        A = FieldDescpr()
        B = FieldDescpr()

    class BOO_CLS(Register):
        C = FieldDescpr()
    BOO = add_register(BOO_CLS)

foo = FooBlock()

Both foo.BAR and foo.BOO show no errors and run correctly. foo.BAZ lists errors with attribute or method access as will be shown below.

To Reproduce

The method access tests are shown here. Python runs as expected from all three, but mypy complains about an unbound self when accessing foo.BAZ.

foo = FooBlock()
print(f'{foo.BAR.reg_func()=}')  # no error
print(f'{foo.BAZ.reg_func()=}')  # ERROR: reg_func has unbound self
print(f'{foo.BOO.reg_func()=}')  # no error

Expected Behavior

Python runs with all test cases. Here is the output when the gist is run with Python.

$ python3 test_class_dec.py 
Register(name=BAR, RegBlock=FooBlock())
BAZ(name=BAZ, RegBlock=FooBlock())
BOO_CLS(name=BOO, RegBlock=FooBlock())
RegField(name=B, Register=BAZ(name=BAZ, RegBlock=FooBlock()))
RegField(name=C, Register=BOO_CLS(name=BOO, RegBlock=FooBlock()))
foo.BAR.reg_func()=42
foo.BAZ.reg_func()=42
foo.BOO.reg_func()=42
foo.BAZ.A()=5
foo.BOO.C()=5

Actual Behavior

Here is the output from mypy. Only foo.BAZ shows any errors in mypy, the class with the descriptor. The errors are present with method access and with the lower lever Field Descriptor access.

$ mypy test_class_dec.py 
test_class_dec.py:121: error: No overload variant of "__get__" of "FieldDescpr" matches argument types "None", "Type[BAZ]"
test_class_dec.py:121: note: Possible overload variants:
test_class_dec.py:121: note:     def __get__(self, obj: None, objtype: None) -> FieldDescpr
test_class_dec.py:121: note:     def __get__(self, obj: Register, objtype: Type[Register]) -> RegField
test_class_dec.py:125: error: Missing positional argument "self" in call to "reg_func" of "Register"
test_class_dec.py:127: error: No overload variant of "__get__" of "FieldDescpr" matches argument types "None", "Type[BAZ]"
test_class_dec.py:127: note: Possible overload variants:
test_class_dec.py:127: note:     def __get__(self, obj: None, objtype: None) -> FieldDescpr
test_class_dec.py:127: note:     def __get__(self, obj: Register, objtype: Type[Register]) -> RegField

Your Environment

  • Python 3.10.4
  • mypy 0.981 (compiled: no)

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 eseguendo mypy sull'esempio test_class_dec.py del gist e confronta la classe BAZ decorata annidata con BAR e BOO. Analizza come mypy gestisce i tipi di descriptor e decorator, quindi conferma che il fix rimuove gli errori segnalati preservando il comportamento mostrato a runtime.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.