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

Fixup collections #19

Merged
merged 3 commits into from
Apr 10, 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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ repos:
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.7.0]
args: [--extend-ignore=E203]
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@register()
def foo(
def visit(
tokens: Sequence[tokenize.TokenInfo],
) -> Iterator[Tuple[int, int, str]]:
for current_token, next_token in zip(tokens, tokens[1:]):
Expand Down
15 changes: 14 additions & 1 deletion pandas_dev_flaker/_plugins_tree/collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

from pandas_dev_flaker._data_tree import State, register

MSG = "PDF001 don't import from collections.abc"
MSG = (
"PDF001 import from collections.abc "
"(use 'from collections import abc' instead)"
)


@register(ast.ImportFrom)
Expand All @@ -14,3 +17,13 @@ def visit_ImportFrom(
) -> Iterator[Tuple[int, int, str]]:
if node.module == "collections.abc":
yield node.lineno, node.col_offset, MSG


@register(ast.Import)
def visit_Import(
state: State,
node: ast.Import,
parent: ast.AST,
) -> Iterator[Tuple[int, int, str]]:
if "collections.abc" in {name.name for name in node.names}:
yield node.lineno, node.col_offset, MSG
52 changes: 52 additions & 0 deletions tests/collections_abc_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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(
"from collections import abc",
id="Imported abc from collections",
),
),
)
def test_noop(source):
assert not results(source)


@pytest.mark.parametrize(
"source, expected",
(
pytest.param(
"from collections.abc import Generator",
"1:0: PDF001 import from collections.abc "
"(use 'from collections import abc' instead)",
id="Imported from collections.abc",
),
pytest.param(
"import collections.abc",
"1:0: PDF001 import from collections.abc "
"(use 'from collections import abc' instead)",
id="Imported collections.abc",
),
),
)
def test_violation(source, expected):
(result,) = results(source)
assert result == expected