Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbidden decorators #60

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
1 change: 0 additions & 1 deletion pyeo/features/code_free_ctor_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class CodeFreeCtorVisitor(ast.NodeVisitor):

def __init__(self, options) -> None:
"""Ctor."""
self._options = options
self.problems: list[tuple[int, int, str]] = []

def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802, WPS231, C901
Expand Down
45 changes: 45 additions & 0 deletions pyeo/features/forbidden_decorator_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# The MIT License (MIT).
#
# Copyright (c) 2023-2025 Almaz Ilaletdinov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

"""ForbiddenDecoratorVisitor."""

import ast
from typing import final


@final
class ForbiddenDecoratorVisitor(ast.NodeVisitor):
"""ForbiddenDecoratorVisitor."""

def __init__(self, options) -> None:
"""Ctor."""
self.problems: list[tuple[int, int, str]] = []

def visit_FunctionDef(self, node: ast.FunctionDef) -> None: # noqa: N802, WPS231, C901
"""Visit by methods.

:param node: ast.ClassDef
"""
for deco in node.decorator_list:
if deco.id == 'staticmethod':
self.problems.append((node.lineno, node.col_offset, 'PEO400 Staticmethod is forbidden'))
self.generic_visit(node)
2 changes: 1 addition & 1 deletion pyeo/fk_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import ast
from collections.abc import Generator
from typing import final, List
from typing import List, final


@final
Expand Down
2 changes: 1 addition & 1 deletion pyeo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from flake8.options.manager import OptionManager

from pyeo.features.code_free_ctor_visitor import CodeFreeCtorVisitor
from pyeo.features.no_mutable_objects import NoMutableObjectsVisitor
from pyeo.features.no_er_suffix import NoErSuffix
from pyeo.features.no_mutable_objects import NoMutableObjectsVisitor


@final
Expand Down
52 changes: 52 additions & 0 deletions tests/features/test_forbidden_decorator_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# The MIT License (MIT).
#
# Copyright (c) 2023-2025 Almaz Ilaletdinov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

from pyeo.features.forbidden_decorator_visitor import ForbiddenDecoratorVisitor


def test_valid(plugin_run, options_factory):
got = plugin_run(
'\n'.join([
'class HttpHouse(House):',
'',
' def double_cost(self):',
' return self.cost * 2',
]),
[ForbiddenDecoratorVisitor(options_factory())]
)

assert not got


def test_staticmethod(plugin_run, options_factory):
got = plugin_run(
'\n'.join([
'class HttpHouse(House):',
'',
' @staticmethod',
' def double_cost(cost):',
' return cost * 2',
]),
[ForbiddenDecoratorVisitor(options_factory())]
)

assert got == [(4, 4, 'PEO400 Staticmethod is forbidden')]
Loading