Phyton Infer generic lambda parameters from another generic lambda's parameters

Consider the following code:

from typing import Callable, TypeVar

T = TypeVar('T')

def middle_man(
    producer: Callable[[], T],
    consumer: Callable[[T], None]
) -> None:
    consumer(producer())

middle_man(
    lambda: "HELLO",
    lambda s: print(s.lower())
)

This code runs without error and works as expected, however, mypy fails to infer the type of s in the second lambda, giving the error:

"object" has no attribute "lower"`

Right now, my only workaround is either to cast s, which can be a pain in a more complex lambda or with a more complex type, or to add # type: ignore, which I'd rather not do.

Is there a better workaround, or a proper way to get mypy to recognize the type?

Answer:1



I forked a GitHub project in Python. After running the project for the first time, some .pyc files appeared inside. Should I put them under version control and commit them to my fork?

I forked a GitHub project in Python. After running the project for the first time, some .pyc files appeared inside. Should I put them under version control and commit them to my fork?

So, i trying to install with the command ecmwf api client conda install -c conda-forge ecmwf-api-client then the warning in the title shows up. I don't know how to proceede (base) C:\Users\caina>...

So, i trying to install with the command ecmwf api client conda install -c conda-forge ecmwf-api-client then the warning in the title shows up. I don't know how to proceede (base) C:\Users\caina>...