Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
MNT: shorten logic + docstring
  • Loading branch information
tacaswell committed Sep 13, 2022
commit bbf0cd2610eb253fdfee73276e0a57fb8a28851b
10 changes: 5 additions & 5 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,14 +1708,17 @@ def safe_first_element(obj):

def _safe_first_finite(obj, *, skip_nonfinite=True):
"""
Return the first non-None element in *obj*.
Return the first non-None (and optionally finite) element in *obj*.

This is a method for internal use.

This is an type-independent way of obtaining the first non-None element,
supporting both index access and the iterator protocol.
The first non-None element will be obtained when skip_none is True.
"""
def safe_isfinite(val):
if val is None:
return False
try:
return np.isfinite(val) if np.isscalar(val) else True
Comment thread
timhoffm marked this conversation as resolved.
except TypeError:
Expand Down Expand Up @@ -1743,10 +1746,7 @@ def safe_isfinite(val):
raise RuntimeError("matplotlib does not "
"support generators as input")
else:
return next(
val for val in obj
if val is not None and safe_isfinite(val)
)
return next(val for val in obj if safe_isfinite(val))


def sanitize_sequence(data):
Expand Down