Skip to content

Add import datetime as dt as a conventional alias #28604

Closed

Description

@adamchainz

Summary

I propose adding "datetime": "dt" as an entry in the lint.flake8-import-conventions.aliases default.

CPython started recommending this alias from a change earlier this year by @hugovk: python/cpython#145315

This was based on my 2019 blog post: https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/

Activity

  1. ntBre commented on Sep 15, 2026

    @ntBre
    Contributor

    I think this is technically a breaking change, but we have a minor release coming up for Python 3.15 and can include it in that.

  2. added this to the v0.17 milestone on Sep 15, 2026
  3. added
    rule
    breaking
    python315
    on Sep 15, 2026
  4. adamchainz commented on Sep 17, 2026

    @adamchainz
    ContributorAuthor

    Are you saying you'll include it for Python 3.15+ only? That seems like a reasonable decision, given that's when the docs start recommend the alias.

  5. ntBre commented on Sep 17, 2026

    @ntBre
    Contributor

    I just meant that we could include the modification to the default aliases value in our v0.17 release, which will also include other breaking changes related to 3.15.

    I guess we could also make the default value dependent on the configured Python version, but that's not what I had in mind at the time.

  6. adamchainz commented on Sep 18, 2026

    @adamchainz
    ContributorAuthor

    Right, I see.

    On second thoughts, it would be best not to tie this change to intrpreter version, since there’s nothing inherent about Python 3.15 affecting the alias. Making linter fixes/errors appear when you upgrade Ruff, rather than when you upgrade Python, is less confusing.

  7. hugovk commented on Sep 19, 2026

    @hugovk
    Contributor

    Are you saying you'll include it for Python 3.15+ only? That seems like a reasonable decision, given that's when the docs start recommend the alias.

    We backported python/cpython#145315 to all bugfix versions (back to 3.13), so the 3.15 distinction isn't special.

  8. added a commit that references this issue on Sep 25, 2026
    6a80f54
  9. added 4 commits that reference this issue on Sep 25, 2026
    1c1aa07
    389861d
    008674d
    23948fb
  10. added 2 commits that reference this issue on Oct 9, 2026
    ec361a2
    dba1bc8
  11. kohlerjl commented on Oct 10, 2026

    @kohlerjl

    Unfortunately it seems there is no simple way to 'opt-out' of this breaking change. I have a lot of code that uses dt as a 'timestep' argument or variable, so this default rule is causing naming collisions.

    Is there any way to revert the rule requiring import datetime as dt in my ruff config, without having to fully redefine the other defaults in aliases? While there is the extend-aliases option, there is not an option to remove one of the aliases from the defaults.

  12. ntBre commented on Oct 13, 2026

    @ntBre
    Contributor

    It's a bit of a hack (#17097), but you can actually use extend-aliases to override one of the defaults. Configuring

    [tool.ruff.lint.flake8-import-conventions.extend-aliases]
    datetime = "datetime"

    will allow you to use import datetime. Otherwise, yes, fully defining aliases is the best fix.

  13. xhan80 commented on Oct 16, 2026

    @xhan80

    What's the rationale for doing this, causing thousands if not millions of repos to break?

    It should be opt-in. If the reason for importing it as dt was prevailing, then the datetime library should rename as dt.

  14. ntBre commented on Oct 16, 2026

    @ntBre
    Contributor

    The rationale is given here in the issue, and this change was made in a breaking Ruff release. If you want to revert to the old behavior, you can configurealiases or extend-aliases, as also described above.

  15. xhan80 commented on Oct 16, 2026

    @xhan80

    Thanks for the response. I do feel the cost / benefit tradeoff here is questionable. I'd like to ask you to reconsider and revert the datetime default.

    The "import datetime as dt" was a new convention, not as established as numpy or pandas. The cPython change was only for the examples in their docs, not a style ruling, not a PEP 8 requirement. Even that was contested, for concerns about churns. The PR author also noted: "You can still import datetime if you prefer."

    I don't think an examples-only docs edit should be treated as an endorsement by the wider Python community.

    Almost every ICN user with "import datetime" now has to change either code or config.

    We enabled ICN for its well-established flake8 conventions, not expecting newer, debatable ones to be added to the defaults.

    Reverting now breaks no one.

  16. tjkuson commented on Oct 16, 2026

    @tjkuson
    Contributor

    Speaking as a user of Ruff, I like the new default. It means I no longer have to add the alias myself. I think the alias makes reading and writing Python code a more pleasant experience, especially for those unfamiliar with the nuances of the datetime library. I'd be favour of including the alias by default even if the CPython docs didn't bless it (it seems sufficiently popular to me regardless).

    If we're being comparative, import datetime as dt is probably the alias with the strongest motivation of all the default set. I've always found import numpy as np a pretty funny convention, it only saves three characters per name!

    I also don't share your interpretation of the discussion under the CPython pull request. Regardless, CPython and the official documentation has very different priorities and motivations to that of a third-party linter.

  17. Jackenmen commented on Oct 17, 2026

    @Jackenmen
    Contributor

    Can't say that I've seen this convention before it got added as an alias to Ruff but have seen people use dt as a name for the variable storing a datetime.datetime object which this is incompatible with.
    More broadly, I don't know how I feel about such a simple change to docs.python.org now making it possible to appeal to authority when arguing for this new linter default.

  18. ashrub-holvi commented on Oct 20, 2026

    @ashrub-holvi

    Not sure why people complaining - datetime.datetime was always confusing and annoying (compare with from pathlib import Path). So, definitely good thing and LLMs can help to fix existing code with minimal effort.

  19. kohlerjl commented on Oct 20, 2026

    @kohlerjl

    Not sure why people complaining - datetime.datetime was always confusing and annoying (compare with from pathlib import Path). So, definitely good thing and LLMs can help to fix existing code with minimal effort.

    The fact is this is not currently a widely used 'convention', even if it might have some advantages.

    Unlike other common aliases, such as np or pd, dt is a much more common abbreviation which is often used in existing code in ways that cannot be simply changed. For instance, if it is used as a kwarg in a well established api func(..., *, dt: float): ..., this collides with import datetime as dt and cannot be changed without breaking downstream code.

    We can debate the merits of adopting a new convention, but there is no point in being dismissive for the disruption that a new rule like this causes.

  20. ashrub-holvi commented on Oct 21, 2026

    @ashrub-holvi

    used as a kwarg in a well established api func(..., *, dt: float): ..., this collides with import datetime as dt and cannot be changed without breaking downstream code

    if it's a public API then yes, but if it's something internal I am sure any modern LLM will fix it cheaply enough.

  21. added a commit that references this issue on Oct 28, 2026
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

    breakingpython315rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions