Skip to content

Commit

Permalink
Rename "CLST" to "CSM" in codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
atollk committed Jul 14, 2023
1 parent 834dcc7 commit 65314b9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 40 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ flake8 plugin that checks rules regarding the staticmethod and classmethod decor

## Options

The plugin offers one flag, `--select_clst1`, accepting a list of error
The plugin offers one flag, `--select_csm1`, accepting a list of error
codes (see below) to be enabled. By default, the enabled errors
are `CLST101` and `CLST131`.
are `CSM101` and `CSM131`.

## Error Codes

### CLST100
### CSM100

`@staticmethod` should not be used.

### CLST101
### CSM101

A method marked as `@staticmethod` should not reference the class it
is defined in. Use `@classmethod` otherwise.
Expand All @@ -38,7 +38,7 @@ class MyClass:
return cls.__name__
```

### CLST102
### CSM102

Do not inherit and override a method marked as `@staticmethod`.

Expand All @@ -63,11 +63,11 @@ class MyClass:
return cls.__name__
```

### CLST130
### CSM130

`@classmethod` should not be used.

### CLST131
### CSM131

A method marked as `@classmethod` should access the parameter `cls`.
Use `@staticmethod` otherwise.
Expand All @@ -88,7 +88,7 @@ class MyClass:
return "MyClass"
```

### CLST132
### CSM132
A method marked as `@classmethod` should not reference the class it
is defined in. Use the `cls` parameter.

Expand Down
30 changes: 15 additions & 15 deletions flake8_classmethod_staticmethod/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import flake8.options.manager

DEFAULT_SELECT = [
"CLST101",
"CLST131",
"CSM101",
"CSM131",
]

PYTHON_38 = sys.version_info >= (3, 8)
Expand All @@ -28,7 +28,7 @@ def __init__(self, tree: ast.AST):
@staticmethod
def add_options(option_manager: flake8.options.manager.OptionManager):
option_manager.add_option(
"--select_clst1",
"--select_csm1",
type=str,
comma_separated_list=True,
default=DEFAULT_SELECT,
Expand All @@ -44,7 +44,7 @@ def parse_options(
extra_args,
):
cls.enabled_errors = [
int(option[4:]) for option in options.select_clst1
int(option[4:]) for option in options.select_csm1
]

def run(self) -> Iterable[Tuple[int, int, str, type]]:
Expand All @@ -66,16 +66,16 @@ def _check_function_node(self, fn_node: ast.FunctionDef, classname: str):
)
if is_staticmethod:
if 100 in self.enabled_errors:
yield from _clst100(fn_node)
yield from _csm100(fn_node)
if 101 in self.enabled_errors:
yield from _clst101(fn_node, classname)
yield from _csm101(fn_node, classname)
if is_classmethod:
if 130 in self.enabled_errors:
yield from _clst130(fn_node)
yield from _csm130(fn_node)
if 131 in self.enabled_errors:
yield from _clst131(fn_node)
yield from _csm131(fn_node)
if 132 in self.enabled_errors:
yield from _clst132(fn_node, classname)
yield from _csm132(fn_node, classname)


ERROR_MESSAGES = {
Expand All @@ -98,16 +98,16 @@ def _error_tuple(error_code: int, node: ast.AST) -> Tuple[int, int, str, type]:
return (
node.lineno,
node.col_offset,
f"CLST{error_code} {ERROR_MESSAGES[error_code]}",
f"CSM{error_code} {ERROR_MESSAGES[error_code]}",
Checker,
)


def _clst100(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
def _csm100(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
yield _error_tuple(100, node)


def _clst101(
def _csm101(
node: ast.FunctionDef, classname: str
) -> Iterable[Tuple[int, int, str, type]]:
references_class = any(
Expand All @@ -118,18 +118,18 @@ def _clst101(
yield _error_tuple(101, node)


def _clst130(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
def _csm130(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
yield _error_tuple(130, node)


def _clst131(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
def _csm131(node: ast.FunctionDef) -> Iterable[Tuple[int, int, str, type]]:
if node.args.args:
cls_param = node.args.args[0].arg
if not _function_uses_identifier(node, cls_param):
yield _error_tuple(131, node)


def _clst132(
def _csm132(
node: ast.FunctionDef, classname: str
) -> Iterable[Tuple[int, int, str, type]]:
if _function_uses_identifier(node, classname):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ zip_safe = False

[options.entry_points]
flake8.extension =
CLST1 = flake8_classmethod_staticmethod.checker:Checker
CSM1 = flake8_classmethod_staticmethod.checker:Checker

6 changes: 3 additions & 3 deletions tests/test_clst100.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from tests.util import BaseTest


class Test_CLST100(BaseTest):
class Test_CSM100(BaseTest):
def error_code(self) -> str:
return "CLST100"
return "CSM100"

def test_pass_1(self):
code = """
Expand All @@ -27,4 +27,4 @@ def bar():
pass
"""
result = self.run_flake8(code)
self.assert_error_at(result, "CLST100", 3 if PYTHON_38 else 2, 5)
self.assert_error_at(result, "CSM100", 3 if PYTHON_38 else 2, 5)
6 changes: 3 additions & 3 deletions tests/test_clst101.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from tests.util import BaseTest


class Test_CLST101(BaseTest):
class Test_CSM101(BaseTest):
def error_code(self) -> str:
return "CLST101"
return "CSM101"

def test_pass_1(self):
code = """
Expand Down Expand Up @@ -37,4 +37,4 @@ def bar():
return Foo.__name__
"""
result = self.run_flake8(code)
self.assert_error_at(result, "CLST101", 3 if PYTHON_38 else 2, 5)
self.assert_error_at(result, "CSM101", 3 if PYTHON_38 else 2, 5)
6 changes: 3 additions & 3 deletions tests/test_clst130.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from tests.util import BaseTest


class Test_CLST130(BaseTest):
class Test_CSM130(BaseTest):
def error_code(self) -> str:
return "CLST130"
return "CSM130"

def test_pass_1(self):
code = """
Expand All @@ -27,4 +27,4 @@ def bar(cls):
pass
"""
result = self.run_flake8(code)
self.assert_error_at(result, "CLST130", 3 if PYTHON_38 else 2, 5)
self.assert_error_at(result, "CSM130", 3 if PYTHON_38 else 2, 5)
6 changes: 3 additions & 3 deletions tests/test_clst131.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from tests.util import BaseTest


class Test_CLST131(BaseTest):
class Test_CSM131(BaseTest):
def error_code(self) -> str:
return "CLST131"
return "CSM131"

def test_pass_1(self):
code = """
Expand Down Expand Up @@ -40,4 +40,4 @@ def bar(cls):
return 123
"""
result = self.run_flake8(code)
self.assert_error_at(result, "CLST131", 3 if PYTHON_38 else 2, 5)
self.assert_error_at(result, "CSM131", 3 if PYTHON_38 else 2, 5)
6 changes: 3 additions & 3 deletions tests/test_clst132.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from tests.util import BaseTest


class Test_CLST132(BaseTest):
class Test_CSM132(BaseTest):
def error_code(self) -> str:
return "CLST132"
return "CSM132"

def test_pass_1(self):
code = """
Expand Down Expand Up @@ -41,4 +41,4 @@ def bar(cls):
return Foo.__name__
"""
result = self.run_flake8(code)
self.assert_error_at(result, "CLST132", 3 if PYTHON_38 else 2, 5)
self.assert_error_at(result, "CSM132", 3 if PYTHON_38 else 2, 5)
2 changes: 1 addition & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _flake8dir(self, flake8dir):

def run_flake8(self, code: str) -> List[ReportedMessage]:
self.flake8dir.make_example_py(textwrap.dedent(code))
args = [f"--select_clst1={self.error_code()}"]
args = [f"--select_csm1={self.error_code()}"]
result = self.flake8dir.run_flake8(args)
return [ReportedMessage.from_raw(report) for report in result.out_lines]

Expand Down

0 comments on commit 65314b9

Please sign in to comment.