Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument 1 to "apply" of "Series" has incompatible type "Callable[[Any], Union[int, NAType]]" #681

Closed
bersbersbers opened this issue May 3, 2023 · 2 comments · Fixed by #724
Labels
Apply Apply, Aggregate, Transform good first issue

Comments

@bersbersbers
Copy link

bersbersbers commented May 3, 2023

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
import pandas as pd

# these values are allowed
allowed = ["A", "B", "C"]

df = pd.DataFrame()

# we add a few that are not allowed
df["Column"] = ["A", "B", "C", "D"]

# replace values by index, and NA if not allowed
df["Label"] = df.Column.apply(lambda x: allowed.index(x) if x in allowed else pd.NA)

print(df.Label)
  1. Indicate which type checker you are using (mypy or pyright).
    mypy 1.2.0 (compiled: yes)

  2. Show the error message received from that type checker while checking your example.

bug.py:12: error: Argument 1 to "apply" of "Series" has incompatible type "Callable[[Any], Union[int, NAType]]"; expected "Callable[..., Union[str, bytes, date, datetime, timedelta, datetime64, timedelta64, bool, int, float, Timestamp, Timedelta, integer[Any], floating[Any], complex, Sequence[Any], Set[Any], Mapping[Any, Any]]]"  [arg-type]
bug.py:12: error: Incompatible return value type (got "Union[int, NAType]", expected "Union[str, bytes, date, datetime, timedelta, datetime64, timedelta64, bool, int, float, Timestamp, Timedelta, integer[Any], floating[Any], complex, Sequence[Any], Set[Any], Mapping[Any, Any]]")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

Please complete the following information:

  • OS: Windows 11
  • OS Version: 22H2
  • python version: 3.10.11
  • version of type checker: 1.2.0
  • version of installed pandas-stubs: 2.0.1.230501

Related: #262?

@twoertwein
Copy link
Member

Probably need to add NAType here

func: Callable[..., Scalar | Sequence | set | Mapping | None],
or even to Scalar / S1.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 5, 2023

Probably need to add NAType here

Correct

func: Callable[..., Scalar | Sequence | set | Mapping | None],

or even to Scalar / S1.

I'm wary (as I wrote #262 (comment)) of doing the latter because it may make some of our existing declarations too wide.

So better to make the change as suggested above to handle this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform good first issue
Projects
None yet
3 participants