From 5124ace94f586465cff418482b0e518c14394242 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:38:18 +0100 Subject: [PATCH 1/3] fix(typing): Confirm arg type before joining in `_deduplicate_enum_errors` Fixes `mypy` error that appeared during a merge https://github.com/vega/altair/pull/3467#issuecomment-2225201532 --- altair/utils/schemapi.py | 19 +++++++++++++++---- tools/schemapi/schemapi.py | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/altair/utils/schemapi.py b/altair/utils/schemapi.py index 8f71247da..3e7c5ed68 100644 --- a/altair/utils/schemapi.py +++ b/altair/utils/schemapi.py @@ -409,6 +409,20 @@ def _group_errors_by_validator(errors: ValidationErrorList) -> GroupedValidation return dict(errors_by_validator) +def _safe_validator_value_iter(errors: ValidationErrorList, /) -> Iterator[str]: + for err in errors: + if isinstance(err.validator_value, Iterable): + yield ",".join(err.validator_value) + else: + msg = ( + "It is assumed that values (and therefore `validator_value`) of an enum are always arrays.\n" + "Therefore, this error should be unreachable.\n" + "see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values\n\n" + f"{type(err.validator_value).__name__!r} is not `Iterable`." + ) + raise TypeError(msg) + + def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList: """Deduplicate enum errors by removing the errors where the allowed values are a subset of another error. For example, if `enum` contains two errors @@ -417,10 +431,7 @@ def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList `enum` list only contains the error with ["A", "B", "C"]. """ if len(errors) > 1: - # Values (and therefore `validator_value`) of an enum are always arrays, - # see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values - # which is why we can use join below - value_strings = [",".join(err.validator_value) for err in errors] + value_strings = list(_safe_validator_value_iter(errors)) longest_enums: ValidationErrorList = [] for value_str, err in zip(value_strings, errors): if not _contained_at_start_of_one_of_other_values(value_str, value_strings): diff --git a/tools/schemapi/schemapi.py b/tools/schemapi/schemapi.py index b8bbe34c2..dd1d3f452 100644 --- a/tools/schemapi/schemapi.py +++ b/tools/schemapi/schemapi.py @@ -407,6 +407,20 @@ def _group_errors_by_validator(errors: ValidationErrorList) -> GroupedValidation return dict(errors_by_validator) +def _safe_validator_value_iter(errors: ValidationErrorList, /) -> Iterator[str]: + for err in errors: + if isinstance(err.validator_value, Iterable): + yield ",".join(err.validator_value) + else: + msg = ( + "It is assumed that values (and therefore `validator_value`) of an enum are always arrays.\n" + "Therefore, this error should be unreachable.\n" + "see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values\n\n" + f"{type(err.validator_value).__name__!r} is not `Iterable`." + ) + raise TypeError(msg) + + def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList: """Deduplicate enum errors by removing the errors where the allowed values are a subset of another error. For example, if `enum` contains two errors @@ -415,10 +429,7 @@ def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList `enum` list only contains the error with ["A", "B", "C"]. """ if len(errors) > 1: - # Values (and therefore `validator_value`) of an enum are always arrays, - # see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values - # which is why we can use join below - value_strings = [",".join(err.validator_value) for err in errors] + value_strings = list(_safe_validator_value_iter(errors)) longest_enums: ValidationErrorList = [] for value_str, err in zip(value_strings, errors): if not _contained_at_start_of_one_of_other_values(value_str, value_strings): From 4d8b28ab9db65ced31a6de4457729df68daae89c Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:18:37 +0100 Subject: [PATCH 2/3] revert: undo last commit From 74fb3d796d596f317915a113cf6ddb3e5a2528a1 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:33:25 +0100 Subject: [PATCH 3/3] revert: Ignore type error introduced by `typeshed` The issue is not related to any runtime changes https://github.com/python-jsonschema/jsonschema/pull/1019#issuecomment-2225773286 --- altair/utils/schemapi.py | 19 ++++--------------- tools/schemapi/schemapi.py | 19 ++++--------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/altair/utils/schemapi.py b/altair/utils/schemapi.py index 3e7c5ed68..f8e445064 100644 --- a/altair/utils/schemapi.py +++ b/altair/utils/schemapi.py @@ -409,20 +409,6 @@ def _group_errors_by_validator(errors: ValidationErrorList) -> GroupedValidation return dict(errors_by_validator) -def _safe_validator_value_iter(errors: ValidationErrorList, /) -> Iterator[str]: - for err in errors: - if isinstance(err.validator_value, Iterable): - yield ",".join(err.validator_value) - else: - msg = ( - "It is assumed that values (and therefore `validator_value`) of an enum are always arrays.\n" - "Therefore, this error should be unreachable.\n" - "see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values\n\n" - f"{type(err.validator_value).__name__!r} is not `Iterable`." - ) - raise TypeError(msg) - - def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList: """Deduplicate enum errors by removing the errors where the allowed values are a subset of another error. For example, if `enum` contains two errors @@ -431,7 +417,10 @@ def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList `enum` list only contains the error with ["A", "B", "C"]. """ if len(errors) > 1: - value_strings = list(_safe_validator_value_iter(errors)) + # Values (and therefore `validator_value`) of an enum are always arrays, + # see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values + # which is why we can use join below + value_strings = [",".join(err.validator_value) for err in errors] # type: ignore longest_enums: ValidationErrorList = [] for value_str, err in zip(value_strings, errors): if not _contained_at_start_of_one_of_other_values(value_str, value_strings): diff --git a/tools/schemapi/schemapi.py b/tools/schemapi/schemapi.py index dd1d3f452..22d0e2a28 100644 --- a/tools/schemapi/schemapi.py +++ b/tools/schemapi/schemapi.py @@ -407,20 +407,6 @@ def _group_errors_by_validator(errors: ValidationErrorList) -> GroupedValidation return dict(errors_by_validator) -def _safe_validator_value_iter(errors: ValidationErrorList, /) -> Iterator[str]: - for err in errors: - if isinstance(err.validator_value, Iterable): - yield ",".join(err.validator_value) - else: - msg = ( - "It is assumed that values (and therefore `validator_value`) of an enum are always arrays.\n" - "Therefore, this error should be unreachable.\n" - "see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values\n\n" - f"{type(err.validator_value).__name__!r} is not `Iterable`." - ) - raise TypeError(msg) - - def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList: """Deduplicate enum errors by removing the errors where the allowed values are a subset of another error. For example, if `enum` contains two errors @@ -429,7 +415,10 @@ def _deduplicate_enum_errors(errors: ValidationErrorList) -> ValidationErrorList `enum` list only contains the error with ["A", "B", "C"]. """ if len(errors) > 1: - value_strings = list(_safe_validator_value_iter(errors)) + # Values (and therefore `validator_value`) of an enum are always arrays, + # see https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values + # which is why we can use join below + value_strings = [",".join(err.validator_value) for err in errors] # type: ignore longest_enums: ValidationErrorList = [] for value_str, err in zip(value_strings, errors): if not _contained_at_start_of_one_of_other_values(value_str, value_strings):