Skip to content

Conversation

rpkilby
Copy link
Contributor

@rpkilby rpkilby commented Oct 26, 2016

This is a better approach to fixing the from fields import * in serializers (see #4626/#4613).

  • Field class imports are separated from utility imports (SkipField, empty, etc...)
  • The test ensures all fields & related fields are imported into the serializers module. New field classes won't accidentally be skipped.

@lovelydinosaur
Copy link
Contributor

Seems reasonable, yup.

@adamn adamn mentioned this pull request Oct 26, 2016
@lovelydinosaur
Copy link
Contributor

Looking good - linter failed on isort, otherwise happy with it.
Any thoughts on if there's likely gotchas with users importing private functions in fields, using the serializers.* notation that we wouldn't now support? What isn't in the import anymore that used to be, that some users might be working with?

@rpkilby
Copy link
Contributor Author

rpkilby commented Oct 26, 2016

from foo import * ignores objects where the name has a leading _.

@rpkilby
Copy link
Contributor Author

rpkilby commented Oct 26, 2016

See the latest commit - added a section for non-field imports that are still public API.


All of the non-field definitions:

# fields.py
empty
is_simple_callable
get_attribute
set_value
to_choices_dict
flatten_choices_dict
iter_options
get_error_detail
CreateOnlyDefault
CurrentUserDefault
SkipField
REGEX_TYPE
NOT_READ_ONLY_WRITE_ONLY
NOT_READ_ONLY_REQUIRED
NOT_REQUIRED_DEFAULT
USE_READONLYFIELD
MISSING_ERROR_MESSAGE

# relations.py
method_overridden
Hyperlink
PKOnlyObject
MANY_RELATION_KWARGS

From the above, serializers currently imports:

# fields.py
empty
get_error_detail
set_value
CreateOnlyDefault
SkipField

# relations.py
Hyperlink
PKOnlyObject

@lovelydinosaur
Copy link
Contributor

Great. Anyone else got thoughts/review on this?

)

def test_fields(self):
msg = "Expected `fields.%s` to be imported in `serializers`"
Copy link
Contributor

Choose a reason for hiding this comment

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

These are great!

@lovelydinosaur lovelydinosaur merged commit d92b24a into encode:master Nov 1, 2016
@rpkilby rpkilby deleted the fix-fields-import branch September 25, 2017 18:47
dodobas added a commit to nyaruka/rapidpro that referenced this pull request Mar 5, 2018
We are stuck on DRF 3.5.1 because on 3.5.2 this PR got merged
encode/django-rest-framework#4628
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants