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

Fix msgspec template to add field #1942

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ianbuss
Copy link

@ianbuss ianbuss commented May 1, 2024

With msgspec outputs, the order of generated fields sometimes results in invalid class definitions, with required and optional fields in the wrong order. Additionally, if using --snake-case-field, field aliases are not added.

This PR contains the following fixes:

  • The _has_field_assignment sort function is modified to sort optional fields last
  • The __str__ function for msgspec fields is updated to add an explicit None default for optional fields when no other default has been set.
  • The jinja template is modified to write out the field.field for annotated types.
  • Added a new unit test to and modified existing tests in test_main.py.

Fixes #1943
Fixes #1919
Fixes #1920

@ianbuss ianbuss force-pushed the retain-ordering-of-fields-with-snake-casing branch from 566fb56 to cf683e4 Compare May 9, 2024 19:23
Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@f703dff). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1942   +/-   ##
=======================================
  Coverage        ?   98.90%           
=======================================
  Files           ?       37           
  Lines           ?     4184           
  Branches        ?      973           
=======================================
  Hits            ?     4138           
  Misses          ?       29           
  Partials        ?       17           
Flag Coverage Δ
unittests 98.56% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

codspeed-hq bot commented May 9, 2024

CodSpeed Performance Report

Merging #1942 will not alter performance

Comparing ianbuss:retain-ordering-of-fields-with-snake-casing (ff5b2b3) with main (f703dff)

Summary

✅ 29 untouched benchmarks

@ianbuss ianbuss changed the title Adjust sorting function in msgspec fields to account for snake casing Fix msgspec template to add field May 10, 2024
@ianbuss ianbuss marked this pull request as draft May 10, 2024 11:47
@ianbuss ianbuss marked this pull request as ready for review May 10, 2024 13:26
@@ -6,7 +6,7 @@

from typing import Annotated, Optional

from msgspec import Meta, Struct
from msgspec import Meta, Struct, field

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'field' is not used.
@@ -6,7 +6,7 @@

from typing import Annotated, List, Optional, Union

from msgspec import Meta, Struct
from msgspec import Meta, Struct, field

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'field' is not used.
@@ -6,7 +6,7 @@

from typing import List, Optional

from msgspec import Meta, Struct
from msgspec import Meta, Struct, field

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'field' is not used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants