diff --git a/craft_grammar/models.py b/craft_grammar/models.py index 8233a93..b824369 100644 --- a/craft_grammar/models.py +++ b/craft_grammar/models.py @@ -121,36 +121,33 @@ def _validate_grammar_list( # noqa: PLR0912 pass else: continue - if issubclass(type_, str): - if isinstance(item, dict): - errors.append( - pydantic_core.InitErrorDetails( - type="value_error", - loc=(index,), - input=item, - ctx={ - "error": ValueError( - f"value must be a str or valid grammar dict: {input_value!r}", - ), - }, - ), - ) - else: - raise pydantic.ValidationError.from_exception_data( - title=f"Grammar[{type_.__name__}]", - line_errors=[ - pydantic_core.InitErrorDetails( - type="string_type", - loc=(), - input=item, - ), - ], - ) - break - else: + if not issubclass(type_, str): raise ValueError( # noqa: TRY004 _format_type_error(type_, input_value), ) + if not isinstance(item, dict): + raise pydantic.ValidationError.from_exception_data( + title=f"Grammar[{type_.__name__}]", + line_errors=[ + pydantic_core.InitErrorDetails( + type="string_type", + loc=(), + input=item, + ), + ], + ) + errors.append( + pydantic_core.InitErrorDetails( + type="value_error", + loc=(index,), + input=item, + ctx={ + "error": ValueError( + f"value must be a str or valid grammar dict: {input_value!r}", + ), + }, + ), + ) if errors: raise pydantic.ValidationError.from_exception_data(