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

Emit output without formatting to help investigate syntax errors #2315

Open
ncoghlan opened this issue Feb 10, 2025 · 2 comments
Open

Emit output without formatting to help investigate syntax errors #2315

ncoghlan opened this issue Feb 10, 2025 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ncoghlan
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I had messed up my custom template formatting, so a required line break was missing, so the class header line was getting concatenated to a preceding comment.

The result was a cryptic syntax error pointing to a context-free field declaration that otherwise looked fine.

Similar to #1969, I investigated by patching datamodel-code-generator locally to pass format_=False to the underlying parser call.

Describe the solution you'd like

Rather than anything more elaborate, the simplest approach to make these cases easier to investigate would be to offer a --skip-formatting CLI option (skip_formatting generate parameter) that was passed down as parser.parse(format_=not skip_formatting)

Describe alternatives you've considered

The only other idea that occurred to me is to save the unformatted output somewhere when generated the formatted output fails. That seemed like it would be harder to discover and explain than a --skip-formatting option, though.

@gaborbernat
Copy link
Collaborator

I'm not a super huge fan of having a lot of flags. We already have too many in my opinion. Instead, I would prefer if you would still write out the content, just not apply the formatting.

@ncoghlan
Copy link
Contributor Author

One way to achieve "write the output even if formatting fails" would be to do something like:

try:
    results = parser.parse()
except Exception as exc:
    warnings.warn(f"Failed to produce formatted output due to {exc!r}. Trying without formatting.")
    results = parser.parse(format_=False)

Would that approach be reasonable?

@ncoghlan ncoghlan changed the title --skip-formatting debugging option to help investigate syntax errors Emit output without formatting to help investigate syntax errors Feb 11, 2025
@gaborbernat gaborbernat added enhancement New feature or request help wanted Extra attention is needed labels Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants