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

Not planned
@zyy37

Description

@zyy37

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

Activity

hugovk

hugovk commented on Jun 1, 2025

@hugovk
Member

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/

skirpichev

skirpichev commented on Jun 1, 2025

@skirpichev
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.

added
pendingThe issue will be closed if no feedback is provided
on Jun 1, 2025
zyy37

zyy37 commented on Jun 1, 2025

@zyy37
Author

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.

removed
pendingThe issue will be closed if no feedback is provided
on Jun 1, 2025
zyy37

zyy37 commented on Jun 1, 2025

@zyy37
Author

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

hugovk commented on Jun 1, 2025

@hugovk
Member

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

locked as resolved and limited conversation to collaborators on Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      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 · Issue #135003 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant