Skip to content

@functools.cache destroys the function signature #11280

Open
@Aran-Fey

Description

@Aran-Fey

A function decorated with @functools.cache loses its signature:

@functools.cache
def my_func(foo: int | list) -> str:
    return str(foo)

my_func('completely', 'incorrect', 'arguments')
# Success: no issues found in 1 source file

There is an error if you try to pass an argument that isn't hashable:

my_func([])
# error: Argument 1 to "__call__" of "_lru_cache_wrapper" has
# incompatible type "list[Never]"; expected "Hashable"  [arg-type]

But this completely ruins Intellisense:

image

I understand that the type system isn't powerful enough to express the correct semantics, which would be this:

@functools.cache
def my_func(foo: int | list) -> str:
    return str(foo)

reveal_type(my_func)  # Callable[[int], str]

That said, I would much rather have functioning Intellisense than a warning about unhashable inputs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false negativeType checkers do not report an error, but should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions