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

Missing lazy_import in model to_multipart() #931

Open
kgutwin opened this issue Jan 3, 2024 · 1 comment
Open

Missing lazy_import in model to_multipart() #931

kgutwin opened this issue Jan 3, 2024 · 1 comment

Comments

@kgutwin
Copy link
Contributor

kgutwin commented Jan 3, 2024

Describe the bug
In the end-to-end tests, the file golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py passes type checking, but would fail in a runtime call in a call to BodyUploadFileTestsUploadPost.to_multipart() at the following point:

    def to_multipart(self) -> Dict[str, Any]:
        some_file = self.some_file.to_tuple()

        some_object = (None, json.dumps(self.some_object.to_dict()).encode(), "application/json")

        some_nullable_object: Union[None, Tuple[None, bytes, str]]
        if isinstance(self.some_nullable_object, BodyUploadFileTestsUploadPostSomeNullableObject):  # raises NameError
            some_nullable_object = (None, json.dumps(self.some_nullable_object.to_dict()).encode(), "application/json")
        else:
            some_nullable_object = self.some_nullable_object

The exception raised is NameError: name 'BodyUploadFileTestsUploadPostSomeNullableObject' is not defined.

The analogous point in to_dict() is avoided because the required model is imported:

    def to_dict(self) -> Dict[str, Any]:
        from ..models.body_upload_file_tests_upload_post_some_nullable_object import (
            BodyUploadFileTestsUploadPostSomeNullableObject,
        )

        some_file = self.some_file.to_tuple()

        some_object = self.some_object.to_dict()

        some_nullable_object: Union[Dict[str, Any], None]
        if isinstance(self.some_nullable_object, BodyUploadFileTestsUploadPostSomeNullableObject):
            some_nullable_object = self.some_nullable_object.to_dict()
        else:
            some_nullable_object = self.some_nullable_object

It appears that model.py.jinja needs to be updated to include the necessary lazy imports:

    def to_dict(self) -> Dict[str, Any]:
        """Convert to a dict"""
    {% for lazy_import in model.lazy_imports | sort %}
        {{ lazy_import }}
    {% endfor %}
        {{ _to_dict() | indent(8) }}

{% if model.is_multipart_body %}
    def to_multipart(self) -> Dict[str, Any]:
        {{ _to_dict(multipart=True) | indent(8) }}
{% endif %}
@kgutwin
Copy link
Contributor Author

kgutwin commented Jan 3, 2024

Looking again, is there a reason why the check for if TYPE_CHECKING: is present at the top of model.py.jinja when importing the model's lazy imports?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant