From b55dab42211e8a79d297d9d590a05e764e0d02ee Mon Sep 17 00:00:00 2001 From: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:30:34 +0200 Subject: [PATCH] Full mypy compliance --- .pre-commit-config.yaml | 8 +-- pyproject.toml | 2 +- src/json_repair/json_repair.py | 114 ++++----------------------------- 3 files changed, 17 insertions(+), 107 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8386b3c..8197a78 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,10 +20,10 @@ repos: rev: 24.8.0 hooks: - id: black -#- repo: https://github.com/pre-commit/mirrors-mypy/ -# rev: "v1.10.0" -# hooks: -# - id: mypy +- repo: https://github.com/pre-commit/mirrors-mypy/ + rev: "v1.11.1" + hooks: + - id: mypy - repo: local hooks: - id: run-tests diff --git a/pyproject.toml b/pyproject.toml index 2374b3c..c924482 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "json_repair" -version = "0.28.1" +version = "0.28.2" license = {file = "LICENSE"} authors = [ { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" }, diff --git a/src/json_repair/json_repair.py b/src/json_repair/json_repair.py index 7e40df7..7c5c6a7 100644 --- a/src/json_repair/json_repair.py +++ b/src/json_repair/json_repair.py @@ -24,7 +24,7 @@ import os import json -from typing import Any, Dict, List, Optional, Union, TextIO, Tuple, overload, Literal +from typing import Any, Dict, List, Optional, Union, TextIO, Tuple, Literal class StringFileWrapper: @@ -33,7 +33,7 @@ def __init__(self, fd: TextIO) -> None: self.fd = fd self.length: int = 0 - def __getitem__(self, index: int) -> str: + def __getitem__(self, index: int | slice) -> str: if isinstance(index, slice): self.fd.seek(index.start) value = self.fd.read(index.stop - index.start) @@ -564,7 +564,7 @@ def parse_boolean_or_null(self) -> Union[bool, str, None]: # is one of the literal strings 'true', 'false', or 'null' (unquoted) starting_index = self.index char = (self.get_char_at() or "").lower() - value = None + value: Optional[Tuple[str, Optional[bool]]] if char == "t": value = ("true", True) elif char == "f": @@ -632,57 +632,13 @@ def log(self, text: str, level: str) -> None: ) -@overload def repair_json( json_str: str = "", - return_objects: Optional[Literal[False]] = False, - skip_json_loads: Optional[bool] = False, - logging: Optional[Literal[False]] = False, # None is treated as False + return_objects: bool = False, + skip_json_loads: bool = False, + logging: bool = False, json_fd: Optional[TextIO] = None, - ensure_ascii: Optional[bool] = True, -) -> str: ... - - -@overload -def repair_json( - json_str: str = "", - return_objects: Literal[True] = True, - skip_json_loads: Optional[bool] = False, - logging: Optional[Literal[False]] = False, # None is treated as False - json_fd: Optional[TextIO] = None, - ensure_ascii: Optional[bool] = True, -) -> JSONReturnType: ... - - -@overload -def repair_json( - json_str: str = "", - return_objects: Optional[Literal[False]] = False, # None is treated as False - skip_json_loads: Optional[bool] = False, - logging: Literal[True] = True, - json_fd: Optional[TextIO] = None, - ensure_ascii: Optional[bool] = True, -) -> Tuple[str, List[Dict[str, str]]]: ... - - -@overload -def repair_json( - json_str: str = "", - return_objects: Literal[True] = True, - skip_json_loads: Optional[bool] = False, - logging: Literal[True] = True, - json_fd: Optional[TextIO] = None, - ensure_ascii: Optional[bool] = True, -) -> Tuple[JSONReturnType, List[Dict[str, str]]]: ... - - -def repair_json( - json_str: str = "", - return_objects: Optional[bool] = False, - skip_json_loads: Optional[bool] = False, - logging: Optional[bool] = False, - json_fd: Optional[TextIO] = None, - ensure_ascii: Optional[bool] = True, + ensure_ascii: bool = True, ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: """ Given a json formatted string, it will try to decode it and, if it fails, it will try to fix it. @@ -709,26 +665,10 @@ def repair_json( return json.dumps(parsed_json, ensure_ascii=ensure_ascii) -@overload -def loads( - json_str: str, - skip_json_loads: Optional[bool] = False, - logging: Optional[Literal[False]] = False, # None is treated as False -) -> JSONReturnType: ... - - -@overload def loads( json_str: str, - skip_json_loads: Optional[bool] = False, - logging: Literal[True] = True, -) -> Tuple[JSONReturnType, List[Dict[str, str]]]: ... - - -def loads( - json_str: str, - skip_json_loads: Optional[bool] = False, - logging: Optional[bool] = False, + skip_json_loads: bool = False, + logging: bool = False, ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: """ This function works like `json.loads()` except that it will fix your JSON in the process. @@ -742,22 +682,8 @@ def loads( ) -@overload def load( - fd: TextIO, - skip_json_loads: Optional[bool] = False, - logging: Optional[Literal[False]] = False, -) -> JSONReturnType: ... - - -@overload -def load( - fd: TextIO, skip_json_loads: Optional[bool] = False, logging: Literal[True] = True -) -> Tuple[JSONReturnType, List[Dict[str, str]]]: ... - - -def load( - fd: TextIO, skip_json_loads: Optional[bool] = False, logging: Optional[bool] = False + fd: TextIO, skip_json_loads: bool = False, logging: bool = False ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: """ This function works like `json.load()` except that it will fix your JSON in the process. @@ -771,26 +697,10 @@ def load( ) -@overload -def from_file( - filename: str, - skip_json_loads: Optional[bool] = False, - logging: Optional[Literal[False]] = False, -) -> JSONReturnType: ... - - -@overload -def from_file( - filename: str, - skip_json_loads: Optional[bool] = False, - logging: Literal[True] = True, -) -> Tuple[JSONReturnType, List[Dict[str, str]]]: ... - - def from_file( filename: str, - skip_json_loads: Optional[bool] = False, - logging: Optional[bool] = False, + skip_json_loads: bool = False, + logging: bool = False, ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: """ This function is a wrapper around `load()` so you can pass the filename as string