Skip to content

Commit 66da361

Browse files
committed
Update stringify_model for Python 3.14
1 parent b0d1b26 commit 66da361

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

pyatv/support/__init__.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import functools
66
import logging
77
from os import environ, path
8-
from typing import Any, List, Sequence, Union
8+
from typing import Any, List, Sequence, Union, get_args, get_origin
99
import warnings
1010

1111
from google.protobuf.text_format import MessageToString
@@ -170,25 +170,22 @@ def stringify_model(model: BaseModel) -> Sequence[str]:
170170
It is assumed optional field does not contain other models (only basic types).
171171
"""
172172

173-
def _lookup_type(current_model: BaseModel, type_path: str) -> str:
174-
splitted_path = type_path.split(".", maxsplit=1)
175-
value = current_model.__annotations__[splitted_path[0]]
176-
if len(splitted_path) == 1:
177-
if value.__dict__.get("__origin__") is Union:
178-
return ", ".join(arg.__name__ for arg in value.__args__)
179-
return value.__name__
180-
return _lookup_type(value, splitted_path[1])
173+
def _lookup_type(current_model: BaseModel, field_name: str) -> str:
174+
value = type(current_model).model_fields[field_name].annotation
175+
if value is None:
176+
return "Any"
177+
if get_origin(value) is Union:
178+
return ", ".join(arg.__name__ for arg in get_args(value))
179+
return value.__name__
181180

182181
def _recurse_into(
183182
current_model: BaseModel, prefix: str, output: List[str]
184183
) -> Sequence[str]:
185184
for name, field in dict(current_model).items():
186-
if hasattr(field, "__annotations__"):
187-
_recurse_into(
188-
getattr(current_model, name), (prefix or "") + f"{name}.", output
189-
)
185+
if isinstance(field, BaseModel):
186+
_recurse_into(field, (prefix or "") + f"{name}.", output)
190187
else:
191-
field_type = _lookup_type(model, f"{prefix}{name}")
188+
field_type = _lookup_type(current_model, name)
192189
output.append(f"{prefix}{name} = {field} ({field_type})")
193190
return output
194191

0 commit comments

Comments
 (0)