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

Make Field constructors keyword-only #7632

Merged
merged 1 commit into from
Aug 6, 2021

Conversation

akx
Copy link
Contributor

@akx akx commented Nov 10, 2020

Description

This PR makes the Field constructor keyword-only by adding a * (and turning *args, **kwargs into just **kwargs in subclasses). I don't believe anyone is (or should be) constructing fields as serializers.CharField(False, True, False, serializers.empty, ...)

The root issue this fixes is the nonobvious problem in e.g.

aliases = serializers.ListField(serializers.CharField(), default=list)

– you wouldn't expect that yields a non-validated read-only list field, would you? 😄 What happens is the positional serializers.CharField() argument gets passed up into the serializers.Field() call as the first argument, which just so happens to be read_only, and the field instance is a truthy value...

/cc @magdapoppins for having initially bumped into this issue.

@akx akx force-pushed the field-kwargs-only branch from ffe82b8 to 25ead1d Compare November 10, 2020 12:37
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

could you please fix the conflicts?

@akx akx force-pushed the field-kwargs-only branch from 25ead1d to 0a1cc37 Compare April 16, 2021 08:10
@akx
Copy link
Contributor Author

akx commented Apr 16, 2021

@auvipy Rebased.

@akx akx force-pushed the field-kwargs-only branch from 0a1cc37 to a094f59 Compare April 19, 2021 15:23
@akx
Copy link
Contributor Author

akx commented Jul 22, 2021

@auvipy Can we get this merged? :)

@auvipy
Copy link
Member

auvipy commented Aug 5, 2021

I am not maintainer :D

@akx
Copy link
Contributor Author

akx commented Aug 6, 2021

Fair. @tomchristie or someone else, can you take a look?

@tomchristie
Copy link
Member

Yup seems pretty reasonable.
We'll want to call it out clearly in the release notes, tho.

@tomchristie tomchristie merged commit fdb4931 into encode:master Aug 6, 2021
@tomchristie
Copy link
Member

Based on past experience I'd expect that someone somewhere may well have some code updating to do after this change, but let's see. And yes it clearly makes sense.

@tomchristie
Copy link
Member

Based on past experience I'd expect that someone somewhere may well have some code updating to do after this change, but let's see.

And there we go: fdb4931#r54532785

We might reconsider this given that we've already seen a user hit a pain point with the change, even without having released a new version. Unsure.

@auvipy
Copy link
Member

auvipy commented Sep 1, 2021

documenting this would be the right choice, IMHO

@akx
Copy link
Contributor Author

akx commented Sep 1, 2021

Cc @afparsons for having bumped into that issue in fdb4931#r54532785:

I believe the change in this PR is net positive and will likely uncover some bugs in downstream code even if it might need some work to adapt. In that sense I agree with @auvipy that it'd be better to just document this loudly for the next release.

@tomchristie tomchristie mentioned this pull request Dec 10, 2021
tomchristie added a commit that referenced this pull request Dec 10, 2021
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
@asottile-sentry
Copy link

I'm extremely late to the party but this now makes MultipleChoiceField and ChoiceField have incompatible signatures -- ChoiceField has choices as a positional argument and now it's required to be named only in MultipleChoiceField

unclear if this was intended -- but it certainly strikes me as odd

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