flake8-no-private-methods
flake8-no-private-methods is a Flake8 plugin that prohibits the use of private methods in Python code. The plugin is based on the object-oriented programming principle: each private method is a candidate for a new class.
Each Private Static Method Is a Candidate for a New Class
Installation
Install flake8-no-private-methods via pip:
pip install flake8-no-private-methods
Usage
To use flake8-no-private-methods, simply include it in your Flake8 configuration. Run Flake8 as usual, and the plugin will check for the presence of private methods in your code.
flake8 your_code_directory
Example
Problematic code
class Token:
def __init__(self, key, secret):
self.key = key
self.secret = secret
def encoded(self):
return "key=" + self._encode(self.key) + "&secret=" + self._encode(self.secret)
def _encode(self, text): # Private method - violation!
return URLEncoder.encode(text, "UTF-8")
Running flake8 will produce the following error:
your_file.py:8:4: NPM100 private methods forbidden
Correct code
Instead of a private method, create a separate class:
class Encoded:
def __init__(self, raw):
self.raw = raw
def __str__(self):
return URLEncoder.encode(self.raw, "UTF-8")
class Token:
def __init__(self, key, secret):
self.key = key
self.secret = secret
def encoded(self):
return "key=" + str(Encoded(self.key)) + "&secret=" + str(Encoded(self.secret))
Rationale
Private methods in Python reduce code modularity, testability, and reusability. The main problems with private methods:
- Not reusable: private methods cannot be used in other classes
- Not testable: it's difficult to create unit tests for private methods
- Violate single responsibility principle: a class takes on too many responsibilities
The flake8-no-private-methods plugin encourages developers to create separate classes instead of private methods, which leads to:
- Better modularity: each class has a single responsibility
- Improved testability: each class can be tested independently
- Enhanced reusability: functionality can be used in different places
- Cleaner design: code becomes more object-oriented
As Yegor Bugayenko said: "Each private method is a candidate for a new class".
License
Credits
This project was generated with wemake-python-package. Current template version is: 864a62ecb432655249d071e263ac51f053448659. See what is updated since then.
The plugin idea is based on Yegor Bugayenko's article about how each private method is a candidate for a new class.
Release files for flake8-no-private-methods 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flake8_no_private_methods-0.0.2.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flake8_no_private_methods-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.1 kB
Release files / flake8_no_private_methods-0.0.2.tar.gz
| Download URL | flake8_no_private_methods-0.0.2.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e01b21ddf408aa9f47f5da20b6aab2a7cec0f753a25a817f4ee4d14ed00f1817
|
|
BLAKE2b-256 checksum How to use checksums |
cd52719003354b01a324846cce0a4e2822c86cd39fc4c1f1b4ae2aa092bf0970
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.0 CPython/3.13.1 Linux/6.17.0-1010-azure
|
Release files / flake8_no_private_methods-0.0.2-py3-none-any.whl
| Download URL | flake8_no_private_methods-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
100cfb263eaf81bec792832eaad8e42224647932d30b97574ac4ef927f35dd6c
|
|
BLAKE2b-256 checksum How to use checksums |
b229acc6b3c8c834e925c9c45c35d6e8aa2d917c2c5e521ff147436cce223e6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.0 CPython/3.13.1 Linux/6.17.0-1010-azure
|