Skip to content

ColorLine raises ValueError when colors is a generator #2396

Backlog

Description

@shashank11yadav

Describe the bug

folium.ColorLine documents colors as an iterable of float (colors: Iterable[float]). Passing a generator raises ValueError: max() iterable argument is empty when colormap is None, a list or a tuple. min(colors) consumes the generator, so max(colors) sees nothing. A list works. positions had the same problem with iterators and was fixed in #2299.

To Reproduce

import folium

pos = [[0, 0], [1, 1], [2, 2]]
vals = [0.1, 0.9]
ctl = folium.ColorLine(positions=pos, colors=list(vals))
print("control (list) polylines:", len(ctl._children))
print("EXPECTED: ColorLine accepts iterable of float (generator) like a list")
cl = folium.ColorLine(positions=pos, colors=(c for c in vals))
print("ACTUAL: built with", len(cl._children), "polylines")

Output on main (0401f7e):

control (list) polylines: 2
EXPECTED: ColorLine accepts iterable of float (generator) like a list

followed by a traceback ending in:

ValueError: max() iterable argument is empty

The same happens on release 1.0.0.

Expected behavior

A generator works like a list, as the Iterable[float] hint and the docstring (folium/features.py:1969, :1993) promise.

Environment (please complete the following information):

  • Browser: not applicable
  • Jupyter Notebook or html files? Plain Python script
  • Python version: 3.12 (Linux)
  • folium version: 1.0.0, and main @ 0401f7e
  • branca version: 0.8.2

Additional context

folium/features.py:2007-2008 and :2015-2016 call min(colors) then max(colors). The final zip(..., colors) at :2026 reads colors again, so fixing only min/max would still give no segments.

Possible solutions

Convert with colors = list(colors) right after validate_locations, which fixes both places.

I have a fix with a regression test ready and will open a pull request for it right after filing this issue; it will link back here.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions