Skip to content

function that returns decorator as class member raises "self-argument missing" #15549

Closed as not planned
@sanjacob

Description

@sanjacob

Bug Report

Similar to #7778, where having a function that returns a decorator inside a class without a self parameter
is not possible.

To Reproduce
I have this setup to be able to do this inside a class:

@log('hello world')
def method(self):
    ...
from functools import wraps
from typing import Callable

class MyClass:
    def log(text: str):
        def log_decorator(func: Callable) -> Callable:
            @wraps(func)
            def log_wrapper(self):
                func(self)
                print(text) # Passed as param in log
            return log_wrapper
        return log_decorator


    @log('print this')
    def test(self) -> None:
        print('word')

foo = MyClass()
foo.test()

Expected Behavior
mypy should not find any errors with this.

Actual Behavior

mypy test_concept.py
test_concept.py:5: error: Self argument missing for a non-static method (or an invalid type for self)  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.4.1 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions