Skip to content

Commit 9b080a6

Browse files
committed
[fix] Resolved F821 undefined name and version.py fallback errors
Moved version.py candidate-files check from _handle_pyproject_toml_version to _handle_python_version so it runs regardless of pyproject.toml existence. Added TypeError to except clause in _handle_python_version list() call.
1 parent fadd47a commit 9b080a6

2 files changed

Lines changed: 10 additions & 25 deletions

File tree

openwisp_utils/releaser/config.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,22 @@ def _handle_python_version(config):
6969
project_name = get_package_name_from_setup()
7070
if project_name:
7171
package_directory = project_name.replace("-", "_")
72-
init_py_path = os.path.join(package_directory, "__init__.py")
73-
if os.path.exists(init_py_path):
74-
with open(init_py_path, "r") as f:
72+
candidate_files = [
73+
os.path.join(package_directory, "__init__.py"),
74+
os.path.join(package_directory, "version.py"),
75+
]
76+
for version_path in candidate_files:
77+
if not os.path.exists(version_path):
78+
continue
79+
with open(version_path, "r") as f:
7580
content = f.read()
7681
version_match = re.search(r"^VERSION\s*=\s*\((.*)\)", content, re.M)
7782
if version_match:
78-
config["version_path"] = init_py_path
83+
config["version_path"] = version_path
7984
try:
8085
version_tuple = ast.literal_eval(f"({version_match.group(1)})")
8186
config["CURRENT_VERSION"] = list(version_tuple)
82-
except (ValueError, SyntaxError):
87+
except (ValueError, SyntaxError, TypeError):
8388
config["CURRENT_VERSION"] = None
8489
return
8590
_handle_pyproject_toml_version(config)
@@ -110,25 +115,6 @@ def _handle_pyproject_toml_version(config):
110115
config["package_type"] = "pyproject"
111116
config["version_path"] = "pyproject.toml"
112117
config["CURRENT_VERSION"] = current_version
113-
package_directory = project_name.replace("-", "_")
114-
candidate_files = [
115-
os.path.join(package_directory, "__init__.py"),
116-
os.path.join(package_directory, "version.py"),
117-
]
118-
for version_path in candidate_files:
119-
if not os.path.exists(version_path):
120-
continue
121-
with open(version_path, "r") as f:
122-
content = f.read()
123-
version_match = re.search(r"^VERSION\s*=\s*\((.*)\)", content, re.M)
124-
if version_match:
125-
config["version_path"] = version_path
126-
try:
127-
version_tuple = ast.literal_eval(f"({version_match.group(1)})")
128-
config["CURRENT_VERSION"] = list(version_tuple)
129-
except (ValueError, SyntaxError, TypeError):
130-
config["CURRENT_VERSION"] = None
131-
return
132118

133119

134120
def _handle_npm_version(config):

openwisp_utils/releaser/tests/test_version_bumping.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,3 @@ def test_bump_version_pyproject_toml_malformed():
467467
with patch("os.path.exists", return_value=True), patch("builtins.open", m_open):
468468
with pytest.raises(RuntimeError, match="Failed to find"):
469469
bump_version(config, "1.2.4")
470-
assert written_content == "1.2.4\n"

0 commit comments

Comments
 (0)