OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is `Optional[int]` not compatible with `float | None`? [closed]

  • Thread starter Thread starter Pro Q
  • Start date Start date
P

Pro Q

Guest
I have this function:

Code:
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"

Pylance complaining

I created a small example that I thought would mimic the same dynamic:

Code:
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 simple example.

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?
<p>I have this function:</p>
<pre class="lang-py prettyprint-override"><code>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
</code></pre>
<p>Pylance is complaining that <code>"UnionType" is incompatible with type "float | None"</code></p>
<p><a href="https://i.sstatic.net/DAn1Fm4E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DAn1Fm4E.png" alt="Pylance complaining" /></a></p>
<p>I created a small example that I thought would mimic the same dynamic:</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<p>Pylance shows no errors for any of this simple example.</p>
<p><strong>Why is it that for the <code>chunk_load</code> function, passing in an <code>Optional[int]</code> for the <code>total</code> parameter is not okay because it expects a <code>float | None</code>, but, in the simple example, doing this is totally fine?</strong></p>
 

Latest posts

Top