diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae7d26..e10efcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ All notable changes to this project will be documented in this file. * Fix `RFC3987_SYNTAX_TERMS` correctly containing "ipath_rootless" (via [#41]) * Fix `RFC3987_SYNTAX_TERMS` correctly containing "ipath_empty" (via [#41]) * Fix `ucschar` range (via [#45]) + * Fix `parse()`'s typehint for arg `term` (via [#50]) + Narrowed down to valid values: "iri", "iri_reference", "absolute_iri" + * Fix `is_valid_syntax`'s typehint for arg `term` (via [#50]) + Narrowed down to valid values: "iri", "iri_reference", "absolute_iri" * Added * Function `is_valid_syntax_scheme` (via [#48]) * Function `is_valid_syntax_non_zero` (via [#48]) @@ -28,6 +32,7 @@ All notable changes to this project will be documented in this file. [#45]: https://github.com/jkowalleck/rfc3987-syntax2/pull/45 [#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 ## v1.2.0 - 2026-07-16 diff --git a/src/rfc3987_syntax2/syntax_helpers.py b/src/rfc3987_syntax2/syntax_helpers.py index 8592598..dc51199 100644 --- a/src/rfc3987_syntax2/syntax_helpers.py +++ b/src/rfc3987_syntax2/syntax_helpers.py @@ -4,7 +4,7 @@ from pathlib import Path from threading import Lock -from typing import Any, Callable, Optional, TYPE_CHECKING +from typing import Any, Callable, Optional, TYPE_CHECKING, Literal from lark import Lark, ParseTree, exceptions @@ -110,11 +110,14 @@ "pct_encoded", ] -def parse(term: str, value: str) -> 'ParseTree': +_SYNTAX_PARSER_STARTS = ["iri", "iri_reference", "absolute_iri"] +_SYNTAX_PARSER_TERM = Literal["iri", "iri_reference", "absolute_iri"] + +def parse(term: _SYNTAX_PARSER_TERM, value: str) -> 'ParseTree': return _get_syntax_parser().parse(value, start=term) -def is_valid_syntax(term: str, value: str) -> bool: +def is_valid_syntax(term: _SYNTAX_PARSER_TERM, value: str) -> bool: try: parse(term=term, value=value) return True @@ -167,7 +170,7 @@ def _get_syntax_parser() -> Lark: with _syntax_parser_lock: if _syntax_parser is None: _syntax_parser = Lark(grammar, - start=["iri", "iri_reference", "absolute_iri"], + start=_SYNTAX_PARSER_STARTS, parser=RFC3987_SYNTAX_PARSER_TYPE) return _syntax_parser