Skip to content

Accept iterators for ColorLine colors - #2397

Open
shashank11yadav wants to merge 1 commit into
python-visualization:mainfrom
shashank11yadav:fix/colorline-generator-colors
Open

shashank11yadav wants to merge 1 commit into
python-visualization:mainfrom
shashank11yadav:fix/colorline-generator-colors

Conversation

@shashank11yadav

Copy link
Copy Markdown

Fixes #2396

ColorLine documents colors as an iterable of floats, but a generator or other one-pass iterator fails when colormap is None or a list or tuple. The constructor raises ValueError: max() arg is an empty sequence. min(colors) consumes the iterator, so max(colors) and the segment zip see nothing.

The fix converts colors to a list once, right after validate_locations(positions) in ColorLine.__init__ (folium/features.py). validate_locations already does the same for positions. The LinearColormap and StepColormap paths read colors only once, so they are unchanged.

The new test test_color_line_colors_generator in tests/test_features.py is parametrized over colormap=None and a list colormap. It builds the line from a generator and from a list, and checks that the segments are equal and not empty.

How it was tested:

  • Without the fix, the new test fails for both cases with ValueError: max() arg is an empty sequence.
  • With the fix, the new test passes for both cases: 2 passed.
  • With the fix, the test file tests/test_features.py passes: 50 passed.
  • ruff 0.17.6 check of the whole repo: passes.
  • black 26.10.1 check of the whole repo: passes.
  • mypy 2.4.1 on the folium package: no issues in 51 source files.

I did not run pre-commit run --all-files, because it needs network access to fetch its hook environments. The ruff and black checks above were run. The other pre-commit hooks (blackdoc, codespell, pre-commit-hooks) were not run.

ColorLine documents colors as an iterable of floats, but a generator was
consumed by min() before max() and the segment zip could read it. Convert
colors to a list once, up front, so all three reads see the same values.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ColorLine raises ValueError when colors is a generator

1 participant