Skip to content

Refactor extractors: Pythonic improvements, docstrings, and optimizations#730

Draft
AndyTheFactory with Copilot wants to merge 3 commits into
dev-0.9.6from
copilot/check-extractor-folder-code
Draft

Refactor extractors: Pythonic improvements, docstrings, and optimizations#730
AndyTheFactory with Copilot wants to merge 3 commits into
dev-0.9.6from
copilot/check-extractor-folder-code

Conversation

Copilot AI commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Reviewed and refactored all files in newspaper/extractors/ for readability, Pythonic style, correctness, and minor performance improvements.

Changes

authors_extractor.py

  • Moved inner functions (uniqify_list, parse_byline, get_authors, get_text_from_element) out of parse() and into proper class/static methods
  • Pre-compiled regex patterns (_DIGITS_RE, _author_stopwords_re) at class level to avoid recompiling on every parse() call
  • Replaced OrderedDict with plain dict (ordered since Python 3.7) — removes an unnecessary import
  • Fixed variable shadowing (m was reused for both a regex match and a loop variable)
  • Fixed return type annotation on content from str | list[str] to just str

pubdate_extractor.py

  • Extracted inner parse_date_str closure to a proper _parse_date_str class method
  • Eliminated redundant intermediate date_str local variables where the value was only used once

categories_extractor.py

  • Used set.isdisjoint() instead of len(set.intersection()) == 0 (more readable and marginally faster)
  • Replaced (x != "http" and x != "https") with idiomatic x not in ("http", "https")
  • Used not _valid_categories instead of len(_valid_categories) == 0

content_extractor.py

  • Simplified get_feed_urls from a multi-step loop + list + dedup pipeline into a clean set-comprehension
  • Added missing module and __init__ docstrings

articlebody_extractor.py

  • Used generator expressions in sum() instead of building intermediate lists (reduces memory allocation)
  • Fixed add_siblings which was missing the res_node = copy.deepcopy(top_node) assignment
  • Added missing docstrings (D100, D101, D102)

videos_extractor.py

  • Used getattr(self, ..., None) for safe method dispatch — avoids a potential AttributeError if an unknown video tag is encountered
  • Added missing module and __init__ docstrings

image_extractor.py / metadata_extractor.py / title_extractor.py

  • Added missing module-level, class-level, and __init__ docstrings
  • Fixed parse docstring in image_extractor.py to document all arguments

defines.py

  • Added module-level docstring
  • Added MetaImageDict class docstring; restored accidentally missing tag: str field

Validation

  • ruff check newspaper/extractors/ — ✅ All checks passed
  • pytest tests/unit/ — ✅ 128 tests pass (10 pre-existing failures from missing optional dependencies: nltk, pythainlp, indic-nlp-library)
  • CodeQL security scan — ✅ 0 alerts

Copilot AI and others added 3 commits April 29, 2026 07:30
Agent-Logs-Url: https://github.com/AndyTheFactory/newspaper4k/sessions/0c3d85a0-d9b2-4638-83b3-e6b79589dc06

Co-authored-by: AndyTheFactory <863810+AndyTheFactory@users.noreply.github.com>
Agent-Logs-Url: https://github.com/AndyTheFactory/newspaper4k/sessions/8c0e3849-680a-4f64-b2d1-7dd92e9f3aae

Co-authored-by: AndyTheFactory <863810+AndyTheFactory@users.noreply.github.com>
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.

2 participants