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

Commit be66105

Browse files
committed
private import
1 parent 752dca6 commit be66105

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

pandas_style_guide/_data.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
from pandas_style_guide import _plugins
66

77
FUNCS = collections.defaultdict(list)
8-
RECORD_FROM_IMPORTS = frozenset((
9-
'numpy',
10-
'pytest',
11-
'unittest'
12-
))
138
class State(NamedTuple):
149
from_imports: Dict[str, Set[str]]
1510
in_annotation: bool = False
@@ -20,6 +15,11 @@ def register_decorator(func):
2015
return func
2116
return register_decorator
2217

18+
def _get_alias(name):
19+
if name.asname is not None:
20+
return name.asname
21+
else:
22+
return name.name
2323

2424
def visit(funcs, tree: ast.Module) -> Dict[int, List[int]]:
2525
"Step through tree, recording when nodes are in annotations."
@@ -35,13 +35,15 @@ def visit(funcs, tree: ast.Module) -> Dict[int, List[int]]:
3535
yield from ast_func(state, node, parent)
3636

3737
if (
38-
isinstance(node, ast.ImportFrom) and
39-
not node.level and
40-
node.module in RECORD_FROM_IMPORTS
38+
isinstance(node, ast.ImportFrom)
39+
and node.module is not None
4140
):
42-
state.from_imports[node.module].update(
43-
name.name for name in node.names if not name.asname
41+
state.from_imports[node.module.split('.')[0]].update(
42+
_get_alias(name) for name in node.names if not name.asname
4443
)
44+
elif isinstance(node, ast.Import):
45+
for name in node.names:
46+
state.from_imports[_get_alias(name)]
4547

4648
for name in reversed(node._fields):
4749
value = getattr(node, name)

pandas_style_guide/_plugins/bare_pytest_raises.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
MSG = 'PSG010 bare pytest raises found'
66
@register(ast.Call)
77
def np_bool_object(state, node, parent):
8-
breakpoint()
98
if not node.keywords:
109
yield node.lineno, node.col_offset, MSG
1110
elif 'match' not in {keyword.arg for keyword in node.keywords}:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import ast
2+
from pandas_style_guide._data import register
3+
from pandas_style_guide._ast_helpers import is_name_attr
4+
5+
PRIVATE_FUNCTIONS_ALLOWED = {"sys._getframe"} # no known alternative
6+
7+
MSG = 'PSG010 private import!'
8+
@register(ast.Call)
9+
def np_bool_object(state, node, parent):
10+
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and (
11+
any(node.func.value.id in imports for imports in state.from_imports.values())
12+
or
13+
any(node.func.value.id in imports for imports in state.from_imports)
14+
) and node.func.attr.startswith('_'):
15+
yield node.lineno, node.col_offset, MSG

t.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
1-
# import pytest as foobar
2-
# from pytest import raises
3-
# import pytest
4-
# import numpy as np
5-
# from unittest import mock
6-
# import unittest
7-
# from numpy import bool
1+
import pandas
2+
import conftest
3+
from pandas.foo.bar.dude import _foo
84

9-
# import numpy as np
10-
# bool(3)
11-
with pytest.raises(ValueError, dude='a'):
12-
pass
13-
14-
15-
pd.api.types.is_scalar
16-
17-
# np.bool
18-
19-
# pytest.raises()
20-
# raises()
21-
# filter([123])
22-
# with pytest.raises:
23-
# pass
24-
25-
# exec('123')
26-
27-
# np.testing
28-
# mock
29-
# unittest.mock
30-
foo = 3
31-
foo.__class__
5+
_foo._foo('dod')

0 commit comments

Comments
 (0)