Skip to content

Commit

Permalink
Merge pull request #51 from Gallaecio/speed-up-arg_to_iter
Browse files Browse the repository at this point in the history
Speed up arg_to_iter
  • Loading branch information
kmike authored Dec 20, 2023
2 parents 6670cde + 93d0dd7 commit de94c5c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- python-version: 3
Expand Down
22 changes: 9 additions & 13 deletions itemloaders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
"""
import inspect
from functools import partial
from typing import Generator

from itemadapter import is_item

_ITERABLE_SINGLE_VALUES = str, bytes
def arg_to_iter(arg):
"""Return an iterable based on *arg*.
If *arg* is a list, a tuple or a generator, it will be returned as is.
def arg_to_iter(arg):
"""Convert an argument to an iterable. The argument can be a None, single
value, or an iterable.
If *arg* is ``None``, an empty list will be returned.
Exception: if arg is a dict, [arg] will be returned
If *arg* is anything else, a list will be returned with *arg* as its only
item, i.e. ``[arg]``.
"""
if arg is None:
return []
elif (
hasattr(arg, "__iter__")
and not isinstance(arg, _ITERABLE_SINGLE_VALUES)
and not is_item(arg)
):
if isinstance(arg, (list, tuple, Generator)):
return arg
else:
return [arg]
return [arg]


def get_func_args(func, stripself=False):
Expand Down

0 comments on commit de94c5c

Please sign in to comment.