Skip to content

make Timestamp.strptime() return None #541

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

Merged
merged 2 commits into from
Feb 17, 2023
Merged

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Feb 17, 2023

mypy_nightly started failing because typeshed got updated to have datetime.strptime() return Self, and since Timestamp is a subclass of datetime, the nightly mypy build was saying that the datetime result of strptime() was invalid since it should return Timestamp if the superclass returns Self.

But Timestamp.strptime() is not implemented, and we always raise, so changed the arguments to Never meaning that type checkers will always say it is invalid to call this method.

@Dr-Irv Dr-Irv requested a review from twoertwein February 17, 2023 04:32
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.

Has typeshed documented how to handle such cases (using Never, NotImplementd, ...)? I did a quick search but I didn't find a comment on that.

@twoertwein twoertwein merged commit 9f4013b into pandas-dev:main Feb 17, 2023
@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Feb 17, 2023

Has typeshed documented how to handle such cases (using Never, NotImplementd, ...)? I did a quick search but I didn't find a comment on that.

I didn't look. But this all depends on whether you want to strongly enforce the Liskov substitution principle. mypy does, while pyright does not.

In this particular case, we don't want people to call Timestamp.strptime() . Right now, if you do, an exception is raised. If we remove the declaration of strptime() from timestamps.pyi, then since Timestamp is a subclass of datetime.datetime, both mypy and pyright will say that a call to Timestamp.strptime() is valid, because it will find the stub for datetime.datetime.strptime(). By using the Never trick, we are telling the type checker that this is an invalid function to call, and I think that is the best user experience.

@twoertwein
Copy link
Member

I didn't look. But this all depends on whether you want to strongly enforce the Liskov substitution principle. mypy does, while pyright does not.

(I prefer pyright's approach, while mypy's approach is theoretically better - it is just not compatible with python: everything inherits from object - but some classes violate its __hash__). I definitely like informing users of the error (instead of adding a type ignore in pandas-stubs), but I wasn't sure whether there is a "typeshed-recommended" way to do so.

@Dr-Irv Dr-Irv deleted the nostrptime branch February 27, 2023 22:17
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this pull request Apr 1, 2023
* make Timestamp.strptime() return None

* Use Never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants