Skip to content

It is strongly recommended that the time module in your datetime library not be named "time", or be merged into the datetime module of datetime #135003

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
zyy37 opened this issue Jun 1, 2025 · 5 comments
Labels
type-feature A feature request or enhancement

Comments

@zyy37
Copy link

zyy37 commented Jun 1, 2025

Feature or enhancement

Proposal:

It is strongly recommended that the time module in your datetime library not be named time module, or be merged into the datetime module of datetime. Because the time module of datetime has the same name as the time module of the standard library and has low performance, the latter is more commonly used. If the former is used, organizations generally do not want to give the datetime.time module various aliases to avoid conflicts.

For example, how does python compare whether the current time is past 9:30? The implement is,

from datetime import datetime, time

current_time = datetime.now().time()
nine_thirty = time(9, 30)
status = current_time > nine_thirty

The from datetime import time will cause time name conflicts with import time which is a lightweight and high-performance standard libraries. But if developers use your datetime module to construct hours, minutes, and seconds with many lines, like now = datetime.now() hour, minute = now.hour, now.minute, or string way nine_thirty = datetime.strptime("09:30:00", "%H:%M:%S").time(), it will be very troublesome and far less elegant than a single line of code like nine_thirty = time(9, 30).

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

@zyy37 zyy37 added the type-feature A feature request or enhancement label Jun 1, 2025
@hugovk
Copy link
Member

hugovk commented Jun 1, 2025

Has this already been discussed elsewhere?

No response given

This needs discussing at https://discuss.python.org/c/ideas/6 first, but it is unlikely to be changed, to maintain backwards compatibility.


I recommend using this pattern to avoid ambiguity:

import datetime as dt

current_time = dt.datetime.now().time()
nine_thirty = dt.time(9, 30)
status = current_time > nine_thirty

(typed on phone, so there may be a typo)

See https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/

@hugovk hugovk closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2025
@skirpichev
Copy link
Contributor

datetime.time is not a module - it's a class:

>>> import datetime
>>> datetime.time
<class 'datetime.time'>

Because the time module of datetime has the same name as the time module of the standard library

Yes, this name clashes with the time module. It happens in other places of the stdlib as well. E.g. math.log and cmath.log.

Renaming (how?) will be a backward compatibility break. Why do you think it worth? To avoid conflict you might create alias like from datetime import time as badtime.

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label Jun 1, 2025
@zyy37
Copy link
Author

zyy37 commented Jun 1, 2025

You can close it, but you will still encounter many people like me who provide feedback on this issue in the future, just like the one in your link. This shows that the problem is not root cause, it is just a compromise.

@hugovk hugovk removed the pending The issue will be closed if no feedback is provided label Jun 1, 2025
@zyy37
Copy link
Author

zyy37 commented Jun 1, 2025

Maybe it is a software design issue.
For example,

from datetime import time, date # It is ok but it will conflict with the standard library 'import time'
t = time(9, 30)
d = date(2025, 12, 1)

or

import datetime # ok
t = datetime.time(9, 30)
d = datetime.date(2025, 12, 1)

both of the above methods are fine, but the one below is incorrect.

from datetime import datetime # error
t = datetime.time(9, 30)
d = datetime.date(2025, 12, 1)

A good design can avoid this confusion. Additionally, time() is sometimes a get property and sometimes a constructor. Going back to the beginning, what I meant was that constructing date or time objects was originally a very simple scenario, but it caused so much trouble...... Thanks.

@hugovk
Copy link
Member

hugovk commented Jun 1, 2025

As mentioned, please discuss this at discuss.python.org/c/ideas/6 first. Thanks.

@python python locked as resolved and limited conversation to collaborators Jun 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy