Question
Pylance reports "Code is unreachable", when my test shows otherwise
Pylance claims that code under if not dfs:
is unreachable and greys it out in VS Code.
import pandas as pd
def concat_dfs(dfs: tuple[pd.DataFrame | None], logger) -> pd.DataFrame | None:
# filter out dfs that are None, if any
dfs = [df for df in dfs if df is not None]
# concatinate dfs, if any are not None:
if not dfs:
logger.warning("All elements of dfs are None, so no inputs remain to be analyzed")
return None # greyed out as unreachable
else:
return pd.concat(dfs).sort_index()
I tried returning dfs
alone, and not pd.concat(dfs).sort_index()
, as there are/were some pd.concat
related bugs in Pylance.
I also tried making a little test. If Pylance is right, I would have expected not dfs = False
, and it is True
instead:
dfs = (None, None)
dfs = [df for df in dfs if df is not None] # dfs is an empty list
not dfs # returns True --> code should be reachable when every dataframe in dfs is None to begin with...