A flake8 plugin to ban PEP-420 implicit namespace packages.
Project description
A flake8 plugin to ban PEP-420 implicit namespace packages.
Requirements
Python 3.8 to 3.12 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.
Linting a Django project? Check out my book Boost Your Django DX which covers Flake8 and many other code quality tools.
Rationale
Implicit namespace packages are directories of Python files without an __init__.py. They’re valid and importable, but they break many tools, such as:
unittest test discovery (and by extension, Django’s test runner)
Mypy without its –namespace-packages option
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
Types may never be checked
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
File details
Details for the file flake8_no_pep420-2.7.0.tar.gz
.
File metadata
- Download URL: flake8_no_pep420-2.7.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1b6db4de355b87f8189714e68785a7c6a2d61d94f3e937b6cee336953f7d703 |
|
MD5 | b498baf7bcfb44bc98d011df4af2ef4e |
|
BLAKE2b-256 | 1a568982259cb646a7f13ebe7781db7227e86291cf64c08b3eb9aba247b25901 |
File details
Details for the file flake8_no_pep420-2.7.0-py3-none-any.whl
.
File metadata
- Download URL: flake8_no_pep420-2.7.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 018bbe0fba039444689d02a0857310b62b90ff9bf737aff27e0b847ec7eb9ba3 |
|
MD5 | 3d1cb8f1fbcdac47d045f9f7d5bf2149 |
|
BLAKE2b-256 | 602e16f8f320a662c14132b67f11601fdd3a5834eed9abcbca45e29b7f0df589 |