Skip to content

t.Optional[t.Union[...]] leads to incorrect behaviour #19204

Closed as not planned
Closed as not planned
@atharva-kelkar

Description

@atharva-kelkar

Bug Report
When using t.Optional with t.Union, mypy checks fail with conditional processing of different types. However, removing t.Optional from the code below leads to a successful check.

To Reproduce

import pandas as pd
import numpy as np
import typing as t

def cast_to_pandas(x: np.ndarray) -> t.Union[pd.Series, pd.DataFrame]:
    if x.ndim == 1:
        return pd.Series(x)
    return pd.DataFrame(x)

def pred(x: t.Optional[t.Union[np.ndarray, pd.Series, pd.DataFrame]] = np.ones(10)) -> None:
    if isinstance(x, np.ndarray):
        x = cast_to_pandas(x)

    print(x.values.ravel)

Expected Behavior
I would have expected this code to pass mypy type-checking because an np.ndarray type is separately cast into pd.Series or pd.DataFrame.

Actual Behavior
Get the following error:

trial.py:14: error: Item "ndarray[Any, Any]" of "ndarray[Any, Any] | Any" has no attribute "values"  [union-attr]
Found 1 error in 1 file

Your Environment
mypy==1.16.0
pandas==2.2.3
numpy==2.2.6

  • Mypy version used: 1.16.0
  • Mypy command-line flags: -p package
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.13.3

Edits: Removed redundant comments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions