Skip to content

Commit

Permalink
fix: an alternative fix for a mypy type error in error_message.py
Browse files Browse the repository at this point in the history
This fix replaces the change in ffeb0a2.
  • Loading branch information
chisholm authored Aug 8, 2024
1 parent 19553d8 commit c289747
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/dioptra/task_engine/error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def _is_valid_for_sub_schema(
resolver=jsonschema.validators.RefResolver.from_schema(full_schema),
)

return cast(bool, validator.is_valid(sub_instance))
return validator.is_valid(sub_instance)


def _one_of_too_many_alternatives_satisfied_message_lines(
Expand All @@ -280,11 +280,15 @@ def _one_of_too_many_alternatives_satisfied_message_lines(
in another, with indented lines.
"""

alt_names = _get_one_of_alternative_names(error.validator_value, schema)
# In this specific case, validator_value contains the oneOf alternative
# schemas, but the typing is such that mypy can't tell that's the case.
alt_schemas = cast(Iterable[Any], error.validator_value)

alt_names = _get_one_of_alternative_names(alt_schemas, schema)
error_desc = "Must be exactly one of: {}".format(", ".join(alt_names))

satisfied_alt_names = []
for alt_name, alt_schema in zip(alt_names, error.validator_value):
for alt_name, alt_schema in zip(alt_names, alt_schemas):
# Perform a little "mini" validation to determine which alternatives
# were satisfied, and describe them in the error message.
if _is_valid_for_sub_schema(schema, alt_schema, error.instance):
Expand Down Expand Up @@ -323,9 +327,13 @@ def _one_of_no_alternatives_satisfied_message_lines(

message_lines = []

# In this specific case, validator_value contains the oneOf alternative
# schemas, but the typing is such that mypy can't tell that's the case.
alt_schemas = cast(Iterable[Any], error.validator_value)

# First error message line describes the error in basic terms.

alt_names = _get_one_of_alternative_names(error.validator_value, schema)
alt_names = _get_one_of_alternative_names(alt_schemas, schema)
basic_desc = (
"Must be exactly one of: {}; all alternatives failed validation."
).format(", ".join(alt_names))
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ deps =
mypy_extensions
types-cryptography
types-freezegun
types-jsonschema
types-mypy-extensions
types-python-dateutil
types-PyYAML
Expand Down

0 comments on commit c289747

Please sign in to comment.