Skip to content

Improve Identity server_default rendering for Decimal values#470

Merged
sheinbergon merged 15 commits intoagronholm:masterfrom
NotCarlosSerrano:fix/identity-decimal-serialization
Mar 17, 2026
Merged

Improve Identity server_default rendering for Decimal values#470
sheinbergon merged 15 commits intoagronholm:masterfrom
NotCarlosSerrano:fix/identity-decimal-serialization

Conversation

@NotCarlosSerrano
Copy link
Contributor

Changes

Improve handling of Identity server defaults when rendering columns.

Previously, Identity attributes containing Decimal values (e.g. start, increment, minvalue, maxvalue) were not rendered correctly. This change inspects the Identity parameters and explicitly renders non-default values. When a value is a Decimal, it is now emitted as decimal.Decimal(...) and the required module import is added.

This ensures generated models correctly represent database identity configurations that rely on Decimal values.

Fixes #.

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) which would fail without your patch
  • You've added a new changelog entry (in CHANGES.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

@coveralls
Copy link

coveralls commented Mar 16, 2026

Coverage Status

coverage: 97.832% (-0.03%) from 97.861%
when pulling d761fc1 on NotCarlosSerrano:fix/identity-decimal-serialization
into 9234437 on agronholm:master.

@agronholm
Copy link
Owner

Deleting the only test that covers this is hardly an acceptable solution to making the test suite pass.

@NotCarlosSerrano
Copy link
Contributor Author

NotCarlosSerrano commented Mar 16, 2026

You're right,

I originally added a test expecting Decimal values to be rendered as decimal.Decimal(...), but after revisiting the implementation I changed the approach. Since the parameters of Identity (start, increment, minvalue, maxvalue, etc.) are defined as int | None, emitting Decimal values in the generated code didn't seem appropriate.

Instead, the implementation now normalizes Decimal values returned by some dialects to int. When I made that change, I removed the original test instead of updating it to reflect the new expected behavior.

I could add a dedicated test for the Decimal → int normalization if you think it's worth it, although the current Identity test should already cover the resulting behavior.

@agronholm
Copy link
Owner

@sheinbergon Thoughts on this one?

Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
@sheinbergon
Copy link
Collaborator

@sheinbergon Thoughts on this one?

I'm not really liking this explicit attribute selection. Let's see if it's required or not

@agronholm
Copy link
Owner

@sheinbergon Thoughts on this one?

I'm not really liking this explicit attribute selection. Let's see if it's required or not

Same here, I'd like to avoid that if possible, as we're doing everywhere else.

@NotCarlosSerrano
Copy link
Contributor Author

NotCarlosSerrano commented Mar 17, 2026

Good point, it would be better to avoid the explicit attribute selection, especially since that’s not how this is handled elsewhere in the generator. The intent here was only to render the non-default public Identity kwargs, not to introduce a separately maintained list.

I’ll take another pass and see if this can be implemented using the existing introspection-based approach instead. Does that direction make sense?

@sheinbergon
Copy link
Collaborator

Good point, it would be better to avoid the explicit attribute selection, especially since that’s not how this is handled elsewhere in the generator. The intent here was only to render the non-default public Identity kwargs, not to introduce a separately maintained list.

I’ll take another pass and see if this can be implemented using the existing introspection-based approach instead. Does that direction make sense?

Is there any problem with just getting all args, seeing if any non-null decimals are present and express them as ints?

@NotCarlosSerrano
Copy link
Contributor Author

That sounds better to me too. I don’t think the special handling needs to be tied specifically to an explicit set of Identity attributes. I can keep the existing rendering approach and only normalize non-null Decimal values to int before rendering, so this stays consistent with the rest of the generator.

@sheinbergon
Copy link
Collaborator

sheinbergon commented Mar 17, 2026

That sounds better to me too. I don’t think the special handling needs to be tied specifically to an explicit set of Identity attributes. I can keep the existing rendering approach and only normalize non-null Decimal values to int before rendering, so this stays consistent with the rest of the generator.

Much better, 10x 🙏. can you add a test to cover this behavior?

@NotCarlosSerrano
Copy link
Contributor Author

Done, I’ve pushed the changes including a test that covers the normalization of Decimal to int in Identity values.

I’m not sure if it’s a bit tricky to set the Decimal values directly in the test, but it seemed like the clearest way to simulate what might come from database reflection. Let me know if you’d like to see any additional cases covered.

@sheinbergon
Copy link
Collaborator

sheinbergon commented Mar 17, 2026

Done, I’ve pushed the changes including a test that covers the normalization of Decimal to int in Identity values.

I’m not sure if it’s a bit tricky to set the Decimal values directly in the test, but it seemed like the clearest way to simulate what might come from database reflection. Let me know if you’d like to see any additional cases covered.

Looks good. If the tests are generated the expected result (with respect to the issue you were trying to solve), it's all good

@sheinbergon sheinbergon self-requested a review March 17, 2026 19:28
Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple minor changes I'd like to see.

Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @sheinbergon feel free to merge unless you have more changes you'd like to see.

@sheinbergon sheinbergon merged commit 986d975 into agronholm:master Mar 17, 2026
8 checks passed
@NotCarlosSerrano NotCarlosSerrano deleted the fix/identity-decimal-serialization branch March 17, 2026 21:18
@NotCarlosSerrano
Copy link
Contributor Author

Thanks for the merge! 🙌
Do you know if there’s an upcoming release planned that will include this change? I’d like to start using it once it’s available.

@sheinbergon
Copy link
Collaborator

@NotCarlosSerrano Just released 4.0.3. Feel free to make more contribution if you have the time!

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

Successfully merging this pull request may close these issues.

4 participants