fix: handle scalar XPath results in Selector.xpath() - #141
Merged
Conversation
lxml's xpath() returns a scalar (float/bool) for XPath functions like count() and boolean(). The existing code tried to iterate over the scalar, causing TypeError: 'float' object is not iterable. Add an isinstance() check before the iteration loop to return scalar results as a single-item string list. Fixes mldsveda#136
- ruff: strip whitespace from the blank lines added around the scalar guard, and ruff format selector.py (the failing lint check). - Add tests for count() and boolean() scalar XPath returning string results instead of crashing (mldsveda#136 asked for coverage); both fail on the pre-fix code.
Collaborator
|
Verified locally: the fix is right, checking the scalar on the whole result and routing it through _strings is exactly the clean way to handle count()/boolean(). CI lint was red on trailing whitespace in the new blank lines, so I pushed a commit that fixes that and adds two regression tests (count and boolean) since #136 asked for coverage; both fail on the pre-fix code. 483 passed. Nice fix @mercael91. |
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.
Summary
lxml's
xpath()returns a scalar (float/bool) for XPath functions likecount()andboolean(). The existing code tried to iterate over the scalar, causingTypeError: 'float' object is not iterable.Change
Add an
isinstance()check before the iteration loop to return scalar results as a single-item string list.Repro
Fixes #136