Skip to content

fix: generator input silently bypasses make_linked_list empty-list contract - #15552

Open
peda-cos wants to merge 1 commit into
TheAlgorithms:masterfrom
peda-cos:fix/linked-list-generator-contract
Open

peda-cos wants to merge 1 commit into
TheAlgorithms:masterfrom
peda-cos:fix/linked-list-generator-contract

Conversation

@peda-cos

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: changed together with the code because the doctests are the regression proof for this bug; the new cases fail on the current code.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Description

make_linked_list(elements_list: Iterable[int]) documents that an empty input raises "The Elements List is empty", but the check is a truthiness test if not elements_list. Truthiness of Iterable has no such meaning for arbitrary iterables, so make_linked_list(x for x in []) currently returns an empty LinkedList instead of raising, and a non-empty generator works only by luck.

Materialize the iterable (elements_list = list(elements_list)) before the emptiness check so the documented contract holds for any iterable. The annotation is unchanged.

How I tested

  • Before the change: make_linked_list(x for x in range(0)) silently returned an empty LinkedList.
  • After the change: raises Exception: The Elements List is empty; make_linked_list(x for x in [7, 25]) still builds 7 -> 25.
  • pytest --doctest-modules data_structures/linked_list/print_reverse.py — 7 passed, including the two new generator doctests.

@peda-cos
peda-cos requested a review from cclauss as a code owner October 11, 2026 04:58
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 11, 2026
make_linked_list declared the input as Iterable[int] but tested emptiness
with truthiness, so an empty generator silently produced an empty
LinkedList instead of raising. Materialize the iterable first and cover
generator input with doctests.
@peda-cos
peda-cos force-pushed the fix/linked-list-generator-contract branch from 580c9cd to f1c37e3 Compare October 11, 2026 04:59
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 11, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant