I have this function:
def chunk_load(filename: str, chunksize=1000, max_chunks:Optional[int]=None, expected_chunks=Optional[int]) -> pd.DataFrame:
display_string = f'Loading {os.path.basename(filename)}|chunksize={chunksize}'
if max_chunks is None:
df = pd.concat([chunk for chunk in tqdm(pd.read_csv(filename, chunksize=chunksize), desc=display_string, total=expected_chunks)]) # pylance says that `total` is not compatible
else:
chunks = []
for _ in tqdm(range(max_chunks), desc=display_string):
try:
chunk = next(pd.read_csv(filename, chunksize=chunksize))
chunks.append(chunk)
except StopIteration:
break
df = pd.concat(chunks)
return df
Pylance is complaining that "UnionType" is incompatible with type "float | None"
I created a small example that I thought would mimic the same dynamic:
from typing import Optional
def foo(foo_val: Optional[float]) -> None:
pass
def trouble_foo(trouble_val: float | None) -> None:
pass
def bar(bar_val: Optional[int]) -> None:
return foo(bar_val)
def trouble_bar(trouble_bar_val: Optional[int]) -> None:
return trouble_foo(trouble_bar_val)
bar(35)
trouble_bar(42)
Pylance shows no errors for any of this.
Why is it that for the chunk_load
function, passing in an Optional[int]
for the total
parameter is not okay because it expects a float | None
, but, in the simple example, doing this is totally fine?