From 8f581151b2d36b71bc581b429e8f9cc8a73e5b62 Mon Sep 17 00:00:00 2001 From: Timur Valiev Date: Wed, 27 Aug 2025 11:54:50 +0300 Subject: [PATCH 1/2] docs: add configuration examples for `known-enum-bases` option --- .../logic/naming/enums.py | 12 ++++++---- wemake_python_styleguide/options/config.py | 3 +-- wemake_python_styleguide/violations/naming.py | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/wemake_python_styleguide/logic/naming/enums.py b/wemake_python_styleguide/logic/naming/enums.py index 7d097d673..bccbc77b7 100644 --- a/wemake_python_styleguide/logic/naming/enums.py +++ b/wemake_python_styleguide/logic/naming/enums.py @@ -86,9 +86,13 @@ def has_enum_like_base_with_config( enum_bases = config.known_enum_bases if enum_bases: - normalized: tuple[str, ...] = tuple({ - name for base in enum_bases for name in (base, base.split('.')[-1]) - }) - return _has_one_of_base_classes(defn, normalized) + return _has_one_of_base_classes( + defn, + tuple({ + name + for base in enum_bases + for name in (base, base.split('.')[-1]) + }), + ) return False diff --git a/wemake_python_styleguide/options/config.py b/wemake_python_styleguide/options/config.py index 47c324ea5..f5ef4eac0 100644 --- a/wemake_python_styleguide/options/config.py +++ b/wemake_python_styleguide/options/config.py @@ -289,8 +289,7 @@ class Configuration: _Option( '--known-enum-bases', defaults.KNOWN_ENUM_BASES, - 'List of additional enum-like base class names that should be ' - 'treated as enums.', + 'List of additional enum-like base class names.', type=String, comma_separated_list=True, ), diff --git a/wemake_python_styleguide/violations/naming.py b/wemake_python_styleguide/violations/naming.py index 859fe4933..548695b4d 100644 --- a/wemake_python_styleguide/violations/naming.py +++ b/wemake_python_styleguide/violations/naming.py @@ -453,6 +453,20 @@ class UpperCaseAttributeViolation(ASTViolation): Default: :str:`wemake_python_styleguide.options.defaults.KNOWN_ENUM_BASES` + You can configure custom enum-like base classes that should be treated + as enums: + + .. code:: ini + + [flake8] + known-enum-bases = BaseEnum, constants.AnotherBaseEnum + + Or via command line: + + .. code:: bash + + flake8 --known-enum-bases=BaseEnum,constants.AnotherBaseEnum ./ + Example:: # Correct: @@ -465,6 +479,14 @@ class Color(enum.Enum): WHITE = 0 LIGHT_GRAY = 1 + # Correct with `--known-enum-bases=BaseEnum` option: + class BaseEnum(enum.Enum): + pass + + class MyEnum(BaseEnum): + HELLO = 'hello' + WORLD = 'world' + # Wrong: class A: MY_CONSTANT = 42 From eec806476f307686d585a91ec796baba25251fa7 Mon Sep 17 00:00:00 2001 From: Timur Valiev Date: Wed, 17 Sep 2025 12:28:32 +0500 Subject: [PATCH 2/2] Update `CHANGELOG.md` --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a96fa34a8..6c611494c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,14 @@ Semantic versioning in our case means: change the client facing API, change code conventions significantly, etc. +## WIP + +### Misc + +- Minor refactoring `wemake_python_styleguide.logic.naming.enums.has_enum_like_base_with_config`, #3518 +- Improves `--known-enum-bases` option help text and documentation, #3518 + + ## 1.5.0 ### Features