Skip to content

Commit

Permalink
fix test and some style things
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueg committed May 20, 2024
1 parent 9413bdf commit 1cac2a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
68 changes: 37 additions & 31 deletions foca/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
)


def _validate_log_level_choices(cls, v: int) -> int:
def _validate_log_level_choices(cls, level: int) -> int:
"""Ensure that a valid logging level is set.
Args:
v: Log level choice to be validated.
level: Log level choice to be validated.
Returns:
Unmodified `level` value if validation succeeds.
Expand All @@ -46,10 +46,10 @@ def _validate_log_level_choices(cls, v: int) -> int:
ValueError: Raised if validation fails.
"""
CHOICES = [0, 10, 20, 30, 40, 50]
if v not in CHOICES:
raise ValueError(f"illegal log level specified: {v}")
if level not in CHOICES:
raise ValueError(f"illegal log level specified: {level}")

return v
return level


def _get_by_path(
Expand Down Expand Up @@ -336,18 +336,18 @@ def validate_exceptions_mapping(self) -> Self:
module_path = ".".join(split_module)
try:
mod = importlib.import_module(module_path)
except ModuleNotFoundError:
except ModuleNotFoundError as exc:
raise ValueError(
f"Module '{module_path}' referenced in field 'exceptions' "
"could not be found."
)
) from exc
try:
exc_dict = getattr(mod, exc_dict_name)
except AttributeError:
except AttributeError as exc:
raise ValueError(
f"Module '{module_path}' referenced in field 'exceptions' "
f"does not have attribute '{exc_dict_name}'."
)
) from exc
if not isinstance(exc_dict, dict):
raise ValueError(
f"Attribute '{exc_dict_name}' in module '{module_path}' "
Expand All @@ -360,9 +360,9 @@ def validate_exceptions_mapping(self) -> Self:
allowed_members += self.extension_members

# Set flag to determine if additional members are allowed
limited_members: bool = True if not (
limited_members: bool = not (
isinstance(self.extension_members, bool) and self.extension_members
) else False
)

# Ensure that status member is among required members
if self.status_member not in self.required_members:
Expand All @@ -379,30 +379,36 @@ def validate_exceptions_mapping(self) -> Self:
"Both public and private member filters are active, but at "
"most one is allowed."
)
if limited_members and self.public_members:
if not all(m in allowed_members for m in self.public_members):
raise ValueError(
"Public members have more fields than are allowed by "
"'required_members' and 'extension_members'."
)
if limited_members and self.private_members:
if not all(m in allowed_members for m in self.private_members):
raise ValueError(
"Private members have more fields than are allowed by "
"'required_members' and 'extension_members'."
)
if (
limited_members
and self.public_members
and any(m not in allowed_members for m in self.public_members)
):
raise ValueError(
"Public members have more fields than are allowed by "
"'required_members' and 'extension_members'."
)
if (
limited_members
and self.private_members
and any(m not in allowed_members for m in self.private_members)
):
raise ValueError(
"Private members have more fields than are allowed by "
"'required_members' and 'extension_members'."
)

# Ensure that each exception fulfills all requirements
for key, val in exc_dict.items():

# Keys are exceptions
try:
getattr(key, '__cause__')
except AttributeError:
except AttributeError as exc:
raise ValueError(
f"Key '{key}' in 'exceptions' dictionary does not appear "
"to be an Exception."
)
) from exc
# Values are dictionaries
if not isinstance(val, dict):
raise ValueError(
Expand All @@ -416,11 +422,11 @@ def validate_exceptions_mapping(self) -> Self:
obj=val,
key_sequence=keys,
)
except (KeyError, ValueError):
except (KeyError, ValueError) as exc:
raise ValueError(
f"Exception '{key}' in 'exceptions' dictionary does "
"not have all fields required by 'required_members'."
)
) from exc
# No forbidden members are present
if limited_members:
members = deepcopy(val)
Expand All @@ -442,11 +448,11 @@ def validate_exceptions_mapping(self) -> Self:
key_sequence=self.status_member,
)
status = int(status)
except (KeyError, ValueError):
except (KeyError, ValueError) as exc:
raise ValueError(
f"Status member in exception '{key}' cannot be cast to "
"type integer."
)
) from exc

# Set mapping
self.mapping = exc_dict
Expand Down Expand Up @@ -611,8 +617,8 @@ def make_paths_absolute_and_set_path_out(self) -> Self:
else [self.path]
)
self.path = [
path.resolve() if not path.is_absolute()
else path for path in paths
path if path.is_absolute()
else path.resolve() for path in paths
]
if self.path_out is None:
_path = self.path[0].resolve()
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

DIR = Path(__file__).parent / "test_files"
EXCEPTIONS_NO_DICT = []
EXCEPTIONS_NOT_NESTED = {'a': 'b'}
EXCEPTIONS_NOT_NESTED = {Exception: 'b'}
EXCEPTIONS_NOT_EXC = {'a': {'status': 400, 'title': 'Bad Request'}}
REQUIRED_MEMBERS = [['title'], ['status']]
MEMBER_TITLE = ['title']
Expand Down

0 comments on commit 1cac2a5

Please sign in to comment.