python / python/mypy

Errors from class decorators returning descriptors

Open
#13,862 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the test_class_dec.py example from the gist and compare the nested decorated BAZ class with BAR and BOO. Trace how mypy handles the descriptor and decorator types, then confirm the fix removes the reported errors while preserving the shown runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.