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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.

<!-- unreleased things below this line -->

## v1.3.0 - 2026-07-21

* Fixed
* Fix grammar for `ucschar` character range (via [#45])
* Fix `RFC3987_SYNTAX_TERMS` correctly containing "ipath" (via [#41])
Expand All @@ -20,7 +22,6 @@ All notable changes to this project will be documented in this file.
* Added
* Function `is_valid_syntax` issues a warning if an unexpected error occurred (via [#51])
* New function `is_valid_syntax_scheme` (via [#48])
* New function `is_valid_syntax_non_zero` (via [#48])
* New function `is_valid_syntax_pct_encoded` (via [#48])
* New dict `RFC3987_SYNTAX_TERM_VALIDATORS` mapping `RFC3987_SYNTAX_TERMS` to validator function (via [#49])
* Performance
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "rfc3987-syntax2"
version = "1.2.0"
version = "1.3.0"
description = "Helper functions to syntactically validate strings according to RFC 3987."
readme = "README.md"
requires-python = ">=3.9"
Expand Down
3 changes: 2 additions & 1 deletion src/rfc3987_syntax2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Re-export of :mod:`.syntax_helpers`
"""

from typing import Any, TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from . import syntax_helpers as _syntax_helpers

Expand All @@ -18,6 +18,7 @@
from .syntax_helpers import *
# cannot simply re-export - need to respect lazy-loading


def __getattr__(name: str) -> Any:
if name in _EXPORTS_SH:
value = getattr(_syntax_helpers, name)
Expand Down
44 changes: 18 additions & 26 deletions src/rfc3987_syntax2/syntax_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

from pathlib import Path
from threading import Lock
from typing import Any, Callable, Optional, TYPE_CHECKING, Literal
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional
from warnings import warn

from lark import Lark, ParseTree, exceptions

from .utils import load_grammar


__all__ = [
"RFC3987_SYNTAX_PARSER_TYPE",
"RFC3987_SYNTAX_GRAMMAR_PATH",
Expand Down Expand Up @@ -59,7 +58,6 @@
"is_valid_syntax_ipv4address",
"is_valid_syntax_dec_octet",
"is_valid_syntax_digit",
"is_valid_syntax_non_zero",
"is_valid_syntax_unreserved",
"is_valid_syntax_alpha",
"is_valid_syntax_hexdig",
Expand Down Expand Up @@ -127,15 +125,15 @@
"""
_SYNTAX_PARSER_STARTS: list[T_SYNTAX_PARSER_TERM] = ["iri", "iri_reference", "absolute_iri"]


def parse(term: T_SYNTAX_PARSER_TERM, value: str) -> ParseTree:
"""Parse text as one of the top-level RFC 3987 syntax terms.

:param term: Start rule used for parsing. Must be one of ``"iri"``,
``"iri_reference"``, or ``"absolute_iri"``.
:param term: Start rule used for parsing.
Must be one of ``"iri"``, ``"iri_reference"``, or ``"absolute_iri"``.
:param value: Input text to parse.
:return: The Lark parse tree for ``value`` under the selected start rule.
:raises lark.exceptions.LarkError: If parser initialization fails or parsing
cannot be completed.
:raises lark.exceptions.LarkError: If parser initialization fails or parsing cannot be completed.
"""

return _get_syntax_parser().parse(value, start=term)
Expand All @@ -146,10 +144,11 @@ def is_valid_syntax(term: T_SYNTAX_PARSER_TERM, value: str) -> bool:

This is a boolean convenience wrapper around :func:`parse`.

.. warning:: Emits a ``RuntimeWarning`` (via :func:`warnings.warn`) when a non-``UnexpectedInput`` ``lark.exceptions.LarkError`` occurs.
.. warning:: Emits a ``RuntimeWarning`` (via :func:`warnings.warn`) when a non-``UnexpectedInput``
``lark.exceptions.LarkError`` occurs.

:param term: Start rule used for validation. Must be one of ``"iri"``,
``"iri_reference"``, or ``"absolute_iri"``.
:param term: Start rule used for validation.
Must be one of ``"iri"``, ``"iri_reference"``, or ``"absolute_iri"``.
:param value: Input text to validate.
:return: ``True`` if parsing succeeds; otherwise ``False``.
"""
Expand All @@ -163,11 +162,12 @@ def is_valid_syntax(term: T_SYNTAX_PARSER_TERM, value: str) -> bool:
# from Lark internals / initialization (non-UnexpectedInput)
warn("Unexpected LarkError (non-UnexpectedInput) "
f"for term={term!r}: {type(err).__name__}: {err}",
RuntimeWarning,
stacklevel=2)
RuntimeWarning,
stacklevel=2)
return False
return True


T_SYNTAX_VALIDATOR = Callable[[str], bool]
"""Callable validator for one RFC 3987 grammar rule.

Expand Down Expand Up @@ -212,6 +212,7 @@ def syntax_validator(text: str) -> bool:
_grammar: Optional[str] = None
_grammar_lock = Lock()


def _get_grammar() -> str:
"""this is private API"""

Expand All @@ -225,6 +226,7 @@ def _get_grammar() -> str:
_syntax_parser: Optional[Lark] = None
_syntax_parser_lock = Lock()


def _get_syntax_parser() -> Lark:
"""this is private API"""

Expand All @@ -239,6 +241,7 @@ def _get_syntax_parser() -> Lark:
parser=RFC3987_SYNTAX_PARSER_TYPE)
return _syntax_parser


if TYPE_CHECKING:
def is_valid_syntax_iri(text: str) -> bool:
"""Validate that input text conforms to the RFC 3987 ``IRI`` rule."""
Expand Down Expand Up @@ -486,19 +489,6 @@ def is_valid_syntax_digit(text: str) -> bool:
else:
is_valid_syntax_digit = make_syntax_validator("digit")

if TYPE_CHECKING:
def is_valid_syntax_non_zero(text: str) -> bool:
"""
Local helper token used in ``dec_octet``.

.. warning::
Deprecated: this is not a stable/public entry point and may be removed
in a future release.
"""
...
else:
is_valid_syntax_non_zero = make_syntax_validator("non_zero")

if TYPE_CHECKING:
def is_valid_syntax_unreserved(text: str) -> bool:
"""Validate that input text conforms to the RFC 3987 ``unreserved`` rule."""
Expand Down Expand Up @@ -571,7 +561,8 @@ def is_valid_syntax_pct_encoded(text: str) -> bool:
"ipv4address": is_valid_syntax_ipv4address,
"dec_octet": is_valid_syntax_dec_octet,
"digit": is_valid_syntax_digit,
"non_zero": is_valid_syntax_non_zero,
# non_zero - a local helper token used in `dec_octet`
"non_zero": make_syntax_validator("non_zero"),
"unreserved": is_valid_syntax_unreserved,
"alpha": is_valid_syntax_alpha,
"hexdig": is_valid_syntax_hexdig,
Expand Down Expand Up @@ -605,6 +596,7 @@ def __getattr__(name: str) -> Any:
return syntax_parser
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def __dir__() -> list[str]:
return sorted(set(globals().keys()) | set(__all__))

Expand Down
9 changes: 7 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import json
from pathlib import Path
from typing import TypedDict
from typing_extensions import Required, NotRequired

from typing_extensions import NotRequired, Required

TESTS_ROOT_DIR = Path(__file__).parent
TESTS_DATA_DIR = TESTS_ROOT_DIR / '_data'
Expand All @@ -15,6 +16,7 @@
'valid_syntax': TESTS_DATA_DIR / 'valid_syntax.json',
}


class SyntaxCase(TypedDict):
value: Required[str]
reason: Required[str]
Expand All @@ -23,12 +25,15 @@ class SyntaxCase(TypedDict):
conforms_with_rfc3987_semantics: NotRequired[bool]
semantics_notes: NotRequired[str]


T_syntax_file = dict[str, list[SyntaxCase]]


def valid_syntax_data() -> T_syntax_file:
with open(TESTS_DATA_FILES['valid_syntax'], "r", encoding="utf-8") as f:
return json.load(f) # type:ignore[no-any-return]


def invalid_syntax_data() -> T_syntax_file:
with open(TESTS_DATA_FILES['invalid_syntax'], "r", encoding="utf-8") as f:
return json.load(f) # type:ignore[no-any-return]
return json.load(f) # type:ignore[no-any-return]
1 change: 0 additions & 1 deletion tests/test_exports_exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
# SPDX-License-Identifier: MIT

import rfc3987_syntax2 as sut

2 changes: 1 addition & 1 deletion tests/test_is_valid_syntax_warning.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) 2026 Jan Kowalleck - modifications and maintenance
# SPDX-License-Identifier: MIT

import pytest
from unittest.mock import patch

import pytest
from lark import exceptions

import rfc3987_syntax2.syntax_helpers as sut
Expand Down
9 changes: 6 additions & 3 deletions tests/test_lazy_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import subprocess
import sys

import rfc3987_syntax2 as sut
from lark import Lark

import rfc3987_syntax2 as sut


def test_grammar_not_eager() -> None:
"""Regression test: grammar is not populated in rfc3987_syntax2.__dict__ immediately after import."""
result = subprocess.run(
[sys.executable, '-c', 'import rfc3987_syntax2; assert "grammar" not in vars(rfc3987_syntax2), "grammar was eagerly loaded on import"'],
[sys.executable, '-c',
'import rfc3987_syntax2; assert "grammar" not in vars(rfc3987_syntax2), "grammar was eagerly loaded on import"'],
capture_output=True,
text=True,
)
Expand All @@ -21,7 +23,8 @@ def test_grammar_not_eager() -> None:
def test_syntax_parser_not_eager() -> None:
"""Regression test: syntax_parser is not populated in rfc3987_syntax2.__dict__ immediately after import."""
result = subprocess.run(
[sys.executable, '-c', 'import rfc3987_syntax2; assert "syntax_parser" not in vars(rfc3987_syntax2), "syntax_parser was eagerly loaded on import"'],
[sys.executable, '-c',
'import rfc3987_syntax2; assert "syntax_parser" not in vars(rfc3987_syntax2), "syntax_parser was eagerly loaded on import"'],
capture_output=True,
text=True,
)
Expand Down
7 changes: 1 addition & 6 deletions tests/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

import rfc3987_syntax2 as sut

from . import (
valid_syntax_data,
invalid_syntax_data,
SyntaxCase,
T_syntax_file,
)
from . import SyntaxCase, T_syntax_file, invalid_syntax_data, valid_syntax_data


def syntax_data_as_params(src_cb: Callable[[], T_syntax_file]) -> Any:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_term_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@


@pytest.mark.parametrize("term", sut.RFC3987_SYNTAX_TERMS)
def test_term_validator_exists(term: str) -> None:
actual = sut.RFC3987_SYNTAX_TERM_VALIDATORS.get(term)
assert (actual is not None), f"missing func for {term}"


@pytest.mark.parametrize("term", (t for t in sut.RFC3987_SYNTAX_TERMS if t not in [
# non_zero - a local helper token used in `dec_octet`
'non_zero',
]))
def test_term_validator_as_expected(term: str) -> None:
expected = getattr(sut, f'is_valid_syntax_{term}')
actual = sut.RFC3987_SYNTAX_TERM_VALIDATORS[term]
Expand Down