Skip to content

Commit

Permalink
Extract method for '_is_assignment' check.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 22, 2024
1 parent 966d86b commit c9a7094
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pip_run/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,20 @@ def _read(self, var_name):
appears more than once.
"""
mod = ast.parse(self.script)
(node,) = (
node
for node in mod.body
if isinstance(node, ast.Assign)
(node,) = (node for node in mod.body if self._is_assignment(node, var_name))
return ast.literal_eval(node.value)

@staticmethod
def _is_assignment(node, var_name):
"""
Does this AST node describe an assignment to var_name?
"""
return (
isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id == var_name
)
return ast.literal_eval(node.value)


class SourceDepsReader(DepsReader):
Expand Down

0 comments on commit c9a7094

Please sign in to comment.