Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ All notable changes to this project will be documented in this file.
* Function `is_valid_syntax_non_zero` (via [#48])
* Function `is_valid_syntax_pct_encoded` (via [#48])
* Dict `RFC3987_SYNTAX_TERM_VALIDATORS` mapping `RFC3987_SYNTAX_TERMS` to validator function (via [#49])
* Function `is_valid_syntax` issues a warning if an unexpected error occurred (via [#51])
* Performance
* Public symbols use lazy loading (via [#43])
* Tests
Expand All @@ -33,6 +34,7 @@ All notable changes to this project will be documented in this file.
[#48]: https://github.com/jkowalleck/rfc3987-syntax2/pull/48
[#49]: https://github.com/jkowalleck/rfc3987-syntax2/pull/49
[#50]: https://github.com/jkowalleck/rfc3987-syntax2/pull/50
[#51]: https://github.com/jkowalleck/rfc3987-syntax2/pull/51
[#52]: https://github.com/jkowalleck/rfc3987-syntax2/pull/52

## v1.2.0 - 2026-07-16
Expand Down
16 changes: 13 additions & 3 deletions src/rfc3987_syntax2/syntax_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from threading import Lock
from typing import Any, Callable, Optional, TYPE_CHECKING, Literal
from warnings import warn

from lark import Lark, ParseTree, exceptions

Expand Down Expand Up @@ -120,9 +121,17 @@ def parse(term: _T_SYNTAX_PARSER_TERM, value: str) -> 'ParseTree':
def is_valid_syntax(term: _T_SYNTAX_PARSER_TERM, value: str) -> bool:
try:
parse(term=term, value=value)
return True
except exceptions.LarkError:
except exceptions.UnexpectedInput:
# from Lark.parse()
return False
except exceptions.LarkError as err:
# from Lark internals / initialization (non-UnexpectedInput)
warn("Unexpected LarkError (non-UnexpectedInput) "
f"for term={term!r}: {type(err).__name__}: {err}",
RuntimeWarning,
stacklevel=2)
return False
return True

_T_SYNTAX_VALIDATOR = Callable[[str], bool]

Expand All @@ -139,9 +148,10 @@ def syntax_validator(text: str) -> bool:
parser=RFC3987_SYNTAX_PARSER_TYPE)
try:
parser.parse(text)
return True
except exceptions.LarkError:
return False
else:
return True
Comment thread
jkowalleck marked this conversation as resolved.

return syntax_validator

Expand Down
22 changes: 22 additions & 0 deletions tests/test_is_valid_syntax_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2026 Jan Kowalleck - modifications and maintenance
# SPDX-License-Identifier: MIT

import pytest
from unittest.mock import patch

from lark import exceptions

import rfc3987_syntax2.syntax_helpers as sut


def _raise_lark_error(*args: object, **kwargs: object) -> None:
raise exceptions.LarkError("unexpected test error")


def test_is_valid_syntax_warns_on_unexpected_lark_error() -> None:
with patch.object(sut, "parse", side_effect=_raise_lark_error):
with pytest.warns(RuntimeWarning, match=r"Unexpected LarkError"):
result = sut.is_valid_syntax(
term='unsupported-term', # type: ignore[arg-type] # on purpose
value="any-value")
assert result is False