0

I have a function named 'sample_func'. As you can see when I call the function without passing the required parameter, there is a red warning.

enter image description here

I have a decorator called 'decorators' in a separate file:

def decorator1(func):
    def wrapper(arg1):
        print('decorator 1')
        return func(arg1)

    return wrapper

Now if I decorate the 'sample_func' with the 'decorator1' and call the function without passing the parameter, there is no red warning anymore.

enter image description here

My question is that how can I get this warning? I've seen this: https://stackoverflow.com/a/29569355/3337597 but I'm a little confused. If I determine the acceptable type for the argument and I pass another type, I'll get an 'unexpected type' warning.

enter image description here

I wonder why I'll get the 'unexpected type' warning but won't get the 'parameter unfilled' warning.

I got more confused when I tried this:

enter image description here

When I've defined the 'decorator1' in the same file as 'sample_func', I got the 'parameter unfilled' warning, but I won't when the decorator is defined in another file.

I appreciate any explanation of why this happens.

Also, I appreciate any solution to get the 'parameter unfilled' warning, even if it is using another pattern. I'm developing a package in which most of its functions are decorated to check if the user is logged in (or other conditions) and it would be a bit annoying to use when there is no such warning and you realize you missed a parameter or pass an unexpected parameter only in runtime.

  • You can try `functools.wrap`. – Vovin Jan 16 '23 at 11:38
  • I think you mean this: https://stackoverflow.com/a/147878/3337597 This helps to change what 'help' or 'inspect.signature' returns at run time, but still doesn't cause giving the 'parameter unfilled' or the 'unexpected parameter' warnings before run time. – Reyhaneh Sharifzadeh Jan 16 '23 at 13:44
  • If you use proper type annotations everywhere (which you should do in any case), any IDE worth its salt should have no problem inferring the type of the function coming out of the decorator, no matter where it is defined. You might want to look into [`typing.ParamSpec`](https://docs.python.org/3/library/typing.html#typing.ParamSpec) for this use case in particular and generics in general ([PEP 484](https://peps.python.org/pep-0484/#generics)). – Daniil Fajnberg Jan 16 '23 at 21:25
  • Does this answer your question? [Python 3 type hinting for decorator](https://stackoverflow.com/questions/47060133/python-3-type-hinting-for-decorator) – Daniil Fajnberg Jan 16 '23 at 21:30
  • I don't think so. It's about type checking, which I already get the related warning if I pass any unexpected type. I'm asking about the number of expected parameters; the problem is that there's no warning if I miss a parameter or pass an unexpected parameter (of number, not type). – Reyhaneh Sharifzadeh Jan 18 '23 at 09:40

0 Answers0