Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add option to use dataclasses instead of attrs in generated code#1158

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
eli-bl wants to merge2 commits intoopenapi-generators:main
base:main
Choose a base branch
Loading
frombenchling:dataclasses

Conversation

eli-bl
Copy link
Collaborator

@eli-bleli-bl commentedNov 5, 2024
edited
Loading

The last time this idea came up (#743),@dbanty felt that there wasn't a compelling reason to switch fromattrs. I decided anyway to see how much of a lift it would be— in the generated code, that is, not in the generator. A few reasons:

  1. In my main use case, which is creating a library to be used by all sorts of third-party applications, there's an incentive to minimize external dependencies if possible.
  2. Even thoughattrs is very popular, I think it's likely to become somewhat less of a standard as Python's dataclasses module keeps on copying features from it.
  3. It's true thatattrs has some features thatdataclasses doesn't, but we're not really using those features anyway in the generated code (with one exception that I'll mention below). Mostly we're just using the simplest possible attribute declaration pattern in model classes.

As I expected, implementing this for the model classes was pretty simple. The only other place we were usingattrs in the generated code was in theClient andAuthenticatedClient classes, and there things got trickier.

In those classes, we were taking advantage ofattrs'salias option in order to define underscored attributes that the app isn't meant to access directly, but that can still be set by name without the underscore in the initializer. You can't do that with dataclasses. However, since the app isn't meant to access them directly, I felt freer to change things there... and attrs/dataclass-type behavior is not really inherent to what the client classes are for, from the app's point of view, it's just an implementation detail.

So, inuse_dataclasses mode, I changed the clients to plain Python classes with no attrs/dataclass decorators—and moved those Httpx-related attributes into a separate dataclass, so we could still take advantage of copy-and-set behavior for builder methods. That is a fairly different implementation and code structure, which makes the Jinja template a bit hairy, but you can see what it ends up looking likehere. However, when you'renot inuse_dataclasses mode, the implementation does not change at all (you can see there are no changes to existing code ingolden_record), so I believe this is not a breaking change.

(Wecould decide to unify those implementations by backporting those changes to the not-using-dataclasses mode, so the only differences between the two would be the decorator names. I do think that'd be a little bit cleaner— the Httpx options do all go together, and I don't think there's a compelling reason to have them be separate attributes on the client class. And, again, apps were never meant to access those directly... but in Python, we couldn't prevent that, so thatcould be considered a breaking change depending on your standards for what is a public/supported interface.)

drsybren reacted with heart emoji
@eli-bl
Copy link
CollaboratorAuthor

Also, if my#1156 is accepted, this is another kind of thing that would be well suited to unit tests of the generated code. For instance, test classes for model behavior could be parameterized to run everything in both attrs mode and dataclasses mode.

@eli-bleli-bl marked this pull request as ready for reviewNovember 5, 2024 17:53
@eli-bleli-bl requested a review fromdbantyNovember 5, 2024 17:53
@eli-bleli-bl marked this pull request as draftNovember 14, 2024 01:36
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dbantydbantyAwaiting requested review from dbanty

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@eli-bl

[8]ページ先頭

©2009-2025 Movatter.jp