Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class ListSerializer(BaseSerializer):
'max_length': _('Ensure this field has no more than {max_length} elements.'),
'min_length': _('Ensure this field has at least {min_length} elements.')
}

def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child))
self.allow_empty = kwargs.pop('allow_empty', True)
Expand All @@ -609,10 +609,16 @@ def __init__(self, *args, **kwargs):
self.child.bind(field_name='', parent=self)

def get_initial(self):
"""
Return a list of initial values, one for each item in `initial_data`,
or an empty list if no input data was provided.
"""
if hasattr(self, 'initial_data'):
return self.to_representation(self.initial_data)
if isinstance(self.initial_data, list):
return [self.child.get_initial() for _ in self.initial_data]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not seem correct, as all the list items would be the same.

return []
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

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

The return [] statement on line 620 is unreachable code. When hasattr(self, 'initial_data') is True, the code returns either on line 618 (if initial_data is a list) or on line 619 (if it's not a list). When the condition is False, execution jumps directly to line 620.

This results in redundant code since line 619 and line 620 both return the same empty list.

Suggested fix - remove line 619 and keep only line 620:

def get_initial(self):
    """
    Return a list of initial values, one for each item in `initial_data`,
    or an empty list if no input data was provided.
    """
    if hasattr(self, 'initial_data'):
        if isinstance(self.initial_data, list):
            return [self.child.get_initial() for _ in self.initial_data]
    return []
Suggested change
return []

Copilot uses AI. Check for mistakes.
return []

def get_value(self, dictionary):
"""
Given the input dictionary, return the field value.
Expand Down
Loading