Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions wemake_python_styleguide/logic/naming/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions wemake_python_styleguide/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
22 changes: 22 additions & 0 deletions wemake_python_styleguide/violations/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down