Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor the msgspec factory to use the fields API #409

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions polyfactory/factories/msgspec_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Callable,
Generic,
TypeVar,
cast,
)

from typing_extensions import get_type_hints
Expand All @@ -23,7 +22,7 @@

try:
import msgspec
from msgspec import inspect
from msgspec.structs import fields
except ImportError as e:
msg = "msgspec is not installed"
raise MissingDependencyException(msg) from e
Expand Down Expand Up @@ -56,12 +55,11 @@ def is_supported_type(cls, value: Any) -> TypeGuard[type[T]]:

@classmethod
def get_model_fields(cls) -> list[FieldMeta]:
type_info = cast(inspect.StructType, inspect.type_info(cls.__model__))

fields_meta: list[FieldMeta] = []

for field in type_info.fields:
annotation = get_type_hints(cls.__model__, include_extras=True)[field.name]
type_hints = get_type_hints(cls.__model__, include_extras=True)
for field in fields(cls.__model__):
annotation = type_hints[field.name]
if field.default is not msgspec.NODEFAULT:
default_value = field.default
elif field.default_factory is not msgspec.NODEFAULT:
Expand Down
Loading