Skip to content

_degrees_to_index raises IndexError instead of ValueError for invalid inputs #2647

@aman-coder03

Description

@aman-coder03

Describe the issue

The helper function _degrees_to_index raises IndexError for invalid
input values and arguments, but these cases represent invalid values
rather than invalid indexing.

Using IndexError here is semantically incorrect and inconsistent with
standard Python and NumPy error conventions.

Location in code

In pvlib/tools.py (function _degrees_to_index):

raise IndexError("coordinate must be 'latitude' or 'longitude'.")
and later:

err = IndexError(
    'Input, %g, is out of range (%g, %g).' % (degrees, inputmin, inputmax)
)

Why this is a problem?

IndexError is intended for invalid indexing operations (e.g. list or
array indices).
These errors are caused by invalid argument values supplied by the
user.
The appropriate exception type in this context is ValueError.

Using ValueError would:

Better reflect the nature of the error
Align with Python and NumPy conventions
Improve API correctness and clarity for users

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