Skip to content

Commit

Permalink
Fix wrong error for setup() w/o python_requires=
Browse files Browse the repository at this point in the history
Fixes: 2e4a76e
  • Loading branch information
mgedmin committed Dec 11, 2018
1 parent 488433b commit 3526962
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
14 changes: 7 additions & 7 deletions check_python_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ def get_setup_py_keyword(setup_py, keyword):
warn(f'Could not parse {setup_py}: {error}')
return None
node = find_call_kwarg_in_ast(tree, 'setup', keyword)
if node is None:
warn('Could not find setup() call in setup.py')
return None
return eval_ast_node(node, keyword)
return node and eval_ast_node(node, keyword)


def find_call_kwarg_in_ast(tree, funcname, keyword):
def find_call_kwarg_in_ast(tree, funcname, keyword, filename='setup.py'):
for node in ast.walk(tree):
if (isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == funcname):
for kwarg in node.keywords:
if kwarg.arg == keyword:
return kwarg.value
break
return None
else:
return None
else:
warn(f'Could not find {funcname}() call in {filename}')
return None


def eval_ast_node(node, keyword):
Expand Down
19 changes: 18 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_get_python_requires(tmp_path, monkeypatch):
assert cpv.get_python_requires(setup_py) == ['3.6', '3.7']


def test_get_python_requires_not_specified(tmp_path):
def test_get_python_requires_not_specified(tmp_path, capsys):
setup_py = tmp_path / "setup.py"
setup_py.write_text(textwrap.dedent("""\
from setuptools import setup
Expand All @@ -98,6 +98,7 @@ def test_get_python_requires_not_specified(tmp_path):
)
"""))
assert cpv.get_python_requires(setup_py) is None
assert capsys.readouterr().err == ''


def test_get_setup_py_keyword_syntax_error(tmp_path, capsys):
Expand All @@ -120,6 +121,22 @@ def test_find_call_kwarg_in_ast():
assert node.s == "foo"


def test_find_call_kwarg_in_ast_no_arg(capsys):
tree = ast.parse('foo(baz="foo")')
ast.dump(tree)
node = cpv.find_call_kwarg_in_ast(tree, 'foo', 'bar')
assert node is None
assert capsys.readouterr().err == ''


def test_find_call_kwarg_in_ast_no_call(capsys):
tree = ast.parse('fooo(bar="foo")')
ast.dump(tree)
node = cpv.find_call_kwarg_in_ast(tree, 'foo', 'bar')
assert node is None
assert 'Could not find foo() call in setup.py' in capsys.readouterr().err


@pytest.mark.parametrize('code, expected', [
('"hi"', "hi"),
('"hi\\n"', "hi\n"),
Expand Down

0 comments on commit 3526962

Please sign in to comment.