Skip to content
This repository was archived by the owner on Feb 19, 2023. It is now read-only.

No single letter variables #25

Merged
merged 2 commits into from
Jun 20, 2021
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: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ a linter for pandas usage, please see [pandas-vet](https://github.com/deppen8/pa
| PDF020 | found private import across modules |
| PDF021 | found 'np.bool' or 'np.object' (use 'np.bool_' or 'np.object_' instead) |
| PDF022 | found import from 'numpy.random' |
| PDF023 | found assignment to single-letter variable |
## contributing

See `contributing.md` for how to get started.
Expand Down
24 changes: 24 additions & 0 deletions pandas_dev_flaker/_plugins_tree/single_letter_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ast
from typing import Iterator, Tuple

from pandas_dev_flaker._data_tree import State, register

MSG = "PDF023 found assignment to single-letter variable"


@register(ast.Assign)
def visit_Assign(
state: State,
node: ast.Assign,
parent: ast.AST,
) -> Iterator[Tuple[int, int, str]]:

# Unpacking is represented by putting a Tuple or List within targets
if isinstance(node.targets[0], (ast.Tuple, ast.List)):
assignment_names = node.targets[0].elts
else:
assignment_names = node.targets

for item in assignment_names:
if isinstance(item, ast.Name) and item.id != "_" and len(item.id) == 1:
yield item.lineno, item.col_offset, MSG
63 changes: 63 additions & 0 deletions tests/single_letter_variables_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import ast
import tokenize
from io import StringIO

import pytest

from pandas_dev_flaker.__main__ import run


def results(s):
return {
"{}:{}: {}".format(*r)
for r in run(
ast.parse(s),
list(tokenize.generate_tokens(StringIO(s).readline)),
)
}


@pytest.mark.parametrize(
"source",
(
pytest.param(
"ab = 3",
id="Multi-letter assignment",
),
pytest.param(
"_ = 3",
id="Underscore assignment",
),
pytest.param(
"ab, cd, _ = (1, 2, 3)",
id="Unpacking including an underscore",
),
),
)
def test_noop(source):
assert not results(source)


@pytest.mark.parametrize(
"source, expected",
(
pytest.param(
"a = 3",
"1:0: PDF023 found assignment to single-letter variable",
id="Single letter variable",
),
pytest.param(
"bar = a = 1",
"1:6: PDF023 found assignment to single-letter variable",
id="Multiple assignment",
),
pytest.param(
"a, bar = (3, 5)",
"1:0: PDF023 found assignment to single-letter variable",
id="Unpacking",
),
),
)
def test_violation(source, expected):
(result,) = results(source)
assert result == expected
6 changes: 3 additions & 3 deletions tests/string_to_concatenate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def results(s):
"source",
(
pytest.param(
"a = (\n" " 'foo'\n" " 'bar'\n" ")",
"var = (\n" " 'foo'\n" " 'bar'\n" ")",
id="separate lines",
),
),
Expand All @@ -34,8 +34,8 @@ def test_noop(source):
"source, expected",
(
pytest.param(
"a = 'foo''bar'",
"1:4: PDF007 line split in two unnecessarily by 'black' formatter",
"var = 'foo''bar'",
"1:6: PDF007 line split in two unnecessarily by 'black' formatter",
id="consecutive strings",
),
),
Expand Down