Visitar URL original
feat(typing): add type annotations to whoosh.util.text by webdevsamran · Pull Request #175 · priya-sundaram-dev/whoosh · GitHub
Skip to content

feat(typing): add type annotations to whoosh.util.text - #175

Merged
priya-sundaram-dev merged 2 commits into
priya-sundaram-dev:mainfrom
webdevsamran:fix/issue-173
Sep 3, 2026
Merged

priya-sundaram-dev merged 2 commits into
priya-sundaram-dev:mainfrom
webdevsamran:fix/issue-173

Conversation

@webdevsamran

Copy link
Copy Markdown
Contributor

Fixes #173 (part of #121)

Adds parameter and return type annotations to all public functions in \src/whoosh/util/text.py:

  • \�yte(num: int) -> bytes\
  • \ irst_diff(a: Sequence[Any], b: Sequence[Any]) -> int\
  • \prefix_encode(a: str | bytes, b: str | bytes) -> bytes\
  • \prefix_encode_all(ls: Iterable[str]) -> Iterator[bytes]\
  • \prefix_decode_all(ls: Iterable[bytes]) -> Iterator[str]\

  • atural_key(s: str) -> tuple[int | str, ...]\

  • compile(pattern: str | re.Pattern[Any], flags: int = 0, verbose: bool = False) -> re.Pattern[Any]\
  • Internal _nkconv(i: str) -> int | str\

Also includes \ rom future import annotations, defensive coercions in prefix encoding helpers, and records the change under ## [Unreleased]\ in \CHANGELOG.md.

All local checks pass:

  • \ y check src/whoosh/util/text.py\

  • uff check src/whoosh/util/text.py\

  • uff format --check src/whoosh/util/text.py\
  • \pytest tests/test_sorting.py -q\ (37/37 passed)

webdevsamran and others added 2 commits September 3, 2026 17:59
…nnotations

The re.Pattern[str] return type on rcompile lets the type checker flag latent
inaccuracies it now surfaces:
- whoosh.analysis.analyzers: expression params widened to str | re.Pattern[str]
  (matching whoosh.analysis.tokenizers) so default_pattern is accepted.
- whoosh.qparser.dateparse: annotate parse() text: str / pos: int so
  Pattern.match overload resolution succeeds.

Keeps the ty pre-commit hook green for priya-sundaram-dev#175 (part of priya-sundaram-dev#121).
@priya-sundaram-dev

Copy link
Copy Markdown
Owner

Beautiful work, @webdevsamran — this is exactly what #173 asked for, and I love the extra care: the byte(i)/defensive coercions in the prefix helpers and the from __future__ import annotations are all the right calls. 🎉

One thing worth a note (nothing you did wrong): giving rcompile a real return type is a good change, and it did its job — it immediately surfaced two latent inaccuracies elsewhere that the ty pre-commit hook then flagged:

  • whoosh.analysis.analyzers annotated several expression params as str, even though they accept str | re.Pattern[str] (the tokenizers already got this right).
  • a couple of whoosh.qparser.dateparse parser classes call Pattern.match(text, pos) where text/pos were unannotated.

Rather than send those back to you (they're outside whoosh.util.text), I pushed a small companion commit to your branch (d7b2e04): I tightened rcompile to re.Pattern[str], widened the analyzer expression params to str | re.Pattern[str], and annotated the dateparse parse() signatures. ty, ruff, and the full test suite are all green locally (103 passing across dateparse/analysis/sorting). Once CI confirms, I'll merge.

Thank you for a genuinely clean, well-described PR — this is your second solid contribution and it's very appreciated. If you enjoyed this, #121 has plenty more whoosh.util.* and analysis modules in the same vein whenever you're up for it.

@priya-sundaram-dev
priya-sundaram-dev merged commit 515a7e6 into priya-sundaram-dev:main Sep 3, 2026
16 checks passed
priya-sundaram-dev added a commit that referenced this pull request Sep 12, 2026
Add type hints to the caching decorators using a Callable-bound TypeVar
(_F) so the wrapped function's signature is preserved for callers.
Annotate internal cache dicts/Counter and the cache_info/cache_clear
helpers. Continues the gradual-typing series (#172/#175/#179/#186).
priya-sundaram-dev added a commit that referenced this pull request Sep 12, 2026
Add type hints to whoosh.util package init: random_name/random_bytes,
make_binary_tree/make_weighted_tree, fib (+ _fib_cache), the now callable,
and the synchronized/unclosed decorators (signature-preserving _F TypeVar).
Continues the gradual-typing series (#172/#175/#179/#186, filelock, cache).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type hints for whoosh.util.text (good first issue)

2 participants