-
I’m working with the python-telegram-bot library and created a decorator to handle boilerplate checks for method parameters. The purpose of the decorator is to streamline parameter validation by moving it outside the core function logic. Here’s the decorator: `def callback_query_check(func):
I’ve applied the decorator to a function like this:
However, Pylance raises the following error for the query.data line: It seems Pylance doesn’t recognize that the decorator ensures None values are handled. Is there a way to make Pylance account for the decorator logic or suppress this false positive effectively? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If pyright encounters an unannotated decorator, it assumes that it doesn't modify the signature of the decorated function (which is the typical case). If your decorator modifies the signature of the decorated function, you need to provide type annotations for your decorator. |
Beta Was this translation helpful? Give feedback.
It's difficult for me to understand the problem based on the code you've provided. The code is not properly formatted and contains a bunch of symbols that are undefined. For example, I don't know how the
Update
class is defined. I'm guessing thatcallback_query
is an attribute or property whose type is declared asCallable | None
or something like that. If my assumption is correct, then the answer is no, the Python type system doesn't provide a way for one function (the decorator) to tell another function (the decorated function) that "I've tested this attribute within this object, and it is not None". You would need to add an assert in the code within the decorated function to handle this.