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
19 changes: 18 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -v -e .[test]
pip install -v -e .[test,stubtest]
shell: bash
- name: Run Cargo tests
run: |
Expand All @@ -72,6 +72,23 @@ jobs:
- name: Run pytest tests
run: |
pytest
- name: Run Mypy stubtest
# XXX doesn't work on Pypy due to some symtable issues and on Windows
# with Python 3.8 due to pathlib issues...
if: >
!startsWith(matrix.python-version, 'pypy')
&& !(
startsWith(matrix.python-version, '3.8')
&& startsWith(matrix.os, 'windows-')
)
run: >
stubtest
${{
!startsWith(matrix.python-version, '3.8')
&& '--ignore-disjoint-bases'
|| ''
}}
--allowlist stubtest-allowlist json_stream_rs_tokenizer
- name: Save Rust/Cargo cache
uses: actions/cache/save@v4
if: always()
Expand Down
8 changes: 4 additions & 4 deletions json_stream_rs_tokenizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class TokenType:

# included only for backwards-compatibility - to the outside world, bigint
# is now always supported via fallback to conversion in Python
def supports_bigint():
def supports_bigint() -> bool:
return True

if _supports_bigint():
RustTokenizer = _RustTokenizer
else:

class RustTokenizer:
class RustTokenizer: # type: ignore[no-redef]
"""
Rust tokenizer (fallback wrapper for integer conversion)
"""
Expand Down Expand Up @@ -111,7 +111,7 @@ def load(fp, persistent=False):
"""
Run json-stream's `load` but using the Rust tokenizer.
"""
import json_stream
import json_stream # type: ignore[import-untyped]

return json_stream.load(
fp, persistent, tokenizer=rust_tokenizer_or_raise()
Expand All @@ -122,6 +122,6 @@ def visit(fp, visitor):
"""
Run json-stream's `visit` but using the Rust tokenizer.
"""
import json_stream
import json_stream # type: ignore[import-untyped]

return json_stream.visit(fp, visitor, tokenizer=rust_tokenizer_or_raise())
4 changes: 2 additions & 2 deletions json_stream_rs_tokenizer/benchmark/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
try:
from .cli import main
except ImportError as e:
except ImportError as _e:
raise ImportError(
"benchmark dependencies not installed, please consult the README"
) from e
) from _e

if __name__ == "__main__":
exit(main())
9 changes: 5 additions & 4 deletions json_stream_rs_tokenizer/benchmark/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from pathlib import Path
from tempfile import TemporaryDirectory

import json_stream as js
from contexttimer import Timer
from json_stream.tokenizer import tokenize as pure_python_tokenizer
from json_stream_to_standard_types import to_standard_types
import json_stream as js # type: ignore[import-untyped]
from contexttimer import Timer # type: ignore[import-untyped]
from json_stream.tokenizer import tokenize as pure_python_tokenizer # type: ignore[import-untyped]
from json_stream_to_standard_types import to_standard_types # type: ignore[import-untyped]

from tqdm import tqdm

import json_stream_rs_tokenizer as jsrs
Expand Down
2 changes: 1 addition & 1 deletion json_stream_rs_tokenizer/benchmark/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sys import stderr

import typer
from si_prefix import si_parse
from si_prefix import si_parse # type: ignore[import-untyped]

from . import app

Expand Down
26 changes: 26 additions & 0 deletions json_stream_rs_tokenizer/json_stream_rs_tokenizer.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Manually written type hints stub file until PyO3 supports stub generation.
See https://pyo3.rs/v0.27.1/python-typing-hints.html
"""
from typing import Any, IO, final

@final
class RustTokenizer:
# TODO: buffering default is actually -1 but Mypy insists on it being
# ellipsis...
def __new__(
cls, stream: IO[Any], *, buffering: int = ..., correct_cursor: bool = True
) -> RustTokenizer: ...

def park_cursor(self) -> None: ...

@property
def remainder(self) -> str | bytes: ...

def supports_bigint() -> bool: ...

__all__ = [
"RustTokenizer",
"supports_bigint",
]
1 change: 1 addition & 0 deletions json_stream_rs_tokenizer/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
partial
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"json-stream-rs-tokenizer[benchmark]",
"json-stream==2.3.2",
],
"stubtest": [
"mypy>=1,<2",
"types-tqdm", # not specifying version b/c it should match tqdm
],
},
classifiers=[
"Programming Language :: Rust",
Expand Down
1 change: 1 addition & 0 deletions stubtest-allowlist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json_stream_rs_tokenizer.RustTokenizer-redefinition
Loading