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

SyntaxError: from __future__ imports must occur at the beginning of the file when custom header is used #2079

Open
Dezzley opened this issue Aug 22, 2024 · 0 comments

Comments

@Dezzley
Copy link

Dezzley commented Aug 22, 2024

Describe the bug
When a model is generated with a custom header, importing the resulting model module causes the following error:

SyntaxError: from __future__ imports must occur at the beginning of the file

This error occurs because the from __future__ statement must appear near the top of the module (see documentation)

To Reproduce
Generate model via the provided command below using any schema:

Used commandline:

$  datamodel-codegen --input "./openapi.yml" --input-file-type openapi --output ./model.py --output-model-type pydantic_v2.BaseModel --snake-case-field --target-python-version 3.10 --use-schema-description --use-field-description  --base-class CustomBaseModel --custom-file-header-path ./custom_pydantic_file_header.py

Expected behavior
Generated module could be imported without any issues.

Version:

  • OS: MacOS Sonoma 14.6.1
  • Python version: 3.10.14
  • datamodel-code-generator version: 0.25.9

Additional context
Consider hoisting the future statement if any custom header option is passed.

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