Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Feb 22, 2024
1 parent 6326d84 commit 6416a39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
40 changes: 9 additions & 31 deletions fgpyo/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,13 @@ def split_at_given_level(
decrease_in_depth += high_level_split.count(char)
outer_depth_of_split += increase_in_depth - decrease_in_depth

assert (
outer_depth_of_split >= 0
), "Unpaired depth character! Likely incorrect output"
assert outer_depth_of_split >= 0, "Unpaired depth character! Likely incorrect output"

current_outer_splits.append(high_level_split)
if outer_depth_of_split == 0:
out_vals.append(split_delim.join(current_outer_splits))
current_outer_splits = []
assert (
outer_depth_of_split == 0
), "Unpaired depth character! Likely incorrect output!"
assert outer_depth_of_split == 0, "Unpaired depth character! Likely incorrect output!"
return out_vals


Expand Down Expand Up @@ -149,9 +145,7 @@ def get_parser() -> partial:
if s == "{}"
else [
subtype_parser(item)
for item in set(
split_at_given_level(s[1:-1], split_delim=",")
)
for item in set(split_at_given_level(s[1:-1], split_delim=","))
]
)
)
Expand All @@ -177,9 +171,7 @@ def tuple_parse(tuple_string: str) -> Tuple[Any, ...]:
if len(tuple_string) == 0:
return ()
else:
val_strings = split_at_given_level(
tuple_string, split_delim=","
)
val_strings = split_at_given_level(tuple_string, split_delim=",")
return tuple(
parser(val_str)
for parser, val_str in zip(subtype_parsers, val_strings)
Expand Down Expand Up @@ -215,14 +207,10 @@ def dict_parse(dict_string: str) -> Dict[Any, Any]:
if len(dict_string) == 0:
return {}
else:
outer_splits = split_at_given_level(
dict_string, split_delim=","
)
outer_splits = split_at_given_level(dict_string, split_delim=",")
out_dict = {}
for outer_split in outer_splits:
inner_splits = split_at_given_level(
outer_split, split_delim=";"
)
inner_splits = split_at_given_level(outer_split, split_delim=";")
assert (
len(inner_splits) % 2 == 0
), "Inner splits of dict didn't have matched key val pairs"
Expand All @@ -242,18 +230,12 @@ def dict_parse(dict_string: str) -> Dict[Any, Any]:
elif typing.get_origin(type_) is Union:
return types.make_union_parser(
union=type_,
parsers=[
_get_parser(cls, arg, parsers)
for arg in typing.get_args(type_)
],
parsers=[_get_parser(cls, arg, parsers) for arg in typing.get_args(type_)],
)
elif typing.get_origin(type_) is Literal: # Py>=3.7.
return types.make_literal_parser(
type_,
[
_get_parser(cls, type(arg), parsers)
for arg in typing.get_args(type_)
],
[_get_parser(cls, type(arg), parsers) for arg in typing.get_args(type_)],
)
else:
raise ParserNotFoundException(
Expand Down Expand Up @@ -308,11 +290,7 @@ def attr_from(
# try setting by casting
# Note that while bools *can* be cast from string, all non-empty strings evaluate to
# True, because python, so we need to check for that explicitly
if (
not set_value
and attribute.type is not None
and not attribute.type == bool
):
if not set_value and attribute.type is not None and not attribute.type == bool:
try:
return_value = attribute.type(str_value)
set_value = True
Expand Down
5 changes: 1 addition & 4 deletions fgpyo/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import TypeVar
from typing import Union


UnionType = TypeVar("UnionType", bound="Union")
EnumType = TypeVar("EnumType", bound="Enum")
# conceptually bound to "Literal" but that's not valid in the spec
Expand Down Expand Up @@ -103,9 +102,7 @@ def _make_union_parser_worker(
raise ValueError(f"{value} could not be parsed as any of {union}")


def make_union_parser(
union: Type[UnionType], parsers: Iterable[Callable[[str], type]]
) -> partial:
def make_union_parser(union: Type[UnionType], parsers: Iterable[Callable[[str], type]]) -> partial:
"""Generates a parser function for a union type object and set of parsers for the possible
parsers to that union type object
"""
Expand Down

0 comments on commit 6416a39

Please sign in to comment.