diff options
| author | Karl Otness <karl@karlotness.com> | 2022-06-15 21:05:57 -0400 |
|---|---|---|
| committer | Karl Otness <karl@karlotness.com> | 2022-06-18 22:24:36 -0400 |
| commit | 685c80c864f05ee37db31246ff125639aa38432e (patch) | |
| tree | d6a9b38ad859a37785e9d71b38b0b7c0285ba789 /setuptools/config | |
| parent | be632321303dd15ef19c97cec8cd9c8239522655 (diff) | |
| download | python-setuptools-git-685c80c864f05ee37db31246ff125639aa38432e.tar.gz | |
Add support for annotated assignments to static attribute lookup.
When walking the ast of a module, look for AnnAssign nodes in addition
to Assign to support assignments with type annotations, for example.
Since we have to read different attributes, split the generators into
a for loop. Existing ast.Assign nodes follow the same processing as
before.
Diffstat (limited to 'setuptools/config')
| -rw-r--r-- | setuptools/config/expand.py | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index be987df5..a0dd7c2a 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -69,24 +69,21 @@ class StaticModule: def __getattr__(self, attr): """Attempt to load an attribute "statically", via :func:`ast.literal_eval`.""" try: - assignment_expressions = ( - statement - for statement in self.module.body - if isinstance(statement, ast.Assign) - ) - expressions_with_target = ( - (statement, target) - for statement in assignment_expressions - for target in statement.targets - ) - matching_values = ( - statement.value - for statement, target in expressions_with_target - if isinstance(target, ast.Name) and target.id == attr - ) - return next(ast.literal_eval(value) for value in matching_values) + 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) 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( |
