Skip to content

Commit

Permalink
fix: stubgen fix for NewType added
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 28, 2024
1 parent f78edbf commit 787976b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]
Expand Down
42 changes: 42 additions & 0 deletions stubgen_fix.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 5 additions & 5 deletions typelog/types.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 787976b

Please sign in to comment.