Skip to content

Fix: before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has... - #269

Open
M001N wants to merge 1 commit into
gawel:masterfrom
M001N:oss-engine/bec1c285-26a56eb3
Open

Fix: before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has...#269
M001N wants to merge 1 commit into
gawel:masterfrom
M001N:oss-engine/bec1c285-26a56eb3

Conversation

@M001N

@M001N M001N commented Aug 17, 2026

Copy link
Copy Markdown

Summary

In before(), added an explicit if parent is None: raise ValueError(...) check both at the point parent.text is accessed and again before parent.index(tag) (the second getparent() call later in the loop). In after(), added the same check before parent.index(tag). Both raise a clear ValueError explaining that the element has no parent, instead of letting the unrelated AttributeError propagate.

Problem

gawel/pyquery issue reference: #105

Root Cause

before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has no previous sibling, unconditionally accesses parent.text — if tag is the topmost/root element, getparent() returns None and None.text raises a confusing AttributeError. after() has the analogous unconditional parent.index(tag) call. replace_with() calls before() internally, so it inherits the same crash.

Testing

PASS - full suite: 74 passed, 4 skipped (skips are pre-existing, OS-specific, unrelated to this change); all 4 new tests pass.

Related Issue

#105

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.

1 participant