A flake8 plugin to ban PEP-420 implicit namespace packages.
Project description
A flake8 plugin to ban PEP-420 implicit namespace packages.
Linting a Django project? Check out my book Boost Your Django DX which covers Flake8 and many other code quality tools.
Requirements
Python 3.9 to 3.13 supported.
Installation
First, install with pip:
python -m pip install flake8-no-pep420
Second, if you define Flake8’s select setting, add the INP prefix to it. Otherwise, the plugin should be active by default.
Rationale
Implicit namespace packages are directories of Python files without an __init__.py. They’re valid and importable, but they break some tools:
unittest test discovery, unless -s (--start-directory) points to the namespace package. By extension, this affects Django’s test runner too.
Coverage.py, unless report.include_namespace_packages is enabled.
pytest (Issue #5147)
In most cases, tools fail silently, which can lead to a false sense of security:
Tests may look legitimate but never run
Code may be untested but not appear in coverage statistics
PEP-420’s algorithm is non-trivial which is probably why such tools haven’t (yet) implemented it.
Rules
INP001: File is part of an implicit namespace package. Add __init__.py?
flake8-no-pep420 will trigger this on the first line of any file that sits in a directory without an __init__.py file.
Often projects have a few root files not in packages, for which an __init__.py file should not be added. For example, Django projects normally have a manage.py file in the root of their repository. In these cases you can ignore the INP001 error. It’s possible to use # noqa: INP001 to ignore the error in-line, but this isn’t possible if the first line is a shebang, such as in Django’s manage.py. In such cases it’s preferable to use Flake8’s per-file-ignores option, for example in setup.cfg:
[flake8]
# ...
per-file-ignores =
manage.py:INP001
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flake8_no_pep420-2.8.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c00f88cab94724ccce916c2617b49b8a6c8fbc1171de8423b0a856a8ecbebf1 |
|
MD5 | e5e13504614f2c80af6babfde5759999 |
|
BLAKE2b-256 | 9dcfefc2ecaca3283dcd1c6d3429191c870e03cf7805e6f98d31e09238d37949 |