Skip to content

fix: handle scalar XPath results in Selector.xpath() - #141

Merged
vedaant00 merged 2 commits into
mldsveda:mainfrom
mercael91:fix/xpath-scalar-handling
Aug 11, 2026
Merged

fix: handle scalar XPath results in Selector.xpath()#141
vedaant00 merged 2 commits into
mldsveda:mainfrom
mercael91:fix/xpath-scalar-handling

Conversation

@mercael91

Copy link
Copy Markdown
Contributor

Summary

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.

Change

Add an isinstance() check before the iteration loop to return scalar results as a single-item string list.

# Added before the iteration loop
if isinstance(results, (float, int, bool)):
    return SelectorList([], _strings=[str(results)])

Repro

from pyscrappy import Selector

Selector("<div><p>1</p><p>2</p></div>").xpath("count(//p)").getall()
# Before: TypeError: 'float' object is not iterable
# After: ['2.0']

Fixes #136

mercael91 and others added 2 commits August 11, 2026 14:20
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.
@vedaant00

Copy link
Copy Markdown
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.

@vedaant00
vedaant00 merged commit c93a092 into mldsveda:main Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Selector.xpath() crashes on a scalar XPath like count(...) or boolean(...)

2 participants