Skip to content

Commit e7ca5b0

Browse files
committed
Don't warn about missing __slots__ for string Enums
1 parent 192faac commit e7ca5b0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

flake8_slots/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
# stdlib
3030
import ast
31+
import re
3132
from typing import Iterator, Union
3233

3334
# 3rd party
@@ -81,6 +82,9 @@ def resolve_dotted_name(name: Union[str, ast.Name, ast.Attribute, ast.Call, ast.
8182
yield from resolve_dotted_name(name.func) # type: ignore
8283

8384

85+
_enum_re = re.compile(r"\.?Enum")
86+
87+
8488
class Visitor(flake8_helper.Visitor):
8589
"""
8690
AST visitor to identify missing ``__slots__`` for subclasses of immutable types.
@@ -90,7 +94,11 @@ def visit_ClassDef(self, node: ast.ClassDef): # noqa: D102
9094

9195
bases = ['.'.join(resolve_dotted_name(base)) for base in node.bases] # type: ignore
9296

93-
is_str = "str" in bases or "builtins.str" in bases # SLOT000
97+
is_str = False
98+
99+
if "str" in bases or "builtins.str" in bases: # SLOT000
100+
if not any(map(_enum_re.match, bases)):
101+
is_str = True
94102

95103
if "tuple" in bases or "builtins.tuple" in bases: # SLOT001
96104
is_tuple = True

tests/test_good_code.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class Compound(Counter):
4343
pass
4444
"""
4545

46+
enum = """\
47+
from enum import Enum
48+
class Protocols(str, Enum):
49+
HTTP = "http"
50+
FTP = "ftp"
51+
SSH = "SSH"
52+
"""
53+
4654
sources = pytest.mark.parametrize(
4755
"source",
4856
[
@@ -59,6 +67,7 @@ class Compound(Counter):
5967
typing_namedtuple_a,
6068
typing_namedtuple_b,
6169
mutable,
70+
enum,
6271
]
6372
)
6473

0 commit comments

Comments
 (0)