Sorting Logic Bug in FeedReader.sort_entries() #73

Closed
opened 2026-02-17 06:23:04 +00:00 by marvin8 · 1 comment
marvin8 commented 2026-02-17 06:23:04 +00:00 (Migrated from codeberg.org)

Sorting Logic Bug in FeedReader.sort_entries()

The Problem

Feed items aren't being sorted properly when some entries have missing or invalid publication dates.

Where: src/feed2fedi/collect.pyFeedReader.sort_entries() method
Priority: High (affects posting order)

What's Broken

The current code checks if ALL feed items have valid published_parsed dates before sorting. If even one item has None or an empty value, sorting gets skipped entirely.

Current code:

    def sort_entries(self) -> None:
        """Sorts entries."""
        if all("published_parsed" in item for item in self.items) and all(
            item["published_parsed"] for item in self.items
        ):
            self.items.sort(key=lambda item: item["published_parsed"])

The issue: That second all(...) check requires every published_parsed value to be "truthy" (not None, not empty). Real-world feeds often have items with missing dates.

What Should Happen

Feed items should be sorted whenever possible, even if some dates are missing. Items with missing dates should be treated as "earliest" and sorted to the beginning.

The Fix

Replace the current method with:

    def sort_entries(self) -> None:
        """Sorts entries."""
        if all("published_parsed" in item for item in self.items):
            # Sort even if some values are None or empty
            self.items.sort(key=lambda item: item.get("published_parsed") or ())

Why this works:

  • .get() safely handles missing keys (though we already check they exist)
  • or () gives missing dates a consistent value (empty tuple) that sorts before real dates
  • Simple, safe, and maintains backward compatibility

Impact

Without this fix:

  • Feed items appear in unpredictable order
  • Posting to Fediverse instances happens in wrong sequence
  • Users see inconsistent feed ordering

To Test

Add unit tests for:

  • Normal feeds with all valid dates
  • Feeds with some None dates mixed in
  • Feeds where all dates are None
  • Edge cases with earliest/latest possible dates

Quick Checklist

  • Update sort_entries() method
  • Add/update tests in tests/unit/test_collect.py
  • Run prek run --all-files
  • Run ty check and ruff check
  • Run pytest tests/unit/test_collect.py
  • Run nox for full CI validation

Notes

  • This is a simple, low-risk fix
  • No dependency changes needed
  • Similar date-handling code in publish.py should be checked for same issue
# Sorting Logic Bug in FeedReader.sort_entries() ## The Problem Feed items aren't being sorted properly when some entries have missing or invalid publication dates. **Where**: `src/feed2fedi/collect.py` → `FeedReader.sort_entries()` method **Priority**: High (affects posting order) ## What's Broken The current code checks if ALL feed items have valid `published_parsed` dates before sorting. If even one item has `None` or an empty value, sorting gets skipped entirely. **Current code**: ```feed2fedi/src/feed2fedi/collect.py#L35-40 def sort_entries(self) -> None: """Sorts entries.""" if all("published_parsed" in item for item in self.items) and all( item["published_parsed"] for item in self.items ): self.items.sort(key=lambda item: item["published_parsed"]) ``` **The issue**: That second `all(...)` check requires every `published_parsed` value to be "truthy" (not `None`, not empty). Real-world feeds often have items with missing dates. ## What Should Happen Feed items should be sorted whenever possible, even if some dates are missing. Items with missing dates should be treated as "earliest" and sorted to the beginning. ## The Fix Replace the current method with: ```feed2fedi/src/feed2fedi/collect.py#L35-40 def sort_entries(self) -> None: """Sorts entries.""" if all("published_parsed" in item for item in self.items): # Sort even if some values are None or empty self.items.sort(key=lambda item: item.get("published_parsed") or ()) ``` **Why this works**: - `.get()` safely handles missing keys (though we already check they exist) - `or ()` gives missing dates a consistent value (empty tuple) that sorts before real dates - Simple, safe, and maintains backward compatibility ## Impact Without this fix: - Feed items appear in unpredictable order - Posting to Fediverse instances happens in wrong sequence - Users see inconsistent feed ordering ## To Test Add unit tests for: - Normal feeds with all valid dates - Feeds with some `None` dates mixed in - Feeds where all dates are `None` - Edge cases with earliest/latest possible dates ## Quick Checklist - [ ] Update `sort_entries()` method - [ ] Add/update tests in `tests/unit/test_collect.py` - [ ] Run `prek run --all-files` - [ ] Run `ty check` and `ruff check` - [ ] Run `pytest tests/unit/test_collect.py` - [ ] Run `nox` for full CI validation ## Notes - This is a simple, low-risk fix - No dependency changes needed - Similar date-handling code in `publish.py` should be checked for same issue
marvin8 commented 2026-04-23 08:26:42 +00:00 (Migrated from codeberg.org)

Implemented in version 3.7.3

Implemented in version 3.7.3
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
marvin8/feed2fedi#73
No description provided.