diff --git a/poetry_version_plugin/plugin.py b/poetry_version_plugin/plugin.py index 1308fa8..74dd259 100644 --- a/poetry_version_plugin/plugin.py +++ b/poetry_version_plugin/plugin.py @@ -54,28 +54,26 @@ def activate(self, poetry: Poetry, io: IO) -> None: ) tree = ast.parse(init_path.read_text()) for el in tree.body: - if isinstance(el, ast.Assign): - if len(el.targets) == 1: - target = el.targets[0] - if isinstance(target, ast.Name): - if target.id == "__version__": - value_node = el.value - if isinstance(value_node, ast.Constant): - version = value_node.value - elif isinstance(value_node, ast.Str): - version = value_node.s - else: # pragma: nocover - # This is actually covered by tests, but can't be - # reported by Coverage - # Ref: https://github.com/nedbat/coveragepy/issues/198 - continue - io.write_line( - "poetry-version-plugin: Setting package " - "dynamic version to __version__ " - f"variable from __init__.py: {version}" - ) - poetry.package._set_version(version) - return + if isinstance(el, ast.Assign) and len(el.targets) == 1: + target = el.targets[0] + if isinstance(target, ast.Name) and target.id == "__version__": + value_node = el.value + if isinstance(value_node, ast.Constant): + version = value_node.value + elif isinstance(value_node, ast.Str): + version = value_node.s + else: # pragma: nocover + # This is actually covered by tests, but can't be + # reported by Coverage + # Ref: https://github.com/nedbat/coveragepy/issues/198 + continue + io.write_line( + "poetry-version-plugin: Setting package " + "dynamic version to __version__ " + f"variable from __init__.py: {version}" + ) + poetry.package._set_version(version) + return message = ( "poetry-version-plugin: No valid __version__ variable found " "in __init__.py, cannot extract dynamic version"