Improve PythonVersions cop to reject hardcoded pythonX.Y assignment - #23479
Improve PythonVersions cop to reject hardcoded pythonX.Y assignment#23479iMichka wants to merge 1 commit into
Conversation
|
I'll open a pull request in core to fix this before this can be merged: let me know if you think this is a good addition |
There was a problem hiding this comment.
Pull request overview
This PR tightens Homebrew鈥檚 FormulaAudit/PythonVersions RuboCop cop to discourage hardcoding pythonX.Y into formula code by detecting python = "pythonX.Y" assignments and auto-correcting them to use Language::Python.major_minor_version (with libexec/"bin/python"), and it updates the cop鈥檚 spec coverage accordingly.
Changes:
- Add a shared
PYTHON_VERSION_REFERENCE_REGEXand reuse it for Python reference detection. - Add a new offense + autocorrect for hardcoded
python = "pythonX.Y"assignments. - Extend
python_versions_spec.rbwith new examples covering the new offense and correction.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Library/Homebrew/rubocops/lines.rb | Adds shared regex and new Python local-assignment offense with autocorrect. |
| Library/Homebrew/test/rubocops/text/python_versions_spec.rb | Adds new specs for the hardcoded assignment offense and dynamic-assignment non-offense. |
馃挕 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
a6d3c96 to
f682844
Compare
MikeMcQuaid
left a comment
There was a problem hiding this comment.
Makes sense to me although a slightly shorter-hand version might be nicer?
| depends_on "python@3.14" | ||
|
|
||
| def install | ||
| python3 = "python#{Language::Python.major_minor_version libexec/"bin/python"}" |
There was a problem hiding this comment.
I don't think libexec path exists? Maybe if using Formula["python@3.14"].opt_libexec but that requires hardcoding formula name. Otherwise would only work if virtualenv was created.
Also note that Language::Python.major_minor_version can output strange results if you don't guarantee the command passed is real, e.g.
brew(main):001> p Language::Python.major_minor_version("not-python-99.100")
#<Version 9.100>
=> #<Version 9.100>
brew(main):002> p Language::Python.major_minor_version("/foo/bar/1.2.3/python")
#<Version 1.2>
=> #<Version 1.2>We currently allow parsing version from error message which may look like command not found: not-python-99.100.
For dynamic detection, most common way is to find dependency first, e.g.
It may also be possible to use sanitized environment, e.g. which("python3") or which("python"), but this could be wrong when multiple pythons are in dependency tree. We do not prioritize direct Python and instead order on versions, which means a depends_on "python@3.13" with indirect python@3.14 will lead to wrong python.
There was a problem hiding this comment.
Also note that Language::Python.major_minor_version can output strange results if you don't guarantee the command passed is real, e.g.
I'm aware of that. In general people have just copy-pasted the right thing, so only the "happy path" has been used and we did not have to safeguard against wrongly formatted output yet. Not sure this is worth checking, given this syntax has been in place without issues for a very long time now.
There was a problem hiding this comment.
I'm checking the best way to handle the libexec issue, you are right that this is not always correct
Enforce dynamic Python version detection in formula code by flagging hardcoded pythonX.Y strings assigned to python and autocorrecting to Language::Python.major_minor_version with libexec/bin/python. Generalize detection using a shared Python version regex and update PythonVersions specs accordingly.
f682844 to
55d4c3f
Compare
Enforce dynamic Python version detection in formula code by flagging hardcoded pythonX.Y strings assigned to python and autocorrecting to Language::Python.major_minor_version with libexec/bin/python. Generalize detection using a shared Python version regex and update PythonVersions specs accordingly.
brewcommands to reproduce the bug?brew lgtm(style, typechecking and tests) locally?