Skip to content

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Oct 4, 2025

% pre-commit autoupdate

[https://github.com/pre-commit/pre-commit-hooks] updating v5.0.0 -> v6.0.0
[https://github.com/crate-ci/typos] updating v1.32.0 -> v1
[https://github.com/asottile/pyupgrade] already up to date!
[https://github.com/adamchainz/django-upgrade] updating 1.25.0 -> 1.28.0
[https://github.com/python-poetry/poetry] updating 1.8.0 -> 2.2.1
[https://github.com/astral-sh/ruff-pre-commit] updating v0.11.12 -> v0.13.3

% pre-commit run --all-files


There are lots of perfectly valid reasons to ignore:
% ruff rule PLC0415

import-outside-top-level (PLC0415)

Derived from the Pylint linter.

What it does

Checks for import statements outside of a module's top-level scope, such
as within a function or class definition.

Why is this bad?

PEP 8 recommends placing imports not only at the top-level of a module,
but at the very top of the file, "just after any module comments and
docstrings, and before module globals and constants."

import statements have effects that are global in scope; defining them at
the top level has a number of benefits. For example, it makes it easier to
identify the dependencies of a module, and ensures that any invalid imports
are caught regardless of whether a specific function is called or class is
instantiated.

An import statement would typically be placed within a function only to
avoid a circular dependency, to defer a costly module load, or to avoid
loading a dependency altogether in a certain runtime environment.

Example

def print_python_version():
    import platform

    print(platform.python_version())

Use instead:

import platform


def print_python_version():
    print(platform.python_version())

See also

This rule will ignore import statements configured in
lint.flake8-tidy-imports.banned-module-level-imports
if the rule banned-module-level-imports is enabled.

Copy link

codecov bot commented Oct 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pfouque pfouque merged commit d032248 into django-commons:main Oct 4, 2025
5 of 9 checks passed
@cclauss cclauss deleted the pre-commit_autoupdate_2025-10-04 branch October 4, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants