Skip to content

Commit

Permalink
add a few more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Nov 17, 2022
1 parent 68af46b commit b0c596e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSES/FLAKE8BUGBEAR_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Łukasz Langa

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.
6 changes: 5 additions & 1 deletion astpretty.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# type: ignore
from Cython.Compiler.TreeFragment import StringParseContext # pragma: no cover


def _print(name, node, indent): # pragma: no cover
if node is None:
Expand All @@ -19,7 +21,9 @@ def pretty_print(path): # pragma: no cover
from Cython.Compiler.TreeFragment import parse_from_strings
with open(path, encoding='utf-8') as fd:
content = fd.read()
tree = parse_from_strings(path, content)
context = StringParseContext(path)
context.set_language_level(3)
tree = parse_from_strings(path, content, context=context)
_print('tree', tree, indent=0)


Expand Down
58 changes: 58 additions & 0 deletions cython_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
from Cython.Compiler.TreeFragment import StringParseContext
import Cython
from Cython.Compiler.ExprNodes import GeneratorExpressionNode
from Cython.Compiler.ExprNodes import SimpleCallNode
from Cython.Compiler.ExprNodes import AttributeNode
from Cython.Compiler.ExprNodes import SetNode
from Cython.Compiler.ExprNodes import UnicodeNode, IntNode, FloatNode
from Cython.Compiler.ExprNodes import ComprehensionNode
from Cython.Compiler.ExprNodes import PrimaryCmpNode
from Cython.Compiler.ExprNodes import ComprehensionAppendNode
from Cython.Compiler.ExprNodes import DictComprehensionAppendNode
from Cython.Compiler.ExprNodes import TupleNode
Expand Down Expand Up @@ -83,6 +88,8 @@
'W5',
))

CONSTANT_NODE = (UnicodeNode, IntNode, FloatNode)


class NodeParent(NamedTuple):
node: Node
Expand Down Expand Up @@ -484,6 +491,57 @@ def _traverse_file(
)
ret = 1

if (
isinstance(node, PrimaryCmpNode)
and isinstance(node.operand1, CONSTANT_NODE)
and isinstance(node.operand2, CONSTANT_NODE)
and not skip_check
):
assert violations is not None
violations.append(
(
node.pos[1], node.pos[2]+1,
'Comparison between constants',
),
)
ret = 1

if (
isinstance(node, SetNode)
and not skip_check
):
assert violations is not None
counts: MutableMapping[object, int] = collections.Counter()
for _arg in node.args:
if hasattr(_arg, 'value'):
counts[_arg.value] += 1
if counts and max(counts.values()) > 1:
violations.append(
(
node.pos[1], node.pos[2]+1,
'Repeated element in set',
),
)
ret = 1

if (
isinstance(node, SimpleCallNode)
and isinstance(node.function, AttributeNode)
and not skip_check
):
assert violations is not None
if node.function.attribute in {'strip', 'rstrip', 'lstrip'}:
if node.args and isinstance(node.args[0], UnicodeNode):
if len(set(node.args[0].value)) != len(node.args[0].value):
violations.append(
(
node.pos[1], node.pos[2]+1,
f'Using \'{node.function.attribute}\' with '
'repeated elements',
),
)
ret = 1

if isinstance(node, (NameNode, CSimpleBaseTypeNode)):
# do we need node.module_path?
names.append(Token(node.name, *node.pos[1:]))
Expand Down
77 changes: 77 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ def test_shadows_import(
assert ret == 1


@pytest.mark.parametrize(
'src, expected',
[
(
'{0, 0, 1}\n',
't.py:1:2: Repeated element in set\n',
),
],
)
def test_repeated_set_element(
capsys: Any,
src: str,
expected: str,
) -> None:
ret = _main(src, 't.py', no_pycodestyle=True)
out, _ = capsys.readouterr()
assert out == expected
assert ret == 1


@pytest.mark.parametrize(
'src, expected',
[
Expand Down Expand Up @@ -252,6 +272,58 @@ def test_always_true(
assert ret == 1


@pytest.mark.parametrize(
'src, expected',
[
(
'if 0 == 0: pass\n',
't.py:1:6: Comparison between constants\n',
),
(
'if "0" == "0": pass\n',
't.py:1:8: Comparison between constants\n',
),
],
)
def test_constants_comparison(
capsys: Any,
src: str,
expected: str,
) -> None:
ret = _main(src, 't.py', no_pycodestyle=True)
out, _ = capsys.readouterr()
assert out == expected
assert ret == 1


@pytest.mark.parametrize(
'src, expected',
[
(
'mystring.strip("aab")\n',
"t.py:1:15: Using 'strip' with repeated elements\n",
),
(
'mystring.lstrip("aab")\n',
"t.py:1:16: Using 'lstrip' with repeated elements\n",
),
(
'mystring.rstrip("aab")\n',
"t.py:1:16: Using 'rstrip' with repeated elements\n",
),
],
)
def test_strip_repeated_elements(
capsys: Any,
src: str,
expected: str,
) -> None:
ret = _main(src, 't.py', no_pycodestyle=True)
out, _ = capsys.readouterr()
assert out == expected
assert ret == 1


@pytest.mark.parametrize(
'src, expected',
[
Expand Down Expand Up @@ -409,6 +481,11 @@ def test_pycodestyle(tmpdir: Any, capsys: Any) -> None:
' def foo(a):\n'
' return b * a\n'
' lst.append(foo(a))\n',
'{0: 1, 1: 2}\n',
'{0, 1, 2}\n',
'{0, f.b}\n',
'mystring.rstrip("abc")\n',
'mystring.rstrip(suffix)\n',
],
)
def test_noop(capsys: Any, src: str) -> None:
Expand Down

0 comments on commit b0c596e

Please sign in to comment.