mypy throws an error when two pipes are combined
#769
Closed
PetitLepton
started this conversation in
General
Replies: 2 comments 12 replies
-
|
The issue is that mypy (and pyright) cannot infer the type of your lambda function but the definition of pipe depends on the return value of this lambda function. You could cast, but it would be better to provide type annotations for your lambda function: from typing import Callable
import pandas as pd
reveal_type(lambda df: df) # "def (df: Any) -> Any"
reveal_type(pd.DataFrame().pipe(lambda df: df)) # Any
# explicitly declare the type
fun: Callable[[pd.DataFrame], pd.DataFrame] = lambda df: df
reveal_type(pd.DataFrame().pipe(fun).pipe(fun)) # DataFrame
# or use a def
def fun2(df: pd.DataFrame) -> pd.DataFrame:
return df
reveal_type(pd.DataFrame().pipe(fun2).pipe(fun2)) # DataFrame |
Beta Was this translation helpful? Give feedback.
11 replies
-
|
@PetitLepton OK to close the discussion? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am taken aback by the behavior of
mypy. With the following minimal examplemypybarks at me withbut a single
pipeis OK.Do I have to cast manually the first
pandas.DataFrame().pipe(lambda df: df)?I am using
mypy1.51,pandas2.0.3 andpandas-stubs2.0.3.230814.Thanks for your help!
Beta Was this translation helpful? Give feedback.
All reactions