openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

Move union parsing def outside of loop for additionalProperties

Open dbanty opened this issue 5 years ago • 0 comments

Right now if you have types specified for additionalProperties you get something like this:

        additional_properties = {}
        for prop_name, prop_dict in d.items():

            def _parse_additional_property(
                data: Any,
            ) -> Union[List[str], str]:
                data = None if isinstance(data, Unset) else data
                additional_property: Union[
                    List[str], str
                ]
                try:
                    additional_property = cast(List[str], data)

                    return additional_property
                except:  # noqa: E722
                    pass
                return cast(str, data)

            additional_property = _parse_additional_property(prop_dict)

            additional_properties[prop_name] = additional_property

I don't actually know what Python does with def inside a loop, it might optimize it somehow, but it at very least looks inefficient. We should do some special handling of Unions most likely to move the parse function outside the loop. Or maybe we make an outer parse function ourselves will all the parse logic and call it inside the loop so we don't have to muddle with a special case on Unions?

dbanty avatar Dec 20 '20 16:12 dbanty