all: update pathlib and typing imports
- ID
6b1b41c- date
2026-07-26 08:24:15+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
dab5991- message
all: update pathlib and typing imports- changed files
Changed files
src/javascript_data_files/__init__.py (6922 → 6835)
diff --git a/src/javascript_data_files/__init__.py b/src/javascript_data_files/__init__.py
index 3e9ab9f..230c3ca 100644
--- a/src/javascript_data_files/__init__.py
+++ b/src/javascript_data_files/__init__.py
@@ -12,9 +12,9 @@ Think of this like the JSON module, but for JavaScript files.
import io
import json
-import pathlib
+from pathlib import Path
import textwrap
-import typing
+from typing import Any, TypeVar
import uuid
from .decoder import decode_from_js
@@ -31,10 +31,10 @@ __all__ = [
]
-T = typing.TypeVar("T")
+T = TypeVar("T")
-def read_js(p: pathlib.Path | str, *, varname: str) -> typing.Any:
+def read_js(p: Path | str, *, varname: str) -> Any:
"""
Read a JavaScript "data file".
@@ -48,12 +48,12 @@ def read_js(p: pathlib.Path | str, *, varname: str) -> typing.Any:
{'sides': 5, 'colour': 'red'}
"""
- p = pathlib.Path(p)
+ p = Path(p)
return decode_from_js(js_string=p.read_text(), varname=varname)
-def read_typed_js[T](p: pathlib.Path | str, *, varname: str, model: type[T]) -> T:
+def read_typed_js[T](p: Path | str, *, varname: str, model: type[T]) -> T:
"""
Read a JavaScript "data file".
@@ -69,9 +69,9 @@ def read_typed_js[T](p: pathlib.Path | str, *, varname: str, model: type[T]) ->
def write_js(
- p: pathlib.Path | str | io.TextIOBase | io.BufferedIOBase,
+ p: Path | str | io.TextIOBase | io.BufferedIOBase,
*,
- value: typing.Any,
+ value: Any,
varname: str,
ensure_ascii: bool = False,
sort_keys: bool = False,
@@ -96,8 +96,8 @@ def write_js(
p.write(js_string)
elif isinstance(p, io.BufferedIOBase):
p.write(js_string.encode("utf8"))
- elif isinstance(p, pathlib.Path) or isinstance(p, str):
- p = pathlib.Path(p)
+ elif isinstance(p, Path) or isinstance(p, str):
+ p = Path(p)
if p.is_dir():
raise IsADirectoryError(p)
@@ -127,7 +127,7 @@ def write_js(
raise TypeError(f"Cannot write JavaScript to {type(p)}!")
-def append_to_js_array(p: pathlib.Path | str, *, value: typing.Any) -> None:
+def append_to_js_array(p: Path | str, *, value: Any) -> None:
"""
Append a single value to an array in a JavaScript "data file".
@@ -141,7 +141,7 @@ def append_to_js_array(p: pathlib.Path | str, *, value: typing.Any) -> None:
appending, and re-writing the entire file.
"""
- p = pathlib.Path(p)
+ p = Path(p)
file_size = p.stat().st_size
json_to_append = (
@@ -179,7 +179,7 @@ def append_to_js_array(p: pathlib.Path | str, *, value: typing.Any) -> None:
raise ValueError(f"End of file {p!r} does not look like an array")
-def append_to_js_object(p: pathlib.Path | str, *, key: str, value: typing.Any) -> None:
+def append_to_js_object(p: Path | str, *, key: str, value: Any) -> None:
"""
Append a single key/value pair to a JSON object in a JavaScript "data file".
@@ -195,7 +195,7 @@ def append_to_js_object(p: pathlib.Path | str, *, key: str, value: typing.Any) -
appending, and re-writing the entire file.
"""
- p = pathlib.Path(p)
+ p = Path(p)
file_size = p.stat().st_size
enc_key = json.dumps(key)
src/javascript_data_files/decoder.py (2178 → 2152)
diff --git a/src/javascript_data_files/decoder.py b/src/javascript_data_files/decoder.py
index fb7533f..8e3b78f 100644
--- a/src/javascript_data_files/decoder.py
+++ b/src/javascript_data_files/decoder.py
@@ -8,10 +8,10 @@ to catch errors.
import json
import re
-import typing
+from typing import Any
-def decode_from_js(js_string: str, *, varname: str) -> typing.Any:
+def decode_from_js(js_string: str, *, varname: str) -> Any:
"""
Parse a string as a JavaScript value.
"""
@@ -27,9 +27,7 @@ def decode_from_js(js_string: str, *, varname: str) -> typing.Any:
return decode_from_json(json_string)
-def dict_with_unique_names(
- pairs: list[tuple[str, typing.Any]],
-) -> dict[str, typing.Any]:
+def dict_with_unique_names(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
"""
Convert a list of name/value pairs to a dict, but only if the
names are unique.
@@ -63,7 +61,7 @@ def dict_with_unique_names(
)
-def decode_from_json(json_string: str) -> typing.Any:
+def decode_from_json(json_string: str) -> Any:
"""
Parse a string as a JSON value.
"""
src/javascript_data_files/encoder.py (1420 → 1408)
diff --git a/src/javascript_data_files/encoder.py b/src/javascript_data_files/encoder.py
index 0283da7..364eef5 100644
--- a/src/javascript_data_files/encoder.py
+++ b/src/javascript_data_files/encoder.py
@@ -7,7 +7,7 @@ a compact encoding that uses less bytes on disk.
"""
import json
-import typing
+from typing import Any
class HumanReadableEncoder(json.JSONEncoder):
@@ -15,7 +15,7 @@ class HumanReadableEncoder(json.JSONEncoder):
A custom JSON encoder with a few niceties for human-readability.
"""
- def encode(self, o: typing.Any) -> str:
+ def encode(self, o: Any) -> str:
"""
Return a JSON string representation of a Python data structure, o.
"""
@@ -26,7 +26,7 @@ class HumanReadableEncoder(json.JSONEncoder):
def encode_as_json(
- value: typing.Any, *, ensure_ascii: bool = False, sort_keys: bool = False
+ value: Any, *, ensure_ascii: bool = False, sort_keys: bool = False
) -> str:
"""
Convert a Python value to a JSON-encoded string.
@@ -41,7 +41,7 @@ def encode_as_json(
def encode_as_js(
- value: typing.Any,
+ value: Any,
varname: str,
*,
ensure_ascii: bool = False,
src/javascript_data_files/validate_type.py (2493 → 2500)
diff --git a/src/javascript_data_files/validate_type.py b/src/javascript_data_files/validate_type.py
index b532ca0..a5f97b6 100644
--- a/src/javascript_data_files/validate_type.py
+++ b/src/javascript_data_files/validate_type.py
@@ -3,12 +3,12 @@ Helper methods for validating that an arbitrary blob matches a given model.
"""
import functools
-import typing
+from typing import Any, Hashable, TypeVar
from pydantic import ConfigDict, PydanticUserError, TypeAdapter
-T = typing.TypeVar("T")
+T = TypeVar("T")
@functools.cache
@@ -54,7 +54,7 @@ def _get_validator(model: type[T]) -> TypeAdapter[T]:
return TypeAdapter(model)
-def validate_type(t: typing.Any, *, model: type[T]) -> T:
+def validate_type(t: Any, *, model: type[T]) -> T:
"""
Check that some data matches a given type.
@@ -69,7 +69,7 @@ def validate_type(t: typing.Any, *, model: type[T]) -> T:
# Argument 1 to "__call__" of "_lru_cache_wrapper"
# has incompatible type "type[T]"; expected "Hashable"
#
- assert isinstance(model, typing.Hashable)
+ assert isinstance(model, Hashable)
validator = _get_validator(model)