Skip to content

Fix missing CSS attr defaults - #133

Merged
vedaant00 merged 1 commit into
mldsveda:mainfrom
kocaemre:fix/selector-missing-attr-default
Aug 10, 2026
Merged

Fix missing CSS attr defaults#133
vedaant00 merged 1 commit into
mldsveda:mainfrom
kocaemre:fix/selector-missing-attr-default

Conversation

@kocaemre

Copy link
Copy Markdown
Contributor

Summary

  • Make CSS ::attr(...) values skip elements where the requested attribute is absent.
  • Let .get(default) return the provided default for matched elements that lack the attribute.
  • Add selector regression tests for present attributes, missing attributes, and getall().

Fixes #131.

Tests

  • RED: python -m pytest tests/test_generic/test_selector.py::TestCss::test_css_attr_missing_attribute_uses_default tests/test_generic/test_selector.py::TestCss::test_css_attr_getall_skips_missing_attributes -q failed with '' == 'MISSING' and ['/present', ''] == ['/present']
  • GREEN: python -m pytest tests/test_generic/test_selector.py -q21 passed
  • GREEN: python -m pytest tests/ -q471 passed, 1 skipped, 19 deselected
  • GREEN: ruff check src/All checks passed!

Signed-off-by: Emre K <110906681+kocaemre@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates SelectorList’s CSS ::attr(...) handling to align with Scrapy/Parsel-style semantics: elements that match the selector but lack the requested attribute no longer yield an empty-string value, allowing .get(default=...) to return the default and .getall() to exclude missing attributes.

Changes:

  • Update SelectorList._values() for ::attr(...) to skip elements where the attribute key is absent.
  • Add regression tests covering .get(default) for missing attributes and .getall() behavior when some elements lack the attribute.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/pyscrappy/generic/selector.py Adjusts ::attr(...) value extraction to skip elements missing the attribute, enabling correct .get(default) semantics.
tests/test_generic/test_selector.py Adds targeted tests for missing-attribute defaults and getall() skipping behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@vedaant00
vedaant00 merged commit 348fe55 into mldsveda:main Aug 10, 2026
6 checks passed
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: ::attr(name) on an element missing that attribute returns "" and ignores .get(default)

3 participants