From 17868ac3877b444eab0016d0132b0448f604843b Mon Sep 17 00:00:00 2001 From: user-11150 <3584434540@qq.com> Date: Mon, 12 Aug 2024 22:46:48 +0800 Subject: [PATCH] Add typing hints --- Makefile | 2 +- src/objprint/decorator.py | 14 +++++++------- src/objprint/frame_analyzer.py | 2 +- src/objprint/objprint.py | 32 +++++++++++++++++--------------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 418fbd1..ee0c7ac 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ release: lint: flake8 --exclude src/objprint/executing src/ tests/ --count --max-line-length=127 --ignore=W503 - mypy src/ --exclude src/objprint/executing --follow-imports=skip + mypy src/ --exclude src/objprint/executing --follow-imports=skip --strict test: python -m unittest diff --git a/src/objprint/decorator.py b/src/objprint/decorator.py index cfcc00a..1e10c10 100644 --- a/src/objprint/decorator.py +++ b/src/objprint/decorator.py @@ -3,28 +3,28 @@ import functools -from typing import Callable, Optional, Type, Set, Union +from typing import Any, Callable, Optional, Set, Union def add_objprint( - orig_class: Optional[Type] = None, - format: str = "string", **kwargs) -> Union[Type, Callable[[Type], Type]]: + orig_class: Optional[type] = None, + format: str = "string", **kwargs: Any) -> Union[type, Callable[[type], type]]: from . import _objprint if format == "json": import json - def __str__(self) -> str: + def __str__(self: Any) -> str: return json.dumps(_objprint.objjson(self), **kwargs) else: - def __str__(self) -> str: + def __str__(self: Any) -> str: cfg = _objprint._configs.overwrite(**kwargs) - memo: Optional[Set] = set() if cfg.skip_recursion else None + memo: Optional[Set[Any]] = set() if cfg.skip_recursion else None return _objprint._get_custom_object_str(self, memo, indent_level=0, cfg=cfg) if orig_class is None: - def wrapper(cls: Type) -> Type: + def wrapper(cls: type) -> type: cls.__str__ = functools.wraps(cls.__str__)(__str__) # type: ignore return cls return wrapper diff --git a/src/objprint/frame_analyzer.py b/src/objprint/frame_analyzer.py index a3a55ea..2d1039c 100644 --- a/src/objprint/frame_analyzer.py +++ b/src/objprint/frame_analyzer.py @@ -7,7 +7,7 @@ import tokenize from types import FrameType from typing import List, Optional -from .executing.executing import Source # type: ignore +from .executing.executing import Source class FrameAnalyzer: diff --git a/src/objprint/objprint.py b/src/objprint/objprint.py index 922a0fb..6364865 100644 --- a/src/objprint/objprint.py +++ b/src/objprint/objprint.py @@ -8,7 +8,7 @@ import json import re from types import FunctionType, FrameType -from typing import Any, Callable, Iterable, List, Optional, Set, TypeVar, Type +from typing import Any, Callable, Dict, Iterable, List, Optional, Set, TypeVar, Type, Tuple from .color_util import COLOR, set_color from .frame_analyzer import FrameAnalyzer @@ -34,7 +34,7 @@ class _PrintConfig: skip_recursion: bool = True honor_existing: bool = True - def __init__(self, **kwargs): + def __init__(self, **kwargs: Any) -> None: for key, val in kwargs.items(): if hasattr(self, key): if isinstance(val, type(getattr(self, key))): @@ -44,7 +44,7 @@ def __init__(self, **kwargs): else: raise ValueError(f"{key} is not configurable") - def set(self, **kwargs) -> None: + def set(self, **kwargs: Any) -> None: for key, val in kwargs.items(): if hasattr(_PrintConfig, key): if isinstance(val, type(getattr(_PrintConfig, key))): @@ -54,7 +54,7 @@ def set(self, **kwargs) -> None: else: raise ValueError(f"{key} is not configurable") - def overwrite(self, **kwargs) -> "_PrintConfig": + def overwrite(self, **kwargs: Any) -> "_PrintConfig": ret = _PrintConfig(**kwargs) return ret @@ -62,7 +62,7 @@ def overwrite(self, **kwargs) -> "_PrintConfig": class ObjPrint: FormatterInfo = namedtuple('FormatterInfo', ['formatter', 'inherit']) - def __init__(self): + def __init__(self) -> None: self._configs = _PrintConfig() self.indicator_map = { @@ -73,9 +73,9 @@ def __init__(self): } self._sys_print = print self.frame_analyzer = FrameAnalyzer() - self.type_formatter = {} + self.type_formatter: Dict[type, ObjPrint.FormatterInfo] = {} - def __call__(self, *objs: Any, file: Any = None, format: str = "string", **kwargs) -> Any: + def __call__(self, *objs: Any, file: Any = None, format: str = "string", **kwargs: Any) -> Any: cfg = self._configs.overwrite(**kwargs) if cfg.enable: # if inspect.currentframe() returns None, set call_frame to None @@ -102,6 +102,7 @@ def __call__(self, *objs: Any, file: Any = None, format: str = "string", **kwarg if format == "json": if cfg.arg_name: + assert args is not None for arg, obj in zip(args, objs): self._sys_print(arg) self._sys_print(json.dumps(self.objjson(obj), **kwargs)) @@ -112,6 +113,7 @@ def __call__(self, *objs: Any, file: Any = None, format: str = "string", **kwarg # Force color with cfg as if color is not in cfg, objstr will default to False kwargs["color"] = cfg.color if cfg.arg_name: + assert args is not None for arg, obj in zip(args, objs): self._sys_print(arg) self._sys_print(self.objstr(obj, **kwargs), file=file) @@ -125,7 +127,7 @@ def __call__(self, *objs: Any, file: Any = None, format: str = "string", **kwarg return objs[0] if len(objs) == 1 else objs - def objstr(self, obj: Any, **kwargs) -> str: + def objstr(self, obj: Any, **kwargs: Any) -> str: # If no color option is specified, don't use color if "color" not in kwargs: kwargs["color"] = False @@ -141,7 +143,7 @@ def _objstr(self, obj: Any, memo: Optional[Set[int]], indent_level: int, cfg: _P if cls in self.type_formatter and ( cls == obj_type or self.type_formatter[cls].inherit ): - return self.type_formatter[cls].formatter(obj) + return self.type_formatter[cls].formatter(obj) # type: ignore # If it's builtin type, return it directly if isinstance(obj, str): @@ -217,7 +219,7 @@ def _objjson(self, obj: Any, memo: Set[int]) -> Any: return ret - def _get_custom_object_str(self, obj: Any, memo: Optional[Set[int]], indent_level: int, cfg: _PrintConfig): + def _get_custom_object_str(self, obj: Any, memo: Optional[Set[int]], indent_level: int, cfg: _PrintConfig) -> str: def _get_method_line(attr: str) -> str: if cfg.color: @@ -264,7 +266,7 @@ def _get_line(key: str) -> str: return self._get_pack_str(elems, obj, indent_level, cfg) - def _get_line_number_str(self, curr_frame: Optional[FrameType], cfg: _PrintConfig): + def _get_line_number_str(self, curr_frame: Optional[FrameType], cfg: _PrintConfig) -> str: if curr_frame is None: return "Unknown Line Number" curr_code = curr_frame.f_code @@ -279,7 +281,7 @@ def enable(self) -> None: def disable(self) -> None: self.config(enable=False) - def config(self, **kwargs) -> None: + def config(self, **kwargs: Any) -> None: self._configs.set(**kwargs) def install(self, name: str = "op") -> None: @@ -325,13 +327,13 @@ def unregister_formatter(self, *obj_types: Type[Any]) -> None: if obj_type in self.type_formatter: del self.type_formatter[obj_type] - def get_formatter(self) -> dict: + def get_formatter(self) -> Dict[type, "ObjPrint.FormatterInfo"]: return self.type_formatter - def _get_header_footer(self, obj: Any, cfg: _PrintConfig): + def _get_header_footer(self, obj: Any, cfg: _PrintConfig) -> Tuple[str, str]: obj_type = type(obj) if obj_type in self.indicator_map: - indicator = self.indicator_map[obj_type] + indicator = self.indicator_map[obj_type] # type: ignore return indicator[0], indicator[1] else: if cfg.color: