Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StaticModule.__getattr__ and fix ast.AnnAssign edge case #1

Merged
merged 3 commits into from Jun 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions setuptools/config/expand.py
Expand Up @@ -66,24 +66,23 @@ def __init__(self, name: str, spec: ModuleSpec):
vars(self).update(locals())
del self.self

def _find_assignments(self) -> Iterator[Tuple[ast.AST, ast.AST]]:
for statement in self.module.body:
if isinstance(statement, ast.Assign):
yield from ((target, statement.value) for target in statement.targets)
elif isinstance(statement, ast.AnnAssign) and statement.value:
karlotness marked this conversation as resolved.
Show resolved Hide resolved
yield (statement.target, statement.value)

def __getattr__(self, attr):
"""Attempt to load an attribute "statically", via :func:`ast.literal_eval`."""
try:
for statement in self.module.body:
if isinstance(statement, ast.Assign):
targets = statement.targets
value = statement.value
elif isinstance(statement, ast.AnnAssign):
targets = [statement.target]
value = statement.value
else:
continue
for target in targets:
if isinstance(target, ast.Name) and target.id == attr:
return ast.literal_eval(value)
return next(
ast.literal_eval(value)
for target, value in self._find_assignments()
if isinstance(target, ast.Name) and target.id == attr
)
except Exception as e:
raise AttributeError(f"{self.name} has no attribute {attr}") from e
raise AttributeError(f"{self.name} has no attribute {attr}")


def glob_relative(
Expand Down
14 changes: 9 additions & 5 deletions setuptools/tests/config/test_expand.py
Expand Up @@ -85,13 +85,17 @@ def test_read_attr(self, tmp_path, monkeypatch):
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'}, tmp_path)
assert values['c'] == (0, 1, 1)

def test_read_annotated_attr(self, tmp_path):
@pytest.mark.parametrize(
"example",
[
"VERSION: str\nVERSION = '0.1.1'\nraise SystemExit(1)\n",
"VERSION: str = '0.1.1'\nraise SystemExit(1)\n",
]
)
def test_read_annotated_attr(self, tmp_path, example):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": (
"VERSION: str = '0.1.1'\n"
"raise SystemExit(1)\n"
),
"pkg/sub/__init__.py": example,
}
write_files(files, tmp_path)
# Make sure this attribute can be read statically
Expand Down