Skip to content

Commit 2734c66

Browse files
committed
Remove checks for typing.NamedTuple as it disallows explicitly setting __slots__
1 parent e63d4a6 commit 2734c66

File tree

5 files changed

+5
-47
lines changed

5 files changed

+5
-47
lines changed

doc-source/usage.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Flake8 codes
1313
SLOT000
1414
SLOT001
1515
SLOT002
16-
SLOT003
1716

1817
For subclasses of immutable types it is recommended to use ``__slots__ = ()`` to keep the memory usage down.
1918
For subclasses of :class:`str` it is possible to define other variables which may be set on instances. For example:
@@ -22,7 +21,7 @@ For subclasses of :class:`str` it is possible to define other variables which ma
2221
2322
class MyString(str):
2423
25-
__slots__ = ("a_variable")
24+
__slots__ = ("a_variable", )
2625
2726
which will allow a value to be assigned to ``a_variable``.
2827

flake8_slots/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@
3232

3333
# 3rd party
3434
import flake8_helper
35-
from domdf_python_tools.paths import PathPlus
3635

3736
__author__: str = "Dominic Davis-Foster"
3837
__copyright__: str = "2020-2021 Dominic Davis-Foster"
3938
__license__: str = "MIT License"
4039
__version__: str = "0.1.0"
4140
__email__: str = "dominic@davis-foster.co.uk"
4241

43-
__all__ = ["Visitor", "Plugin", "SLOT000", "SLOT001", "SLOT002", "SLOT003"]
42+
__all__ = ["Visitor", "Plugin", "SLOT000", "SLOT001", "SLOT002"]
4443

4544
SLOT000 = "SLOT000 Define __slots__ for subclasses of str"
4645
SLOT001 = "SLOT001 Define __slots__ for subclasses of tuple"
4746
SLOT002 = "SLOT002 Define __slots__ for subclasses of collections.namedtuple"
48-
SLOT003 = "SLOT003 Define __slots__ for subclasses of typing.NamedTuple"
47+
# SLOT003 # Don't reuse
4948

5049
# TODO: custom immutable types - config option and SLOT004
5150

@@ -101,9 +100,8 @@ def visit_ClassDef(self, node: ast.ClassDef): # noqa: D102
101100
is_tuple = False
102101

103102
is_collections_namedtuple = "namedtuple" in bases or "collections.namedtuple" in bases # SLOT002
104-
is_typing_namedtuple = "NamedTuple" in bases or "typing.NamedTuple" in bases # SLOT003
105103

106-
if not any([is_str, is_tuple, is_collections_namedtuple, is_typing_namedtuple]):
104+
if not any([is_str, is_tuple, is_collections_namedtuple]):
107105
return
108106

109107
body_visitor = ClassBodyVisitor()
@@ -116,8 +114,6 @@ def visit_ClassDef(self, node: ast.ClassDef): # noqa: D102
116114
self.report_error(node, SLOT001)
117115
if is_collections_namedtuple:
118116
self.report_error(node, SLOT002)
119-
if is_typing_namedtuple:
120-
self.report_error(node, SLOT003)
121117

122118

123119
class Plugin(flake8_helper.Plugin[Visitor]):

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
domdf-python-tools>=1.8.0
21
flake8>=3.7
32
flake8-helper>=0.1.1

tests/test_errors.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
# this package
8-
from flake8_slots import SLOT000, SLOT001, SLOT002, SLOT003, Plugin, Visitor
8+
from flake8_slots import SLOT000, SLOT001, SLOT002, Plugin, Visitor
99

1010
string_a = "\nclass String(str): pass"
1111
string_b = "import builtins\nclass String(builtins.str): pass"
@@ -17,20 +17,6 @@
1717
namedtuple_a = "import collections\nclass Person(collections.namedtuple('foo', 'name, age')): pass"
1818
namedtuple_b = "from collections import namedtuple\nclass Person(namedtuple('foo', 'name, age')): pass"
1919

20-
typing_namedtuple_a = """\
21-
import typing
22-
class Person(typing.NamedTuple):
23-
name: str
24-
age: int
25-
"""
26-
27-
typing_namedtuple_b = """\
28-
from typing import NamedTuple
29-
class Person(NamedTuple):
30-
name: str
31-
age: int
32-
"""
33-
3420
sources = pytest.mark.parametrize(
3521
"source, error",
3622
[
@@ -43,8 +29,6 @@ class Person(NamedTuple):
4329
(typing_tuple_c, SLOT001),
4430
(namedtuple_a, SLOT002),
4531
(namedtuple_b, SLOT002),
46-
(typing_namedtuple_a, SLOT003),
47-
(typing_namedtuple_b, SLOT003),
4832
]
4933
)
5034

tests/test_good_code.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,13 @@ class Person(namedtuple('foo', 'name, age')):
2626
typing_namedtuple_a = """\
2727
import typing
2828
class Person(typing.NamedTuple):
29-
__slots__ = ()
3029
name: str
3130
age: int
3231
"""
3332

3433
typing_namedtuple_b = """\
3534
from typing import NamedTuple
3635
class Person(NamedTuple):
37-
__slots__ = ()
38-
name: str
39-
age: int
40-
"""
41-
42-
typing_namedtuple_c = """\
43-
from typing import NamedTuple
44-
class Person(NamedTuple):
45-
__slots__: Tuple[str] = ()
46-
name: str
47-
age: int
48-
"""
49-
50-
typing_namedtuple_d = """\
51-
from typing import NamedTuple
52-
class Person(NamedTuple):
53-
__slots__: List[str] = []
5436
name: str
5537
age: int
5638
"""
@@ -76,8 +58,6 @@ class Compound(Counter):
7658
namedtuple_c,
7759
typing_namedtuple_a,
7860
typing_namedtuple_b,
79-
typing_namedtuple_c,
80-
typing_namedtuple_d,
8161
mutable,
8262
]
8363
)

0 commit comments

Comments
 (0)