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

Commitfdb4931

Browse files
authored
Make Field constructors keyword-only (#7632)
1 parentb215375 commitfdb4931

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

‎rest_framework/fields.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class Field:
320320
default_empty_html=empty
321321
initial=None
322322

323-
def__init__(self,read_only=False,write_only=False,
323+
def__init__(self,*,read_only=False,write_only=False,
324324
required=None,default=empty,initial=empty,source=None,
325325
label=None,help_text=None,style=None,
326326
error_messages=None,validators=None,allow_null=False):
@@ -1163,14 +1163,14 @@ class DateTimeField(Field):
11631163
}
11641164
datetime_parser=datetime.datetime.strptime
11651165

1166-
def__init__(self,format=empty,input_formats=None,default_timezone=None,*args,**kwargs):
1166+
def__init__(self,format=empty,input_formats=None,default_timezone=None,**kwargs):
11671167
ifformatisnotempty:
11681168
self.format=format
11691169
ifinput_formatsisnotNone:
11701170
self.input_formats=input_formats
11711171
ifdefault_timezoneisnotNone:
11721172
self.timezone=default_timezone
1173-
super().__init__(*args,**kwargs)
1173+
super().__init__(**kwargs)
11741174

11751175
defenforce_timezone(self,value):
11761176
"""
@@ -1249,12 +1249,12 @@ class DateField(Field):
12491249
}
12501250
datetime_parser=datetime.datetime.strptime
12511251

1252-
def__init__(self,format=empty,input_formats=None,*args,**kwargs):
1252+
def__init__(self,format=empty,input_formats=None,**kwargs):
12531253
ifformatisnotempty:
12541254
self.format=format
12551255
ifinput_formatsisnotNone:
12561256
self.input_formats=input_formats
1257-
super().__init__(*args,**kwargs)
1257+
super().__init__(**kwargs)
12581258

12591259
defto_internal_value(self,value):
12601260
input_formats=getattr(self,'input_formats',api_settings.DATE_INPUT_FORMATS)
@@ -1315,12 +1315,12 @@ class TimeField(Field):
13151315
}
13161316
datetime_parser=datetime.datetime.strptime
13171317

1318-
def__init__(self,format=empty,input_formats=None,*args,**kwargs):
1318+
def__init__(self,format=empty,input_formats=None,**kwargs):
13191319
ifformatisnotempty:
13201320
self.format=format
13211321
ifinput_formatsisnotNone:
13221322
self.input_formats=input_formats
1323-
super().__init__(*args,**kwargs)
1323+
super().__init__(**kwargs)
13241324

13251325
defto_internal_value(self,value):
13261326
input_formats=getattr(self,'input_formats',api_settings.TIME_INPUT_FORMATS)
@@ -1470,9 +1470,9 @@ class MultipleChoiceField(ChoiceField):
14701470
}
14711471
default_empty_html= []
14721472

1473-
def__init__(self,*args,**kwargs):
1473+
def__init__(self,**kwargs):
14741474
self.allow_empty=kwargs.pop('allow_empty',True)
1475-
super().__init__(*args,**kwargs)
1475+
super().__init__(**kwargs)
14761476

14771477
defget_value(self,dictionary):
14781478
ifself.field_namenotindictionary:
@@ -1529,12 +1529,12 @@ class FileField(Field):
15291529
'max_length':_('Ensure this filename has at most {max_length} characters (it has {length}).'),
15301530
}
15311531

1532-
def__init__(self,*args,**kwargs):
1532+
def__init__(self,**kwargs):
15331533
self.max_length=kwargs.pop('max_length',None)
15341534
self.allow_empty_file=kwargs.pop('allow_empty_file',False)
15351535
if'use_url'inkwargs:
15361536
self.use_url=kwargs.pop('use_url')
1537-
super().__init__(*args,**kwargs)
1537+
super().__init__(**kwargs)
15381538

15391539
defto_internal_value(self,data):
15401540
try:
@@ -1578,9 +1578,9 @@ class ImageField(FileField):
15781578
),
15791579
}
15801580

1581-
def__init__(self,*args,**kwargs):
1581+
def__init__(self,**kwargs):
15821582
self._DjangoImageField=kwargs.pop('_DjangoImageField',DjangoImageField)
1583-
super().__init__(*args,**kwargs)
1583+
super().__init__(**kwargs)
15841584

15851585
defto_internal_value(self,data):
15861586
# Image validation is a bit grungy, so we'll just outright
@@ -1595,8 +1595,8 @@ def to_internal_value(self, data):
15951595
# Composite field types...
15961596

15971597
class_UnvalidatedField(Field):
1598-
def__init__(self,*args,**kwargs):
1599-
super().__init__(*args,**kwargs)
1598+
def__init__(self,**kwargs):
1599+
super().__init__(**kwargs)
16001600
self.allow_blank=True
16011601
self.allow_null=True
16021602

@@ -1617,7 +1617,7 @@ class ListField(Field):
16171617
'max_length':_('Ensure this field has no more than {max_length} elements.')
16181618
}
16191619

1620-
def__init__(self,*args,**kwargs):
1620+
def__init__(self,**kwargs):
16211621
self.child=kwargs.pop('child',copy.deepcopy(self.child))
16221622
self.allow_empty=kwargs.pop('allow_empty',True)
16231623
self.max_length=kwargs.pop('max_length',None)
@@ -1629,7 +1629,7 @@ def __init__(self, *args, **kwargs):
16291629
"Remove `source=` from the field declaration."
16301630
)
16311631

1632-
super().__init__(*args,**kwargs)
1632+
super().__init__(**kwargs)
16331633
self.child.bind(field_name='',parent=self)
16341634
ifself.max_lengthisnotNone:
16351635
message=lazy_format(self.error_messages['max_length'],max_length=self.max_length)
@@ -1694,7 +1694,7 @@ class DictField(Field):
16941694
'empty':_('This dictionary may not be empty.'),
16951695
}
16961696

1697-
def__init__(self,*args,**kwargs):
1697+
def__init__(self,**kwargs):
16981698
self.child=kwargs.pop('child',copy.deepcopy(self.child))
16991699
self.allow_empty=kwargs.pop('allow_empty',True)
17001700

@@ -1704,7 +1704,7 @@ def __init__(self, *args, **kwargs):
17041704
"Remove `source=` from the field declaration."
17051705
)
17061706

1707-
super().__init__(*args,**kwargs)
1707+
super().__init__(**kwargs)
17081708
self.child.bind(field_name='',parent=self)
17091709

17101710
defget_value(self,dictionary):
@@ -1753,8 +1753,8 @@ def run_child_validation(self, data):
17531753
classHStoreField(DictField):
17541754
child=CharField(allow_blank=True,allow_null=True)
17551755

1756-
def__init__(self,*args,**kwargs):
1757-
super().__init__(*args,**kwargs)
1756+
def__init__(self,**kwargs):
1757+
super().__init__(**kwargs)
17581758
assertisinstance(self.child,CharField), (
17591759
"The `child` argument must be an instance of `CharField`, "
17601760
"as the hstore extension stores values as strings."
@@ -1769,11 +1769,11 @@ class JSONField(Field):
17691769
# Workaround for isinstance calls when importing the field isn't possible
17701770
_is_jsonfield=True
17711771

1772-
def__init__(self,*args,**kwargs):
1772+
def__init__(self,**kwargs):
17731773
self.binary=kwargs.pop('binary',False)
17741774
self.encoder=kwargs.pop('encoder',None)
17751775
self.decoder=kwargs.pop('decoder',None)
1776-
super().__init__(*args,**kwargs)
1776+
super().__init__(**kwargs)
17771777

17781778
defget_value(self,dictionary):
17791779
ifhtml.is_html_input(dictionary)andself.field_nameindictionary:

‎tests/test_fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,11 @@ def test_collection_types_are_invalid_input(self):
20102010
field.to_internal_value(input_value)
20112011
assertexc_info.value.detail== ['Expected a list of items but got type "dict".']
20122012

2013+
deftest_constructor_misuse_raises(self):
2014+
# Test that `ListField` can only be instantiated with keyword arguments
2015+
withpytest.raises(TypeError):
2016+
serializers.ListField(serializers.CharField())
2017+
20132018

20142019
classTestNestedListField(FieldValues):
20152020
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp