Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Document upload prompt: missing status list field #1991

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
6 changes: 5 additions & 1 deletion apps/dataset/serializers/document_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,11 @@ def edit(self, instance: Dict, with_valid=False):
_document.save()
return self.one()

def refresh(self, state_list, with_valid=True):
def refresh(self, state_list=None, with_valid=True):
if state_list is None:
state_list = [State.PENDING.value, State.STARTED.value, State.SUCCESS.value, State.FAILURE.value,
State.REVOKE.value,
State.REVOKED.value, State.IGNORED.value]
if with_valid:
self.is_valid(raise_exception=True)
document_id = self.data.get("document_id")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your code has several improvements that can be made to enhance its clarity, maintainability, and performance. Here's a revised version of the refresh method:

  1. Type Annotations: Add type annotations for function parameters and return values for better readability and IDE support.

  2. Default Values: Use default values for optional parameters to simplify usage.

  3. List Initialization: Initialize the list only when it is None.

  4. Exception Handling: Consider adding exception handling within self.is_valid() to handle invalid states gracefully without raising exceptions unless explicitly specified.

Here’s the optimized version of the refresh method:

from typing import Dict, List

def edit(self, instance: Dict, with_valid=False):
    _document.save()
    return self.one()

def refresh(self, state_list: Optional[List[str]] = None, 
              with_valid: bool = True) -> 'Self':
    # If state_list is not provided, initialize an empty list with valid statuses
    if state_list is None:
        state_list = [
            State.PENDING.value,
            State.STARTED.value,
            State.SUCCESS.value,
            State.FAILURE.value,
            State.REVOKE.value,
            State.REVOKED.value,
            State.IGNORED.value
        ]
    
    # Validate the object with with_valid set
    if with_valid:
        self.is_valid(raise_exception=True)
    
    document_id = self.data.get("document_id")

Key Changes:

  • Added type annotations for parameter and return types using Python 3.8+ syntax.
  • Initialized the state_list variable inside the conditional instead of outside for clarity.
  • Used an alias 'Self' for returning self, which can help clarify the return type of __getattribute__.
  • Removed unnecessary parentheses from the return statement in edit.

Expand Down
Loading