Skip to content

Commit

Permalink
Fix for uncompiled python.
Browse files Browse the repository at this point in the history
Models should now go deeper.
Closes #109
  • Loading branch information
thesadru authored Mar 31, 2023
1 parent 137107b commit 1137ec4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ per-file-ignores =
tests/**: D10, S10, F841
noxfile.py: I900

max-complexity = 12
max-complexity = 16
max-function-length = 100
max-line-length = 130

Expand Down
41 changes: 28 additions & 13 deletions genshin/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,39 @@ def __init__(self, _frame: int = 1, **data: typing.Any) -> None:
lang = data.pop("lang", None)

if lang is None:
frame = sys._getframe(_frame)
if frame.f_code.co_name == "<listcomp>":
frame = typing.cast("types.FrameType", frame.f_back)
assert frame
frames = [sys._getframe(_frame)]
while _frame <= 100: # ensure we give up in a reasonable amount of time
_frame += 1
try:
frame = sys._getframe(_frame)
except ValueError:
break

if isinstance(frame.f_locals.get("lang"), str):
lang = frame.f_locals["lang"]
if frame.f_code.co_name == "__init__" and frame.f_code.co_filename == __file__:
frames.append(frame)
break

for name, value in frame.f_locals.items():
if isinstance(value, (APIModel, client_base.BaseClient)):
lang = value.lang
for frame in frames:
if frame.f_code.co_name == "<listcomp>":
frame = typing.cast("types.FrameType", frame.f_back)
assert frame

if isinstance(frame.f_locals.get("lang"), str):
lang = frame.f_locals["lang"]

for name, value in frame.f_locals.items():
if isinstance(value, (APIModel, client_base.BaseClient)):
lang = value.lang

if lang:
break

if lang is None:
# validator, it's a skipper
if isinstance(frame.f_locals.get("cls"), type) and issubclass(frame.f_locals["cls"], APIModel):
lang = None
else:
raise Exception("lang not found", frame)
continue

else:
raise Exception("lang not found")

object.__setattr__(self, "lang", lang)
super().__init__(**data, lang=lang)
Expand Down

0 comments on commit 1137ec4

Please sign in to comment.