Skip to content

Commit 455d793

Browse files
akxsigvef
authored andcommitted
Make Field constructors keyword-only (encode#7632)
1 parent 17d4420 commit 455d793

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
if format is not empty:
11681168
self.format = format
11691169
if input_formats is not None:
11701170
self.input_formats = input_formats
11711171
if default_timezone is not None:
11721172
self.timezone = default_timezone
1173-
super().__init__(*args, **kwargs)
1173+
super().__init__(**kwargs)
11741174

11751175
def enforce_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
if format is not empty:
12541254
self.format = format
12551255
if input_formats is not None:
12561256
self.input_formats = input_formats
1257-
super().__init__(*args, **kwargs)
1257+
super().__init__(**kwargs)
12581258

12591259
def to_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
if format is not empty:
13201320
self.format = format
13211321
if input_formats is not None:
13221322
self.input_formats = input_formats
1323-
super().__init__(*args, **kwargs)
1323+
super().__init__(**kwargs)
13241324

13251325
def to_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
def get_value(self, dictionary):
14781478
if self.field_name not in dictionary:
@@ -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' in kwargs:
15361536
self.use_url = kwargs.pop('use_url')
1537-
super().__init__(*args, **kwargs)
1537+
super().__init__(**kwargs)
15381538

15391539
def to_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
def to_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
if self.max_length is not None:
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
def get_value(self, dictionary):
@@ -1753,8 +1753,8 @@ def run_child_validation(self, data):
17531753
class HStoreField(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
assert isinstance(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
def get_value(self, dictionary):
17791779
if html.is_html_input(dictionary) and self.field_name in dictionary:

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
assert exc_info.value.detail == ['Expected a list of items but got type "dict".']
20122012

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

20142019
class TestNestedListField(FieldValues):
20152020
"""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy