Skip to content

datetime supports non-int arguments #9902

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

Closed
sobolevn opened this issue Mar 18, 2023 · 6 comments
Closed

datetime supports non-int arguments #9902

sobolevn opened this issue Mar 18, 2023 · 6 comments

Comments

@sobolevn
Copy link
Member

sobolevn commented Mar 18, 2023

While discussing python/cpython#102791 I've noticed that datetime's objects can support non-int arguments:

>>> import datetime
>>> class Day:
...     def __index__(self):
...          return 1  # whatever
... 
>>> datetime.datetime(year=2023, month=3, day=Day())
datetime.datetime(2023, 3, 1, 0, 0)

Technically it might be used with some libs/dsls like datetime.datetime(year=Year(2023), month=Month(3), day=Day(2))

Should we use SupportsIndex there?

@srittau
Copy link
Collaborator

srittau commented Mar 19, 2023

I am ok with supporting it here, since the various date classes explicitly check for it. But nothing I find particularly important, unless someone explicitly asks for it.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 21, 2023

The proposed change SGTM too!

@sobolevn
Copy link
Member Author

I confirm that __index__ is not an implementation detail here, it is a choice. Python's implementation also supports it: https://github.com/python/cpython/blob/7f760c2fca3dc5f46a518f5b7622783039bc8b7b/Lib/datetime.py#L518-L535

But, on the other hand: it is not documented and it is not tested.

So, I think we should keep this open. If some real use-case needs this, I will send a PR.
But, right now it is hypothetical (I've just seen this while reading CPython's source code).

@AlexWaygood
Copy link
Member

Our policy is to prefer following the implementation rather than the documentation: https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#what-to-do-when-a-projects-documentation-and-implementation-disagree

@sobolevn
Copy link
Member Author

Ok, I will send a PR then 🚀

@sobolevn
Copy link
Member Author

Resolved in #9915

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

No branches or pull requests

3 participants