Skip to content

Commit

Permalink
Full mypy compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
mangiucugna committed Aug 19, 2024
1 parent 4014630 commit b55dab4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 107 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
114 changes: 12 additions & 102 deletions src/json_repair/json_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -564,7 +564,7 @@ def parse_boolean_or_null(self) -> Union[bool, str, None]:
# <boolean> 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":
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit b55dab4

Please sign in to comment.