Skip to content

Commit 71f995e

Browse files
committed
Add type hints
1 parent c08e8d1 commit 71f995e

File tree

9 files changed

+48
-12
lines changed

9 files changed

+48
-12
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- name: Install dependencies
6363
run: |
6464
python -m pip install --upgrade pip
65-
pip install -v -e .[test]
65+
pip install -v -e .[test,stubtest]
6666
shell: bash
6767
- name: Run Cargo tests
6868
run: |
@@ -72,6 +72,9 @@ jobs:
7272
- name: Run pytest tests
7373
run: |
7474
pytest
75+
- name: Run Mypy stubtest
76+
run: |
77+
stubtest --ignore-disjoint-bases --allowlist stubtest-allowlist json_stream_rs_tokenizer
7578
- name: Save Rust/Cargo cache
7679
uses: actions/cache/save@v4
7780
if: always()

json_stream_rs_tokenizer/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class TokenType:
2424

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

3030
if _supports_bigint():
3131
RustTokenizer = _RustTokenizer
3232
else:
3333

34-
class RustTokenizer:
34+
class RustTokenizer: # type: ignore[no-redef]
3535
"""
3636
Rust tokenizer (fallback wrapper for integer conversion)
3737
"""
@@ -111,7 +111,7 @@ def load(fp, persistent=False):
111111
"""
112112
Run json-stream's `load` but using the Rust tokenizer.
113113
"""
114-
import json_stream
114+
import json_stream # type: ignore[import-untyped]
115115

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

127127
return json_stream.visit(fp, visitor, tokenizer=rust_tokenizer_or_raise())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
try:
22
from .cli import main
3-
except ImportError as e:
3+
except ImportError as _e:
44
raise ImportError(
55
"benchmark dependencies not installed, please consult the README"
6-
) from e
6+
) from _e
77

88
if __name__ == "__main__":
99
exit(main())

json_stream_rs_tokenizer/benchmark/app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from pathlib import Path
55
from tempfile import TemporaryDirectory
66

7-
import json_stream as js
8-
from contexttimer import Timer
9-
from json_stream.tokenizer import tokenize as pure_python_tokenizer
10-
from json_stream_to_standard_types import to_standard_types
7+
import json_stream as js # type: ignore[import-untyped]
8+
from contexttimer import Timer # type: ignore[import-untyped]
9+
from json_stream.tokenizer import tokenize as pure_python_tokenizer # type: ignore[import-untyped]
10+
from json_stream_to_standard_types import to_standard_types # type: ignore[import-untyped]
11+
1112
from tqdm import tqdm
1213

1314
import json_stream_rs_tokenizer as jsrs

json_stream_rs_tokenizer/benchmark/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sys import stderr
22

33
import typer
4-
from si_prefix import si_parse
4+
from si_prefix import si_parse # type: ignore[import-untyped]
55

66
from . import app
77

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Manually written type hints stub file until PyO3 supports stub generation.
3+
4+
See https://pyo3.rs/v0.27.1/python-typing-hints.html
5+
"""
6+
from typing import Any, IO, final
7+
8+
@final
9+
class RustTokenizer:
10+
# TODO: buffering default is actually -1 but Mypy insists on it being
11+
# ellipsis...
12+
def __new__(
13+
cls, stream: IO[Any], *, buffering: int = ..., correct_cursor: bool = True
14+
) -> RustTokenizer: ...
15+
16+
def park_cursor(self) -> None: ...
17+
18+
@property
19+
def remainder(self) -> str | bytes: ...
20+
21+
def supports_bigint() -> bool: ...
22+
23+
__all__ = [
24+
"RustTokenizer",
25+
"supports_bigint",
26+
]

json_stream_rs_tokenizer/py.typed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
partial

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"json-stream-rs-tokenizer[benchmark]",
4848
"json-stream==2.3.2",
4949
],
50+
"stubtest": [
51+
"mypy>=1,<2",
52+
"types-tqdm", # not specifying version b/c it should match tqdm
53+
],
5054
},
5155
classifiers=[
5256
"Programming Language :: Rust",

stubtest-allowlist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json_stream_rs_tokenizer.RustTokenizer-redefinition

0 commit comments

Comments
 (0)