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

"pathlib.Path / pd.Series" should be inferred as Series, not as Path #682

Open
bersbersbers opened this issue May 3, 2023 · 2 comments
Open
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Strings String extension data type and string data

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.
from pathlib import Path

import pandas as pd

folder = Path.cwd()
files = pd.Series(["a.png", "b.png"])
# reveal_type(files)  # good: Series!
paths = folder / files
# reveal_type(paths)  # bad: pathlib.Path!
good_paths = [f if f.is_file() else pd.NA for f in paths]

print(paths)
print(good_paths)
  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:10: error: "Path" has no attribute "__iter__"; maybe "__enter__"? (not iterable)  [attr-defined]
Found 1 error 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
@twoertwein
Copy link
Member

__truediv__ and __rtruediv__ are defined for Path and Series but based on the annotations of the second argument non of them claims to allow Path / Series so I'm surprised that mypy/pyright do not report an error here.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 5, 2023

__truediv__ and __rtruediv__ are defined for Path and Series but based on the annotations of the second argument non of them claims to allow Path / Series so I'm surprised that mypy/pyright do not report an error here.

I guess this is a mypy bug, because pyright DOES pick this up as invalid in terms of the / operator.

I wouldn't want to support __truediv__ and __rtruediv__ for untyped Series. We could support it for Series[str], but to do that, we'd have to create a StringSeries (as we have done with TimestampSeries, TimedeltaSeries, etc.) so that the __truediv__ and __rtruediv__ operators would work for Path arguments, but not for untyped series, or series with other dtypes. Then, for the OP example, you'd change the declaration of the first Series to be files = pd.Series(["a.png", "b.png"], dtype=str)

Open to a PR that does this.

@Dr-Irv Dr-Irv added Numeric Operations Arithmetic, Comparison, and Logical operations Strings String extension data type and string data labels May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

3 participants