Skip to main content

This is how python package should look like!

Project description

flake8-no-private-methods

test Python Version

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

MIT

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.

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_no_private_methods-0.0.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_no_private_methods-0.0.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file flake8_no_private_methods-0.0.2.tar.gz.

File metadata

  • Download URL: flake8_no_private_methods-0.0.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.0 CPython/3.13.1 Linux/6.17.0-1010-azure

File hashes

Hashes for flake8_no_private_methods-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e01b21ddf408aa9f47f5da20b6aab2a7cec0f753a25a817f4ee4d14ed00f1817
MD5 26a4c8ccfc35e243c9a8ebf424c5088d
BLAKE2b-256 cd52719003354b01a324846cce0a4e2822c86cd39fc4c1f1b4ae2aa092bf0970

See more details on using hashes here.

File details

Details for the file flake8_no_private_methods-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_no_private_methods-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 100cfb263eaf81bec792832eaad8e42224647932d30b97574ac4ef927f35dd6c
MD5 e285d307b099d71d4266d2ea4f86d4d2
BLAKE2b-256 b229acc6b3c8c834e925c9c45c35d6e8aa2d917c2c5e521ff147436cce223e6b

See more details on using hashes here.

Supported by

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