From 787976bd5fb85a197ca2feacda75f39b9f7dd523 Mon Sep 17 00:00:00 2001 From: dd84ai Date: Sun, 28 Jan 2024 21:41:43 +0100 Subject: [PATCH] fix: stubgen fix for NewType added --- Taskfile.yml | 1 + pyproject.toml | 2 +- stubgen_fix.py | 42 ++++++++++++++++++++++++++++++++++++++++++ typelog/types.py | 10 +++++----- 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 stubgen_fix.py diff --git a/Taskfile.yml b/Taskfile.yml index 0c8bc31..e4207a8 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -39,6 +39,7 @@ tasks: - rm -R typelog.egg-info | true - rm -R typelog-stubs | true - stubgen typelog -o stubs && mv stubs/typelog typelog-stubs && rm -R stubs + - python3 stubgen_fix.py - python3 -m build build:check: diff --git a/pyproject.toml b/pyproject.toml index dc53c7c..981c3a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "typelog" -version = "0.2.3" +version = "0.2.5" description = "Static typed structured logging" readme = "README.md" authors = [{ name = "dd84ai", email = "dark.dreamflyer@gmail.com" }] diff --git a/stubgen_fix.py b/stubgen_fix.py new file mode 100644 index 0000000..8529037 --- /dev/null +++ b/stubgen_fix.py @@ -0,0 +1,42 @@ +""" +Fix to stubgen +stubgen replaces NewType to Incomplete. +We discover files with NewType and +replace them in stubgen generated files back to NewType +""" +from pathlib import Path + +pathlist = Path(".").rglob('*.pyi') +for path in pathlist: + path_in_str = str(path) + if ".venv" in path_in_str: + continue + + with open(path_in_str, "r") as file: + pyi_file = file.readlines() + with open(path_in_str.replace("-stubs","").replace(".pyi", ".py"), "r") as file: + py_file = file.readlines() + + has_new_types = False + + # replace in pyi files NewType back to original code line + for py_line in py_file: + if "NewType" in py_line: + varname: str = py_line.split(" ")[0] + has_new_types = True + + for pyi_i, pyi_line in enumerate(pyi_file): + if f"{varname}: Incomplete\n" == pyi_line: + pyi_file[pyi_i] = py_line + + if has_new_types: + # replace from typing import back because + # stubgen autoremoved some of it + for py_line in py_file: + if "from typing" in py_line: + for pyi_i, pyi_line in enumerate(pyi_file): + if f"from typing" in pyi_line: + pyi_file[pyi_i] = py_line + + with open(path_in_str, "w") as file: + file.writelines(pyi_file) diff --git a/typelog/types.py b/typelog/types.py index 164be18..b6858ee 100644 --- a/typelog/types.py +++ b/typelog/types.py @@ -1,11 +1,11 @@ -from typing import Any, Callable, Dict +from typing import Any, Callable, Dict, NewType Serialazable = Any -LogAttrs = Dict[str, Serialazable] +LogAttrs = NewType("LogAttrs", Dict[str, Serialazable]) LogType = Callable[[LogAttrs], None] -LibName = str -LogLevel = int -RootLogLevel = LogLevel +LibName = NewType("LibName", str) +LogLevel = NewType("LogLevel", int) +RootLogLevel = NewType("RootLogLevel", int)