fix: Do not choose top node that will be emptied by post_cleanup#729
Draft
AndyTheFactory with Copilot wants to merge 2 commits into
Draft
fix: Do not choose top node that will be emptied by post_cleanup#729AndyTheFactory with Copilot wants to merge 2 commits into
AndyTheFactory with Copilot wants to merge 2 commits into
Conversation
Agent-Logs-Url: https://github.com/AndyTheFactory/newspaper4k/sessions/bf0ca2dc-a14b-43f7-9cbc-a4276f0de3e2 Co-authored-by: AndyTheFactory <863810+AndyTheFactory@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix top node selection to avoid empty nodes
fix: Do not choose top node that will be emptied by post_cleanup
Apr 27, 2026
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.
calculate_best_nodealways selectedparent_nodes[0](highest gravity score) without checking whether that node has any actual text content. Nodes can score highly via grandparent propagation while being structurally empty (images-only, empty wrappers, etc.), resulting in a top node that post-cleanup strips entirely—producing an empty article.Changes
newspaper/extractors/articlebody_extractor.py:calculate_best_nodenow iterates through sorted candidates and picks the first with non-empty text (parsers.get_text(candidate)), skipping nodes that would be emptied by cleanup. ReturnsNoneif no candidate has text.tests/unit/test_parsing.py: AddedTestArticleBodyExtractorwith two tests covering the skip-empty behavior and the all-empty-candidates →Nonefallback.How did you test it?
Unit tests via
pytest tests/unit/test_parsing.py. The new tests mockcompute_gravity_scoresto inject an empty high-score node alongside a text-containing lower-score node, verifying the correct node is selected.Notes for the reviewer
parsers.get_text()already callsinner_trim(strips all whitespace), so the truthiness check is sufficient—no additional regex stripping needed.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
example.com/home/REDACTED/.local/bin/pytest pytest tests/unit/ --ignore=tests/unit/test_google_news.py --ignore=tests/unit/test_nlp.py -x -q(dns block)/home/REDACTED/.local/bin/pytest pytest tests/unit/ --ignore=tests/unit/test_google_news.py --ignore=tests/unit/test_nlp.py -q(dns block)/home/REDACTED/.local/bin/pytest pytest tests/unit/test_article.py tests/unit/test_cleaners.py tests/unit/test_parsing.py -v(dns block)media.cnn.com/home/REDACTED/.local/bin/pytest pytest tests/unit/ --ignore=tests/unit/test_google_news.py --ignore=tests/unit/test_nlp.py -q(dns block)publicsuffix.org/home/REDACTED/.local/bin/pytest pytest tests/unit/ --ignore=tests/unit/test_google_news.py --ignore=tests/unit/test_nlp.py -q(dns block)If you need me to access, download, or install something from one of these locations, you can either: