Skip to main content

Check that class attributes are defined at the top level

Project description

flake8-define-class-attributes

PyPI - Python Version PyPI PyPI - License pre-commit.ci status

Check that class attributes are defined at the "top level" of your class. This is in the same spirit as Pylint's W0201 (attribute-defined-outside-init) error, expanded to allow attribute declaration at the class level.

For example:

Failing Code:

class Spaceship:
    def reset_location(self) -> None:
        self.xy = (0, 0)  # CLA001

Passing Code:

class Spaceship:
    def __init__(self):
        self.xy = (0, 0)

    def reset_location(self):
        self.xy = (0, 0)

# --OR--

class Spaceship:
    xy: tuple[int, int]

    def reset_location(self):
        self.xy = (0, 0)

When using dataclasses, __post_init__ methods are also considered when checking that an attribute has been defined.

Installation

Install from PyPi with your favorite pip invocation:

$ pip install flake8-define-class-attributes

It will then be run automatically as part of flake8.

You can verify it's being picked up by invoking the following in your shell:

$ flake8 --version
7.3.0 (flake8-define-class-attributes: 0.2.2, mccabe: 0.7.0, pycodestyle: 2.14.0, pyflakes: 3.4.0) CPython 3.13.5 on Darwin

Warnings

ID Description
CLA001 Attribute <name> not defined prior to assignment

Python Version Support

Beginning with Python 3.11, a best attempt is made to support Python versions until they reach EOL, after which support will be formally dropped by the next minor or major release of this package, whichever arrives first. The status of Python versions can be found here.

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_define_class_attributes-0.2.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file flake8_define_class_attributes-0.2.2.tar.gz.

File metadata

File hashes

Hashes for flake8_define_class_attributes-0.2.2.tar.gz
Algorithm Hash digest
SHA256 688d65c7fd41ae24804c0c1932e982117fef5c7554d2198821a92854c669a585
MD5 75aa7d8c72a1881a01263eb664f7d983
BLAKE2b-256 6527e35b2257f6ead574d21b8832b1c2a8c1e6bb221652705bd9754f3b7cf52a

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_define_class_attributes-0.2.2.tar.gz:

Publisher: pypi_release.yml on sco1/flake8-define-class-attributes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flake8_define_class_attributes-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_define_class_attributes-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1a6350467e04ae75a17bee268224c94cbb06da706f9c8c442ee039d9609f7b8
MD5 c37d6f4209155e33d18921c38a10a511
BLAKE2b-256 12b2805880f66deddb3cda86370c6dd23a67288e641e282c0cc18ad9db364d4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_define_class_attributes-0.2.2-py3-none-any.whl:

Publisher: pypi_release.yml on sco1/flake8-define-class-attributes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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