Skip to content
Open
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
61 changes: 45 additions & 16 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,20 @@ def _args_flatten_map(self, args: List[FuncParam]) -> Dict[str, Tuple[str, ...]]

def _model_flatten_map(self, model: TModel, prefix: str) -> Generator:
field: FieldInfo
for attr, field in model.model_fields.items():
field_name = field.alias or attr
name = f"{prefix}{self.FLATTEN_PATH_SEP}{field_name}"
if is_pydantic_model(field.annotation):
yield from self._model_flatten_map(field.annotation, name) # type: ignore
else:
yield field_name, name
if get_origin(model) in UNION_TYPES:
# If the model is a union type, process each type in the union
for arg in get_args(model):
if type(arg) is None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied this patch locally to test things out and noticed that this is flipped.

Suggested change
if type(arg) is None:
if arg is type(None):

arg will be a type, and we want to check if it's NoneType.

continue # Skip NoneType
yield from self._model_flatten_map(arg, prefix)
else:
for attr, field in model.model_fields.items():
field_name = field.alias or attr
name = f"{prefix}{self.FLATTEN_PATH_SEP}{field_name}"
if is_pydantic_model(field.annotation):
yield from self._model_flatten_map(field.annotation, name) # type: ignore
else:
yield field_name, name

def _get_param_type(self, name: str, arg: inspect.Parameter) -> FuncParam:
# _EMPTY = self.signature.empty
Expand Down Expand Up @@ -278,7 +285,11 @@ def _get_param_type(self, name: str, arg: inspect.Parameter) -> FuncParam:
def is_pydantic_model(cls: Any) -> bool:
try:
if get_origin(cls) in UNION_TYPES:
return any(issubclass(arg, pydantic.BaseModel) for arg in get_args(cls))
return any(
issubclass(arg, pydantic.BaseModel)
for arg in get_args(cls)
if (type(arg) is not None)
)
return issubclass(cls, pydantic.BaseModel)
except TypeError:
return False
Expand Down Expand Up @@ -321,14 +332,32 @@ def detect_collection_fields(
for attr in path[1:]:
if hasattr(annotation_or_field, "annotation"):
annotation_or_field = annotation_or_field.annotation
annotation_or_field = next(
(
a
for a in annotation_or_field.model_fields.values()
if a.alias == attr
),
annotation_or_field.model_fields.get(attr),
) # pragma: no cover

# check union types
if get_origin(annotation_or_field) in UNION_TYPES:
for arg in get_args(annotation_or_field):
if type(arg) is None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if type(arg) is None:
if arg is type(None):

continue # Skip NoneType
if hasattr(arg, "model_fields"):
annotation_or_field = next(
(
a
for a in arg.model_fields.values()
if a.alias == attr
),
arg.model_fields.get(attr),
) # pragma: no cover
else:
continue
else:
annotation_or_field = next(
(
a
for a in annotation_or_field.model_fields.values()
if a.alias == attr
),
annotation_or_field.model_fields.get(attr),
) # pragma: no cover

annotation_or_field = getattr(
annotation_or_field, "outer_type_", annotation_or_field
Expand Down
Loading