# Buggy code
def read_multilayer_svg(
    filename: str,
    quantization: float,
    simplify: bool = False,
    return_size: bool = False,
) -> Union["VectorData", Tuple["VectorData", float, float]]:
    """Read a multilayer SVG file and return its content as a :class:`VectorData` instance
    retaining the SVG's layer structure.

    Each top-level group is considered a layer. All non-group, top-level elements are imported
    in layer 1.

    Groups are matched to layer ID according their `inkscape:label` attribute, their `id`
    attribute or their appearing order, in that order of priority. Labels are stripped of
    non-numeric characters and the remaining is used as layer ID. Lacking numeric characters,
    the appearing order is used. If the label is 0, its changed to 1.

    All curved geometries are chopped in segments no longer than the value of *quantization*.
    Optionally, the geometries are simplified using Shapely, using the value of *quantization*
    as tolerance.

    Args:
        filename: path of the SVG file
        quantization: maximum size of segment used to approximate curved geometries
        simplify: run Shapely's simplify on loaded geometry
        return_size: if True, return a size 3 Tuple containing the geometries and the SVG
            width and height

    Returns:
         imported geometries, and optionally width and height of the SVG
    """

    doc = svg.Document(filename)

    width, height, scale_x, scale_y, offset_x, offset_y = _calculate_page_size(doc.root)

    vector_data = VectorData()

    # non-group top level elements are loaded in layer 1
    top_level_elements = doc.flatten_all_paths(group_filter=lambda x: x is doc.root)
    if top_level_elements:
        vector_data.add(
            _convert_flattened_paths(
                top_level_elements,
                quantization,
                scale_x,
                scale_y,
                offset_x,
                offset_y,
                simplify,
            ),
            1,
        )

    for i, g in enumerate(doc.root.iterfind("svg:g", SVG_NAMESPACE)):
        # compute a decent layer ID
        lid_str = re.sub(
            "[^0-9]",
            "",
            g.get("{http://www.inkscape.org/namespaces/inkscape}label") or "",
        )
        if not lid_str:
            lid_str = re.sub("[^0-9]", "", g.get("id") or "")
        if lid_str:
            lid = int(lid_str)
            if lid == 0:
                lid = 1
        else:
            lid = i + 1

        vector_data.add(
            _convert_flattened_paths(
                flatten_group(g, g),
                quantization,
                scale_x,
                scale_y,
                offset_x,
                offset_y,
                simplify,
            ),
            lid,
        )

    if return_size:
        if width is None or height is None:
            _, _, width, height = vector_data.bounds() or 0, 0, 0, 0
        return vector_data, width, height
    else:
        return vector_data


# Traceback (IndexError)
Traceback (most recent call last):
File "/Users/theuser/.pyenv/versions/plotter/bin/vpype", line 8, in <module>
sys.exit(cli())
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype_cli/cli.py", line 72, in main
return super().main(args=preprocess_argument_list(args), **extra)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/click/core.py", line 1290, in invoke
return _process_result(rv)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/click/core.py", line 1224, in _process_result
value = ctx.invoke(self.result_callback, value, **ctx.params)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype_cli/cli.py", line 118, in process_pipeline
execute_processors(processors)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype_cli/cli.py", line 202, in execute_processors
state = proc(state)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype/decorators.py", line 154, in global_processor
state.vector_data = f(state.vector_data, *args, **kwargs)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype_cli/read.py", line 106, in read
read_svg(file, quantization=quantization, simplify=not no_simplify),
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/vpype/io.py", line 163, in read_svg
doc.flatten_all_paths(), quantization, scale_x, scale_y, offset_x, offset_y, simplify,
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/document.py", line 219, in flatten_all_paths
path_filter, path_conversions)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/document.py", line 142, in flatten_all_paths
path = transform(parse_path(converter(path_elem)), path_tf)
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/parser.py", line 91, in parse_path
segments.closed = True
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/path.py", line 2241, in closed
if value and not self._is_closable():
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/path.py", line 2219, in _is_closable
end = self[-1].end
File "/Users/theuser/.pyenv/versions/3.7.7/envs/plotter/lib/python3.7/site-packages/svgpathtools/path.py", line 2081, in __getitem__
return self._segments[index]
IndexError: list index out of range

# Fixed code
def read_multilayer_svg(
    filename: str,
    quantization: float,
    simplify: bool = False,
    return_size: bool = False,
) -> Union["VectorData", Tuple["VectorData", float, float]]:
    """Read a multilayer SVG file and return its content as a :class:`VectorData` instance
    retaining the SVG's layer structure.

    Each top-level group is considered a layer. All non-group, top-level elements are imported
    in layer 1.

    Groups are matched to layer ID according their `inkscape:label` attribute, their `id`
    attribute or their appearing order, in that order of priority. Labels are stripped of
    non-numeric characters and the remaining is used as layer ID. Lacking numeric characters,
    the appearing order is used. If the label is 0, its changed to 1.

    All curved geometries are chopped in segments no longer than the value of *quantization*.
    Optionally, the geometries are simplified using Shapely, using the value of *quantization*
    as tolerance.

    Args:
        filename: path of the SVG file
        quantization: maximum size of segment used to approximate curved geometries
        simplify: run Shapely's simplify on loaded geometry
        return_size: if True, return a size 3 Tuple containing the geometries and the SVG
            width and height

    Returns:
         imported geometries, and optionally width and height of the SVG
    """

    doc = svg.Document(filename)

    width, height, scale_x, scale_y, offset_x, offset_y = _calculate_page_size(doc.root)

    vector_data = VectorData()

    # non-group top level elements are loaded in layer 1
    top_level_elements = doc.paths(group_filter=lambda x: x is doc.root)
    if top_level_elements:
        vector_data.add(
            _convert_flattened_paths(
                top_level_elements,
                quantization,
                scale_x,
                scale_y,
                offset_x,
                offset_y,
                simplify,
            ),
            1,
        )

    for i, g in enumerate(doc.root.iterfind("svg:g", svg.SVG_NAMESPACE)):
        # compute a decent layer ID
        lid_str = re.sub(
            "[^0-9]",
            "",
            g.get("{http://www.inkscape.org/namespaces/inkscape}label") or "",
        )
        if not lid_str:
            lid_str = re.sub("[^0-9]", "", g.get("id") or "")
        if lid_str:
            lid = int(lid_str)
            if lid == 0:
                lid = 1
        else:
            lid = i + 1

        vector_data.add(
            _convert_flattened_paths(
                doc.paths_from_group(g, g),
                quantization,
                scale_x,
                scale_y,
                offset_x,
                offset_y,
                simplify,
            ),
            lid,
        )

    if return_size:
        if width is None or height is None:
            _, _, width, height = vector_data.bounds() or 0, 0, 0, 0
        return vector_data, width, height
    else:
        return vector_data