Skip to main content

A plugin for flake8 which enforces importing `datetime as dt` and `time as tm`.

Project description

flake8-datetime-import

PyPI version PyPI - Python Version PyPI - Wheel codecov pre-commit.ci status

flake8-datetime-import is an opinionated plugin which aims to reduce confusing or inconsistent usage of Python's datetime module. It checks that datetime and time are imported as modules and aliased like:

import datetime as dt
import time as tm

Installation

pip install flake8-datetime-import

flake8 codes

Code Description
DTI100 from datetime import ... is not allowed. datetime must be imported as a module.
DTI101 datetime imported without aliasing as dt. Expected import datetime as dt.
DTI200 from time import ... is not allowed. time must be imported as a module.
DTI201 time imported without aliasing as tm. Expected import time as tm.

Rationale

datetime and time are confusing when encountered in code. Are they modules? Are they classes or functions?

# Bad
import datetime
from datetime import datetime, time, timezone

import time
from time import time, timezone

Consistently importing and aliasing the datetime and time modules helps prevent this ambiguity.

# Good
import datetime as dt
import time as tm

dt.datetime.now()
tm.time()

Importing and namespacing datetime prevents other naming collisions, such as Django's django.utils.timezone:

import datetime as dt

from django.utils import timezone

dt.timezone.utc
timezone.now()

This plugin was inspired by:

  • A talk by @brandon-rhodes at PyCon Canada in which he mentioned this importing strategy
  • Code review fatigue

Other notable mentions of this importing strategy:

pre-commit

To use with pre-commit, add flake8-datetime-import as an additional dependency to flake8.

# .pre-commit-config.yml

-   repo: https://github.com/pycqa/flake8
    rev: 7.3.0
    hooks:
    -   id: flake8
        additional_dependencies: [
          flake8-datetime-import,
        ]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_datetime_import-0.2.0.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_datetime_import-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file flake8_datetime_import-0.2.0.tar.gz.

File metadata

  • Download URL: flake8_datetime_import-0.2.0.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for flake8_datetime_import-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0f7dda7fd75e19d3aa5ac58415b8598b6129d31ed46266f4a45d53eaba9f5db3
MD5 0b0ec28aaae09a40a1f2ca994fd23223
BLAKE2b-256 52bb2dd19083d9b4d2fa48902852b86f0a3af1b6888f1c13b877297ca13ce702

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_datetime_import-0.2.0.tar.gz:

Publisher: publish.yml on marcgibbons/flake8-datetime-import

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flake8_datetime_import-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_datetime_import-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54ba80dc2c136b7a49e81af3476e57489137446ed6c3c1aaf8158f3db60df344
MD5 12207760184e7bcda1293282f7ccec48
BLAKE2b-256 7acc19f60a3a20dec234c81b2dd8edf1cee67507d42102e0af4b526849aeb2cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_datetime_import-0.2.0-py3-none-any.whl:

Publisher: publish.yml on marcgibbons/flake8-datetime-import

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page