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

Allow lists wherever possible in builtin operators #5379

Merged
merged 3 commits into from
Jan 14, 2025
Merged

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Jan 13, 2025

Change log

@brimoor brimoor requested a review from ritch January 13, 2025 04:36
Copy link
Contributor

coderabbitai bot commented Jan 13, 2025

Walkthrough

The pull request introduces modifications across multiple files to enhance field handling and operator functionality. In fiftyone/core/dataset.py, the create_summary_field method now processes list fields more robustly by iterating through them individually. The fiftyone/core/odm/mixins.py file adds a new _remove_nested_paths function to filter paths more effectively. In plugins/operators/__init__.py, significant changes improve input parameter handling, allowing multiple field operations and providing more flexible and informative error messaging.

Changes

File Change Summary
fiftyone/core/dataset.py Modified create_summary_field method to handle list fields through individual iteration and enhanced error handling.
fiftyone/core/odm/mixins.py Added _remove_nested_paths function to filter top-level paths and integrated it into existing methods.
plugins/operators/__init__.py Enhanced operator input handling, changed field_name to str, added support for multiple field operations, improved error messaging, and refactored input functions.

Possibly related PRs

Suggested labels

feature

Suggested reviewers

  • minhtuev
  • ritch

Poem

🐰 Hopping through code with glee,
List fields now dance more free!
Paths filtered, operators bright,
FiftyOne's magic takes flight!
Rabbit's code leap of delight! 🚀

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
plugins/operators/__init__.py (2)

259-261: Refactor duplicated validation logic into a helper function

The validation code that checks whether a field_name exists in schema and sets the invalid and error_message properties is repeated across multiple functions:

  • Lines 259-261 in _clone_sample_field_inputs
  • Lines 377-379 in _clone_frame_field_inputs
  • Lines 454-456 in _rename_sample_field_inputs
  • Lines 546-548 in _rename_frame_field_inputs
  • Lines 661-664 in _clear_sample_field_inputs
  • Lines 769-774 in _clear_frame_field_inputs
  • Lines 921-924 in _delete_sample_field_inputs
  • Lines 996-1001 in _delete_frame_field_inputs

Consider refactoring this repeated logic into a shared helper function to reduce code duplication and improve maintainability.

Also applies to: 377-379, 454-456, 546-548, 661-664, 769-774, 921-924, 996-1001


647-647: Simplify dictionary iteration by removing .keys()

In Python, iterating over a dictionary or checking for membership automatically uses the keys, so explicitly calling .keys() is unnecessary. This makes the code more concise and Pythonic.

Lines where this applies:

  • Line 647:

    - for key in schema.keys():
    + for key in schema:
  • Line 755:

    - for key in schema.keys():
    + for key in schema:
  • Line 907:

    - for key in schema.keys():
    + for key in schema:
  • Line 982:

    - for key in schema.keys():
    + for key in schema:

Also applies to: 755-755, 907-907, 982-982

🧰 Tools
🪛 Ruff (0.8.2)

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

fiftyone/core/odm/mixins.py (1)

1872-1878: Add a docstring to _remove_nested_paths for clarity

Consider adding a docstring to the _remove_nested_paths function to explain its purpose and usage. This will improve code readability and assist other developers in understanding its functionality.

fiftyone/core/dataset.py (1)

1939-1940: LGTM! Consider adding a comment explaining the list field unwinding logic.

The change to unwind list fields one by one improves robustness compared to unwinding all fields at once. This is a good optimization.

Add a comment explaining why list fields need to be unwound individually:

+        # Unwind list fields one by one to avoid potential issues with multiple list fields
         for list_field in list_fields:
             pipeline.append({"$unwind": "$" + list_field})
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7fd2b30 and 5c38f63.

📒 Files selected for processing (3)
  • fiftyone/core/dataset.py (1 hunks)
  • fiftyone/core/odm/mixins.py (4 hunks)
  • plugins/operators/__init__.py (20 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
plugins/operators/__init__.py

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


755-755: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


907-907: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


982-982: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build / build
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.10)
  • GitHub Check: e2e / test-e2e
  • GitHub Check: lint / eslint
  • GitHub Check: test / test-app
  • GitHub Check: build
🔇 Additional comments (1)
fiftyone/core/odm/mixins.py (1)

1872-1878: Approve addition of _remove_nested_paths function

The _remove_nested_paths function correctly filters out nested paths from the list, ensuring that only top-level fields are manipulated. This prevents unintended modifications to nested fields and enhances the robustness of field operations.

@brimoor brimoor force-pushed the builtin-use-lists2 branch from 5c38f63 to 4a35af8 Compare January 13, 2025 04:42
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (7)
plugins/operators/__init__.py (7)

247-261: LGTM! Field validation looks robust.

The field validation logic is well-implemented, handling both direct and nested paths. The error messages are clear and consistent between sample and frame field handling.

Consider extracting the common validation logic into a helper function since the same pattern is used in both _clone_sample_field_inputs and _clone_frame_field_inputs:

def _validate_field_existence(field_name, schema, field_prop, is_frame=False):
    if field_name not in schema and "." not in field_name:
        field_prop.invalid = True
        prefix = "Frame field" if is_frame else "Field"
        field_prop.error_message = f"{prefix} '{field_name}' does not exist"
        return False
    return True

Also applies to: 365-379


644-670: Well-implemented multi-field clearing functionality!

The implementation correctly handles:

  • Multiple field selection with proper UI support
  • Validation of field existence and read-only status
  • Prevention of nested field selection conflicts

Consider caching the field validation results to avoid repeated database lookups when validating multiple fields:

def _validate_fields(dataset, field_names, schema, field_prop, is_frame=False):
    field_cache = {}
    for field_name in field_names:
        if field_name not in schema and "." not in field_name:
            field_prop.invalid = True
            prefix = "Frame field" if is_frame else "Field"
            field_prop.error_message = f"{prefix} '{field_name}' does not exist"
            return False
            
        if field_name not in field_cache:
            prefix = dataset._FRAMES_PREFIX if is_frame else ""
            field_cache[field_name] = dataset.get_field(prefix + field_name)
            
        field = field_cache[field_name]
        if field is not None and field.read_only:
            field_prop.invalid = True
            prefix = "Frame field" if is_frame else "Field"
            field_prop.error_message = f"{prefix} '{field_name}' is read-only"
            return False
    return True

Also applies to: 752-782

🧰 Tools
🪛 Ruff (0.8.2)

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


Line range hint 886-930: Code duplication between clear and delete operations.

The field validation and selection logic is duplicated between clear and delete operations. While the implementation is correct, this introduces maintenance overhead.

Extract the common field validation and selection logic into shared utility functions:

def _setup_field_selector(schema, field_names, multiple=True):
    field_choices = types.AutocompleteView(multiple=multiple)
    for key in schema:
        if not any(key.startswith(f + ".") for f in field_names):
            field_choices.add_choice(key, label=key)
    return field_choices

def _create_field_input(inputs, field_choices, label, description, required=True):
    return inputs.list(
        "field_names",
        types.String(),
        label=label,
        description=description,
        required=required,
        view=field_choices,
    )

Also applies to: 952-1009

🧰 Tools
🪛 Ruff (0.8.2)

907-907: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


1619-1660: LGTM! Proper handling of current slice during multi-deletion.

The implementation correctly handles the edge case of deleting the current slice by resetting to the default slice.

Consider using any() for a more concise current slice check:

-curr_slice = False
-for name in names:
-    curr_slice |= name == ctx.group_slice
-    ctx.dataset.delete_group_slice(name)
+curr_slice = any(name == ctx.group_slice for name in names)
+for name in names:
+    ctx.dataset.delete_group_slice(name)

2184-2225: LGTM! Proper handling of current workspace during multi-deletion.

The implementation correctly handles the edge case of deleting the current workspace by resetting to the default workspace.

For consistency with the suggested group slice improvement, consider using any() here as well:

-curr_spaces = False
-for name in names:
-    curr_spaces |= name == ctx.spaces.name
-    ctx.dataset.delete_workspace(name)
+curr_spaces = any(name == ctx.spaces.name for name in names)
+for name in names:
+    ctx.dataset.delete_workspace(name)

647-647: Use key in dict instead of key in dict.keys()

The .keys() call is unnecessary when checking for key existence in a dictionary.

Apply this change in the following locations:

  • Line 647: for key in schema:
  • Line 755: for key in schema:
  • Line 907: for key in schema:
  • Line 982: for key in schema:

Also applies to: 755-755, 907-907, 982-982

🧰 Tools
🪛 Ruff (0.8.2)

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


1788-1790: Address the TODO comment about App bug.

The comment indicates that there's a bug in the App preventing proper view setting functionality.

Would you like me to help investigate the App bug and open a GitHub issue to track this task?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5c38f63 and 4a35af8.

📒 Files selected for processing (1)
  • plugins/operators/__init__.py (20 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
plugins/operators/__init__.py

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


755-755: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


907-907: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


982-982: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: e2e / test-e2e
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.11)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.10)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.9)
  • GitHub Check: build
🔇 Additional comments (3)
plugins/operators/__init__.py (3)

1124-1167: LGTM! Clean implementation of multi-index dropping.

The implementation correctly:

  • Filters out default indexes
  • Provides a clear UI for multiple index selection
  • Maintains consistency with other multi-selection operations

1443-1479: LGTM! Consistent implementation of multi-field deletion for summary fields.

The implementation maintains consistency with other multi-field operations while properly handling summary field specifics.


1898-1934: LGTM! Clean implementation of multi-view deletion.

The implementation maintains consistency with other multi-selection operations while properly handling saved views.

@brimoor brimoor force-pushed the builtin-use-lists2 branch from 4a35af8 to 2047cfb Compare January 14, 2025 14:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
fiftyone/core/odm/mixins.py (1)

1872-1878: Add docstring and consider performance optimization.

The function correctly filters out nested paths, but could benefit from:

  1. A docstring explaining its purpose and parameters
  2. Type hints for better code maintainability
  3. Performance optimization for large lists by using sets
+from typing import List, Set
+
-def _remove_nested_paths(paths):
+def _remove_nested_paths(paths: List[str]) -> List[str]:
+    """Filters a list of paths to exclude any that are nested within other paths.
+    
+    Args:
+        paths: List of field paths
+        
+    Returns:
+        List of paths excluding any that are nested within other paths
+        
+    Example:
+        >>> _remove_nested_paths(['a', 'a.b', 'c'])
+        ['a', 'c']
+    """
+    # Convert to set for O(1) lookups
+    path_set: Set[str] = set(paths)
     return [
         path
-        for path in paths
-        if not any(path.startswith(p + ".") for p in paths)
+        for path in paths
+        if not any(
+            path.startswith(p + ".")
+            for p in path_set
+            if p != path
+        )
     ]
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a35af8 and 2047cfb.

📒 Files selected for processing (2)
  • fiftyone/core/dataset.py (1 hunks)
  • fiftyone/core/odm/mixins.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • fiftyone/core/dataset.py
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: modified-files
  • GitHub Check: build
🔇 Additional comments (3)
fiftyone/core/odm/mixins.py (3)

673-674: LGTM! Clean integration of nested path filtering.

The integration prevents redundant operations by filtering out nested paths before clearing fields.


721-722: LGTM! Clean integration of nested path filtering.

The integration prevents redundant operations by filtering out nested paths before deleting fields.


826-827: LGTM! Clean integration of nested path filtering.

The integration prevents redundant operations by filtering out nested paths before removing dynamic fields.

@brimoor brimoor changed the title Allow lists wherever possible in builtin operators (ready for #5375) Allow lists wherever possible in builtin operators Jan 14, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
plugins/operators/__init__.py (4)

259-261: Consider extracting field validation logic into a helper function.

The field existence validation logic is duplicated across multiple functions. Consider extracting it into a helper function to improve maintainability and reduce code duplication.

+def _validate_field_exists(field_prop, field_name, schema, is_frame_field=False):
+    if field_name not in schema and "." not in field_name:
+        field_prop.invalid = True
+        field_prop.error_message = (
+            f"Frame field '{field_name}' does not exist"
+            if is_frame_field
+            else f"Field '{field_name}' does not exist"
+        )
+        return False
+    return True

Also applies to: 377-379, 454-456, 546-548, 661-663, 769-773


1788-1790: TODO comment needs attention.

The TODO comment indicates a bug in the App that needs to be fixed.

Would you like me to create a GitHub issue to track this App bug that prevents ctx.ops.set_view(name=name) from working?


2193-2196: Consider using any() for better readability and performance.

The loop that checks if the current workspace is being deleted could be simplified using any().

-curr_spaces = False
-for name in names:
-    curr_spaces |= name == ctx.spaces.name
-    ctx.dataset.delete_workspace(name)
+curr_spaces = any(name == ctx.spaces.name for name in names)
+for name in names:
+    ctx.dataset.delete_workspace(name)

647-647: Use key in dict instead of key in dict.keys().

Using key in dict is more idiomatic Python and slightly more efficient as it avoids creating a view object.

-for key in schema.keys():
+for key in schema:

Also applies to: 755-755, 907-907, 982-982

🧰 Tools
🪛 Ruff (0.8.2)

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2047cfb and 6800029.

📒 Files selected for processing (1)
  • plugins/operators/__init__.py (20 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
plugins/operators/__init__.py

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


755-755: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


907-907: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


982-982: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.10)
  • GitHub Check: test / test-app
  • GitHub Check: e2e / test-e2e
  • GitHub Check: build / build
  • GitHub Check: lint / eslint
  • GitHub Check: build / changes
  • GitHub Check: build
🔇 Additional comments (1)
plugins/operators/__init__.py (1)

646-649: Good use of allow_duplicates=False and nested field handling.

The implementation correctly prevents:

  1. Duplicate field selection using allow_duplicates=False
  2. Selection of nested fields using the startswith check
🧰 Tools
🪛 Ruff (0.8.2)

647-647: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

@brimoor brimoor merged commit 865fb0e into develop Jan 14, 2025
12 checks passed
@brimoor brimoor deleted the builtin-use-lists2 branch January 14, 2025 15:00
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.

2 participants