From aa403479622e93e809dd089ac99981c6b7989fe3 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 4 Nov 2022 12:11:16 +0000 Subject: [PATCH] do use orjson typing stubs after all --- src/ufoLib2/serde/json.py | 14 +++++++------- tox.ini | 2 -- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/ufoLib2/serde/json.py b/src/ufoLib2/serde/json.py index b6e27565..0913de09 100644 --- a/src/ufoLib2/serde/json.py +++ b/src/ufoLib2/serde/json.py @@ -1,17 +1,17 @@ from __future__ import annotations -from typing import Any, Type, cast +from typing import Any, Type from ufoLib2.converters import structure, unstructure from ufoLib2.typing import T have_orjson = False try: - import orjson as json # type: ignore + import orjson have_orjson = True except ImportError: - import json # type: ignore + import json def dumps( @@ -26,16 +26,16 @@ def dumps( if indent is not None: if indent != 2: raise ValueError("indent must be 2 or None for orjson") - kwargs["option"] = kwargs.pop("option", 0) | json.OPT_INDENT_2 + kwargs["option"] = kwargs.pop("option", 0) | orjson.OPT_INDENT_2 if sort_keys: - kwargs["option"] = kwargs.pop("option", 0) | json.OPT_SORT_KEYS + kwargs["option"] = kwargs.pop("option", 0) | orjson.OPT_SORT_KEYS # orjson.dumps always returns bytes - result = json.dumps(data, **kwargs) + result = orjson.dumps(data, **kwargs) else: # built-in json.dumps returns a string, not bytes, hence the encoding s = json.dumps(data, indent=indent, sort_keys=sort_keys, **kwargs) result = s.encode("utf-8") - return cast(bytes, result) + return result def loads(s: str | bytes, object_class: Type[T], **kwargs: Any) -> T: diff --git a/tox.ini b/tox.ini index 8d418c3e..e55a83b3 100644 --- a/tox.ini +++ b/tox.ini @@ -28,8 +28,6 @@ deps = commands = black --check --diff . isort --skip-gitignore --check-only --diff src tests - # typing stubs for orjson exist but I don't want mypy to use them! - pip uninstall -y orjson mypy --strict src tests flake8