Skip to content

Commit

Permalink
chore: revert vendored changes
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Nov 21, 2022
1 parent 01ba97f commit 388340e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/bentoml/_internal/utils/lazy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
name: str,
warning: str | None = None,
exc_msg: str | None = None,
exc: t.Type[Exception] = MissingDependencyException,
exc: type[Exception] = MissingDependencyException,
):
self._local_name = local_name
self._parent_module_globals = parent_module_globals
Expand Down
51 changes: 26 additions & 25 deletions src/bentoml/_internal/utils/unflatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,48 @@
from __future__ import absolute_import

import re
import sys
import typing as t
from operator import itemgetter

if sys.version_info[0] == 2:
string_type = basestring # noqa: F821
else:
string_type = str


def unflatten(arg: dict[str, t.Any]) -> dict[str, t.Any]:
"""Unflatten nested dict/array data.
This function takes a single argument which may either be a
``dict`` (or any object having a dict-like ``.items()``) or
a sequence of ``(key, value)`` pairs.
``dict`` (or any object having a dict-like ``.items()`` or
``.iteritems()`` method) or a sequence of ``(key, value)`` pairs.
The keys in the ``dict`` or sequence should must all be strings.
Examples:
Nested ``dict``:
.. code-block:: python
unflatten({'foo.bar': 'val'})
# {'foo': {'bar': 'val'}}
Examples
--------
Nested ``list``:
Nested ``dict``\\s::
.. code-block:: python
>>> unflatten({'foo.bar': 'val'})
{'foo': {'bar': 'val'}}
unflatten({'foo[0]': 'val', 'foo[1]': 'bar'})
# {'foo': ['val', 'bar']}
Nested ``list``::
Nested ``list``:
>>> unflatten({'foo[0]': 'val', 'foo[1]': 'bar'})
{'foo': ['val', 'bar']}
.. code-block:: python
Nested ``list``\\s::
unflatten({'foo[0][0]': 'val'})
# {'foo': [['val']]}
>>> unflatten({'foo[0][0]': 'val'})
{'foo': [['val']]}
Lists of ``dict``:
Lists of ``dict``\\s::
.. code-block:: python
>>> unflatten({'foo[0].bar': 'val',
... 'foo[1].baz': 'x'})
{'foo': [{'bar': 'val'}, {'baz': 'x'}]}
unflatten({'foo[0].bar': 'val', 'foo[1].baz': 'x'})
# {'foo': [{'bar': 'val'}, {'baz': 'x'}]}
"""
if hasattr(arg, "items"):
items = arg.items()
Expand All @@ -92,7 +93,7 @@ def unflatten(arg: dict[str, t.Any]) -> dict[str, t.Any]:
parsed_key = _parse_key(flat_key)
obj = data
for depth, (key, next_key) in enumerate(zip(parsed_key, parsed_key[1:]), 1):
if isinstance(next_key, str):
if isinstance(next_key, string_type):
holder_type = _dict_holder
else:
holder_type = _list_holder
Expand Down Expand Up @@ -178,7 +179,7 @@ def getvalue(self) -> list[t.Any]:


def _parse_key(flat_key: str):
if not isinstance(flat_key, str):
if not isinstance(flat_key, string_type):
raise TypeError("keys must be strings")

split_key = _dot_or_indexes_re.split(flat_key)
Expand Down Expand Up @@ -233,7 +234,7 @@ def _parse_key(flat_key: str):
def _unparse_key(parsed: list[t.Any]) -> str:
bits: list[str] = []
for part in parsed:
if isinstance(part, str):
if isinstance(part, string_type):
if part.isidentifier():
fmt = ".%s" if bits else "%s"
elif part == "":
Expand Down

0 comments on commit 388340e

Please sign in to comment.