Tips on Type Narrowing #2703
padraic-mcatee
started this conversation in
General
Replies: 1 comment 3 replies
-
I would suggest an def exclude_iterator(df_or_iterator: pd.DataFrame | Iterator[pd.DataFrame]) -> pd.DataFrame:
if isinstance(df_or_iterator, pd.DataFrame):
return df_or_iterator
raise TypeError("single dataframe expected")
result = exclude_iterator(api_func()).some_method() |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I'm a bit new to typing in python (coming from scala). I tend to write long chained method calls for processing pandas dataframes. I am working with an API that often returns union types of dataframes and iterators of dataframes. However, I rarely need to handle the case of iterators and would prefer to assume return type of simple DataFrame. For example:
Does anyone have a nice typeguard / cast pattern for resolving this? Some method on the Union type that would return one of its constituent types?
Beta Was this translation helpful? Give feedback.
All reactions