Skip to content

Conversation

Dr-Irv
Copy link
Contributor

@Dr-Irv Dr-Irv commented Feb 20, 2022

I would like to add pandas/_libs/interval.pyi, and I have a tentative file for that, but when I started to do that, it uncovered issues in our existing typing, because currently, mypy sees Interval as Unknown, which allows a bunch of things to pass.

So this PR addresses that issue. The way to see the problems that were fixed by this PR is to change in _typing.pyi:

PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]

TO

PandasScalar = Union["Period", "Timestamp", "Timedelta"]  # , "Interval"]

A followup PR will add pandas/_libs/interval.pyi and make changes to handle that.

@Dr-Irv Dr-Irv added the Typing type annotations, mypy/pyright type checking label Feb 20, 2022
@twoertwein
Copy link
Member

Shameless plug :) PR based on the Microsoft-stubs: #44922

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Feb 20, 2022

Shameless plug :) PR based on the Microsoft-stubs: #44922

Ugh. I wrote that stub in the MS stubs, which is why I tried to then convert it over to use in pandas. Took me a long time to get this far!

I guess I should close this? @twoertwein ?

@twoertwein
Copy link
Member

Sorry @Dr-Irv getting interval.py right is definitely a mess.

Let's keep this PR open in case my PR is not the preferred solution.

@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented Feb 21, 2022

Sorry @Dr-Irv getting interval.py right is definitely a mess.

Let's keep this PR open in case my PR is not the preferred solution.

If we go this direction, then I still have to create a PR for the interval.pyi part. This "part 1" PR is just to fix up things that are a problem because Interval is undefined.

@simonjayhawkins
Copy link
Member

This "part 1" PR is just to fix up things that are a problem because Interval is undefined.

IMO baby steps is definitely OK with typing.

return stamp.time()
else:
self.close()
raise ValueError(f"Unrecognized time {str(cell)}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If changing code in a typing PR, I have some questions...

does it change behavior?
has typing found a latent bug?
is a release note needed?
do we need some additional tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it is a time, the other solution is to just do a cast to Timestamp since we know the result won't be a NaTType . I will revert to that with a comment

@simonjayhawkins
Copy link
Member

Shameless plug :) PR based on the Microsoft-stubs: #44922

Ugh. I wrote that stub in the MS stubs, which is why I tried to then convert it over to use in pandas. Took me a long time to get this far!

I guess I should close this? @twoertwein ?

@jbrockmendel created nearly all the stubs for the cython code last year and also created a stub for libinterval #41059

it uncovered issues in our existing typing, because currently, mypy sees Interval as Unknown, which allows a bunch of things to pass.

yep. from #41059 (comment)

This one is pretty ugly.

@Dr-Irv Dr-Irv mentioned this pull request Feb 21, 2022
Copy link
Member

@twoertwein twoertwein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Happy to close my PR.

@jreback jreback added this to the 1.5 milestone Feb 26, 2022
@jreback jreback merged commit 93ba57a into pandas-dev:main Feb 26, 2022
@jreback
Copy link
Contributor

jreback commented Feb 26, 2022

thanks @Dr-Irv

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
@Dr-Irv Dr-Irv deleted the typeinterval_part1 branch February 13, 2023 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Typing type annotations, mypy/pyright type checking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants