Skip to content

Loss of typing information on composed functions #569

@muxator

Description

@muxator

Passing any function through compose() seems to strip away all the information about the types of those functions.

Minimal example:

import toolz


def f(x: int) -> str:
    return f"{x}"


# compose f with the identity function
g = toolz.compose(f)

reveal_type(f(1))
reveal_type(g(1))

Testing on python 3.11 yields:

$ mypy --version
mypy 1.5.1 (compiled: yes)

$ mypy test_toolz.py 
test_toolz.py:11: note: Revealed type is "builtins.str"
test_toolz.py:12: note: Revealed type is "Any"
$ pyright --version
pyright 1.1.323

$ pyright test_toolz.py 
<BASE>/test_toolz.py
  <BASE>/test_toolz.py:11:13 - information: Type of "f(1)" is "str"
  <BASE>/test_toolz.py:12:13 - information: Type of "g(1)" is "Unknown | Literal[1]"

I expected that the static type checker would have printed builtins.str even for g(1).

This is a stripped down example of a larger problem I had early on on a project of mine, where I wrongly assumed that composed functions would have brought over the type information about their signature. I ended up introducing a bug in my project because of this assumption.

Is this use case something that toolz could ever support? I have no idea if this is due to a deep limitation in the Python language or is simply something that eventually needs to be worked on.

I also read #496, but I do not know if my problem could be solved by working on that issue.

Thank you for the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions