Skip to content

Conversation

@shrajal01
Copy link

This change updates ListSerializer.get_initial() to return an initial
list structure when initial_data is provided. The previous behavior
did not correctly handle list initialization and could result in
inconsistent initial states when rendering form-like serializers.

The updated implementation ensures:

  • When initial_data is a list, an initial list structure is returned
  • When no initial_data is provided, an empty list is returned
  • No unintended conversion via to_representation()

This aligns ListSerializer behavior with Serializer.get_initial and
prevents incorrect input→output transformations.

…itial to return consistent initial list structure

Modify get_initial to return a consistent list format when initial_data is a list.
Corrected the get_initial method to return consistent list format and fixed indentation to avoid SyntaxError / IndentationError in tests.
Copilot finished reviewing on behalf of auvipy November 12, 2025 04:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes ListSerializer.get_initial() to return a consistent initial list structure instead of using to_representation(), which could cause incorrect input→output transformations when rendering form-like serializers.

Key changes:

  • Updated get_initial() to return a list of initial values from child serializers when initial_data is provided
  • Added proper docstring explaining the method's behavior
  • Removed the call to to_representation() that was causing inconsistent behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return self.to_representation(self.initial_data)
if isinstance(self.initial_data, list):
return [self.child.get_initial() for _ in self.initial_data]
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.
Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

please add reference issue in the description section and appropriate unit tests for verifying the changes

Copy link
Collaborator

@peterthomassen peterthomassen left a comment

Choose a reason for hiding this comment

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

Thanks. I've added a comment where I think there's a problem.

Also, please add a test to demonstrate before/after behavior, and undo the unneeded whitespace changes. Thanks!

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants