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

fix: MultipleChoiceField use ordered sort#9735

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

Open
fbozhang wants to merge14 commits intoencode:main
base:main
Choose a base branch
Loading
fromfbozhang:master

Conversation

@fbozhang
Copy link

This modification has the following advantages:

  1. Maintain the input order.
  2. Prevent the output from being a set, which would cause issues with JSON serialization (e.g., when passing parameters to certain third-party SDKs that may internally use json.dumps, and I cannot modify the third party's encoder).

Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

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

please also add unit tests to verify this and make the CI green

@fbozhang
Copy link
Author

please also add unit tests to verify this and make the CI green

I’ve added the tests and the CI is green now.

auvipy reacted with thumbs up emoji

Copy link
Member

@browniebrokebrowniebroke left a comment

Choose a reason for hiding this comment

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

Seems sensible to me 👍🏻

However, it will have to wait for 3.17 as I see this as potentially disruptive for some downstream users. Left a small suggestion


assertserializer.is_valid()
assertserializer.validated_data['nested']['example']=={1,2}
assertserializer.validated_data['nested']['example']==[1,2]
Copy link
Member

Choose a reason for hiding this comment

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

Hum, that's a bit unfortunate that this data type is exposed at this level. I'm thinking of potential users testing at the serializer level might get some new test failures with this change.

Copy link
Author

Choose a reason for hiding this comment

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

image

I think we just need to note in the new version's documentation that the return type is a list, so that if a user upgrades and their tests fail, they'll know exactly what to look at.

Since the input, as mentioned in the signature, is already an ordered sequence (a list or a tuple), it would be much more user-friendly if the output keeps that same order.

@browniebrokebrowniebroke added this to the3.17 milestoneJul 6, 2025
@fbozhang
Copy link
Author

Seems sensible to me 👍🏻

However, it will have to wait for 3.17 as I see this as potentially disruptive for some downstream users. Left a small suggestion

Good point — waiting for 3.17 sounds like the right call. Appreciate the suggestion!

@fbozhangfbozhang deleted the master branchAugust 17, 2025 16:37
@fbozhangfbozhang restored the master branchAugust 17, 2025 16:39
@fbozhangfbozhang reopened thisAug 17, 2025
@stale
Copy link

stalebot commentedOct 18, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

fbozhang reacted with eyes emoji

@stalestalebot added the stale labelOct 18, 2025
Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR modifiesMultipleChoiceField to return ordered lists instead of sets, ensuring JSON serialization compatibility and maintaining the order of input values. The change usesdict.fromkeys() to deduplicate values while preserving insertion order.

Key Changes

  • Changedto_internal_value() andto_representation() methods to return lists usinglist(dict.fromkeys([...]))
  • Updated all test expectations from sets to lists
  • Added JSON serialization tests to prevent regression

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

FileDescription
rest_framework/fields.pyModifiedMultipleChoiceField methods to return ordered lists instead of sets usingdict.fromkeys() for deduplication
tests/test_fields.pyUpdated test expectations from sets to lists, added order-preserving test case, and added JSON serialization validation tests
tests/test_serializer_nested.pyUpdated nested serializer test assertions to expect lists instead of sets

💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.

Comment on lines 2181 to 2186
valid_inputs= {
():set(),
('aircon',): {'aircon'},
('aircon','manual'): {'aircon','manual'},
():list(),
('aircon',): ['aircon'],
('aircon','manual'): ['aircon','manual'],
('manual','aircon'): ['manual','aircon'],
}
Copy link

CopilotAIDec 5, 2025

Choose a reason for hiding this comment

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

Missing test case for duplicate values. The change to usedict.fromkeys() deduplicates values, but there's no test case to verify this behavior. Consider adding a test case like('aircon', 'aircon'): ['aircon'] to ensure duplicates are properly handled and deduplicated while maintaining order.

Copilot uses AI. Check for mistakes.
Comment on lines 2191 to 2194
outputs= [
(['aircon','manual','incorrect'], {'aircon','manual','incorrect'})
(['aircon','manual','incorrect'], ['aircon','manual','incorrect']),
(['manual','aircon','incorrect'], ['manual','aircon','incorrect']),
]
Copy link

CopilotAIDec 5, 2025

Choose a reason for hiding this comment

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

Missing test case for duplicate values in the outputs. Theto_representation method now deduplicates values while maintaining order, but there's no test to verify this behavior. Consider adding a test case with duplicate values like(['aircon', 'manual', 'aircon'], ['aircon', 'manual']) to ensure the deduplication logic works correctly.

Copilot uses AI. Check for mistakes.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@browniebrokebrowniebrokebrowniebroke left review comments

@auvipyauvipyauvipy left review comments

Copilot code reviewCopilotCopilot left review comments

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

3.17

Development

Successfully merging this pull request may close these issues.

3 participants

@fbozhang@browniebroke@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp