Skip to content

Commit eb76181

Browse files
authored
Revert "Make Field constructors keyword-only (#7632)"
This reverts commit fdb4931.
1 parent 1cb3fa2 commit eb76181

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
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, **kwargs):
1166+
def __init__(self, format=empty, input_formats=None, default_timezone=None, *args, **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__(**kwargs)
1173+
super().__init__(*args, **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, **kwargs):
1252+
def __init__(self, format=empty, input_formats=None, *args, **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__(**kwargs)
1257+
super().__init__(*args, **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, **kwargs):
1318+
def __init__(self, format=empty, input_formats=None, *args, **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__(**kwargs)
1323+
super().__init__(*args, **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, **kwargs):
1473+
def __init__(self, *args, **kwargs):
14741474
self.allow_empty = kwargs.pop('allow_empty', True)
1475-
super().__init__(**kwargs)
1475+
super().__init__(*args, **kwargs)
14761476

14771477
def get_value(self, dictionary):
14781478
if self.field_name not in dictionary:
@@ -1531,12 +1531,12 @@ class FileField(Field):
15311531
'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'),
15321532
}
15331533

1534-
def __init__(self, **kwargs):
1534+
def __init__(self, *args, **kwargs):
15351535
self.max_length = kwargs.pop('max_length', None)
15361536
self.allow_empty_file = kwargs.pop('allow_empty_file', False)
15371537
if 'use_url' in kwargs:
15381538
self.use_url = kwargs.pop('use_url')
1539-
super().__init__(**kwargs)
1539+
super().__init__(*args, **kwargs)
15401540

15411541
def to_internal_value(self, data):
15421542
try:
@@ -1580,9 +1580,9 @@ class ImageField(FileField):
15801580
),
15811581
}
15821582

1583-
def __init__(self, **kwargs):
1583+
def __init__(self, *args, **kwargs):
15841584
self._DjangoImageField = kwargs.pop('_DjangoImageField', DjangoImageField)
1585-
super().__init__(**kwargs)
1585+
super().__init__(*args, **kwargs)
15861586

15871587
def to_internal_value(self, data):
15881588
# Image validation is a bit grungy, so we'll just outright
@@ -1597,8 +1597,8 @@ def to_internal_value(self, data):
15971597
# Composite field types...
15981598

15991599
class _UnvalidatedField(Field):
1600-
def __init__(self, **kwargs):
1601-
super().__init__(**kwargs)
1600+
def __init__(self, *args, **kwargs):
1601+
super().__init__(*args, **kwargs)
16021602
self.allow_blank = True
16031603
self.allow_null = True
16041604

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

1622-
def __init__(self, **kwargs):
1622+
def __init__(self, *args, **kwargs):
16231623
self.child = kwargs.pop('child', copy.deepcopy(self.child))
16241624
self.allow_empty = kwargs.pop('allow_empty', True)
16251625
self.max_length = kwargs.pop('max_length', None)
@@ -1631,7 +1631,7 @@ def __init__(self, **kwargs):
16311631
"Remove `source=` from the field declaration."
16321632
)
16331633

1634-
super().__init__(**kwargs)
1634+
super().__init__(*args, **kwargs)
16351635
self.child.bind(field_name='', parent=self)
16361636
if self.max_length is not None:
16371637
message = lazy_format(self.error_messages['max_length'], max_length=self.max_length)
@@ -1696,7 +1696,7 @@ class DictField(Field):
16961696
'empty': _('This dictionary may not be empty.'),
16971697
}
16981698

1699-
def __init__(self, **kwargs):
1699+
def __init__(self, *args, **kwargs):
17001700
self.child = kwargs.pop('child', copy.deepcopy(self.child))
17011701
self.allow_empty = kwargs.pop('allow_empty', True)
17021702

@@ -1706,7 +1706,7 @@ def __init__(self, **kwargs):
17061706
"Remove `source=` from the field declaration."
17071707
)
17081708

1709-
super().__init__(**kwargs)
1709+
super().__init__(*args, **kwargs)
17101710
self.child.bind(field_name='', parent=self)
17111711

17121712
def get_value(self, dictionary):
@@ -1755,8 +1755,8 @@ def run_child_validation(self, data):
17551755
class HStoreField(DictField):
17561756
child = CharField(allow_blank=True, allow_null=True)
17571757

1758-
def __init__(self, **kwargs):
1759-
super().__init__(**kwargs)
1758+
def __init__(self, *args, **kwargs):
1759+
super().__init__(*args, **kwargs)
17601760
assert isinstance(self.child, CharField), (
17611761
"The `child` argument must be an instance of `CharField`, "
17621762
"as the hstore extension stores values as strings."
@@ -1771,11 +1771,11 @@ class JSONField(Field):
17711771
# Workaround for isinstance calls when importing the field isn't possible
17721772
_is_jsonfield = True
17731773

1774-
def __init__(self, **kwargs):
1774+
def __init__(self, *args, **kwargs):
17751775
self.binary = kwargs.pop('binary', False)
17761776
self.encoder = kwargs.pop('encoder', None)
17771777
self.decoder = kwargs.pop('decoder', None)
1778-
super().__init__(**kwargs)
1778+
super().__init__(*args, **kwargs)
17791779

17801780
def get_value(self, dictionary):
17811781
if html.is_html_input(dictionary) and self.field_name in dictionary:

tests/test_fields.py

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

2022-
def test_constructor_misuse_raises(self):
2023-
# Test that `ListField` can only be instantiated with keyword arguments
2024-
with pytest.raises(TypeError):
2025-
serializers.ListField(serializers.CharField())
2026-
20272022

20282023
class TestNestedListField(FieldValues):
20292024
"""

0 commit comments

Comments
 (0)