Fix UnboundLocalError on single-clause npm version ranges#202
Open
arpitjain099 wants to merge 1 commit into
Open
Fix UnboundLocalError on single-clause npm version ranges#202arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
NpmVersionRange.from_native() routes wildcard tokens such as ">=1.x" and "*.x" through get_npm_version_constraints_from_semver_npm_spec(). When NpmSpec(...).clause.simplify() collapses to a single node-semver Range clause (rather than an AnyOf or AllOf), the helper never bound anyof_constraints and returned it, raising a raw UnboundLocalError out of the parser. Handle the single Range clause by building one VersionConstraint from its operator and target, and raise InvalidVersionRange for any other unexpected clause type. Multi-clause ranges are unchanged. Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While parsing native node-semver ranges I hit an internal crash on some wildcard inputs.
NpmVersionRange.from_native()sends tokens like>=1.xand*.xthroughget_npm_version_constraints_from_semver_npm_spec(). That helper only assignsanyof_constraintsinside theisinstance(clause, (AnyOf, AllOf))branch. WhenNpmSpec(...).clause.simplify()reduces to a single node-semverRangeclause instead of anAnyOf/AllOf, execution falls straight through to thereturn, so a rawUnboundLocalErrorleaks out of the parser:>=1.x,*.x,>1.xand<=2.xall trip it, while ranges that expand to two bounds (1.x,0.x,~1.x,1.2.x - 2.0.0) were fine because they simplify to anAllOf.The fix flattens the branch and adds an explicit case for the single
Rangeclause, building oneVersionConstraintfrom its operator and target the same wayget_allof_constraintsdoes per constraint. Any other unexpected clause type now raisesInvalidVersionRangerather than the old (unreachable)ValueError. Multi-clause handling is unchanged.After the change:
Added parametrized tests in
tests/test_version_range.pycovering the previously-crashing single-clause inputs and confirming the multi-clause ranges still produce the same output. The fulltests/test_version_range.pysuite passes.