Skip to main content

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

Project description

flake8-datetime-import

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: 5.0.4
    hooks:
    -   id: flake8
        additional_dependencies: [
          flake8-datetime-import==0.1.0,
        ]

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.1.0a1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file flake8-datetime-import-0.1.0a1.tar.gz.

File metadata

File hashes

Hashes for flake8-datetime-import-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 d61e8b0a002181be2e3318d89491a9c7cb31326e352f24e0eb4d86202e6a9c7b
MD5 c67f808efbb4870405d47a86691aa206
BLAKE2b-256 3a52cd7939e8a7e387d4d1745119b411c6552a6cbaa42fb55ef5cf2b5fe41cf1

See more details on using hashes here.

File details

Details for the file flake8_datetime_import-0.1.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_datetime_import-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 11b40ca6fdd759163b1a80871b9ae09f82cc9823bdfe5cec9cf3a048d315a629
MD5 1622b3a8ec82c6449f2ab832160393a1
BLAKE2b-256 9b43a540730c0569df56ebf37a8f9c0b50f77608ee93a9174bc6e9e8420ab018

See more details on using hashes here.

Supported by

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