PyCharm warning for 'None' argument passed to function decorated with functools.cache

50 views Asked by At

I have a function that may accept None as a value of its argument, this function is cached using functools.cache. Here is a simplified example:

from functools import cache
counter = 0

@cache
def cached_func(arg1, arg2):
    global counter
    print('Call of cached func: #{}, args: {}, {}'.format(counter, arg1, arg2))
    counter += 1
    return None

cached_func(1, None)
cached_func(2, None)
cached_func(1, None)  # <- cache will be used
cached_func(3, None)

Output:

Call of cached func: #0, args: 1, None
Call of cached func: #1, args: 2, None
Call of cached func: #2, args: 3, None

As I can see caching works ok. But at the same time PyCharm shows me a warning on None argument:

enter image description here

According to the documentation for functools, "Since a dictionary is used to cache results, the positional and keyword arguments to the function must be hashable."

I see that None is actually hashable:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__hash__()
-9223363242286426420
>>> hash(None)
-9223363242286426420

So, should I care about this warning? It it ok to pass None as argument to a cached function?

0

There are 0 answers