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 some warnings from new Pylint #7955

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented May 28, 2024

Motivation and context

I tried to update Pylint to the most recent version (3.2.2), which produced a bunch of new warnings. I couldn't fix all of them in one sitting, but I fixed some.

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • [ ] I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • [ ] I have added tests to cover my changes
  • [ ] I have linked related issues (see GitHub docs)
  • [ ] I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Refactor

    • Improved code readability by removing unnecessary parentheses in boolean assignments.
    • Adjusted indentation and formatting in various scripts for better code clarity.
  • Bug Fixes

    • Corrected angle calculation logic in the 3D interpolation function.
    • Enhanced type safety with added assertions to handle unexpected values and dimensions.
  • New Features

    • Introduced more detailed logging and error handling in dataset manager and REST API tests.
  • Chores

    • Updated import statements to include necessary modules and remove unused ones for better code organization.

Copy link
Contributor

coderabbitai bot commented May 28, 2024

Important

Review Skipped

Automatic incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes in this update focus on code quality improvements, minor bug fixes, and enhancements in the cvat-sdk, dataset_manager, engine, webhooks, serverless, and utils directories. Key changes include adding assertions for better error handling, simplifying boolean expressions, adjusting function calls, and importing specific modules for clarity and efficiency. These updates aim to improve maintainability, readability, and robustness of the codebase without introducing significant new features.

Changes

File(s) Change Summary
cvat-sdk/.../interface.py Added # pylint: disable-next=redefined-builtin comment to skeleton_label_spec function.
cvat/apps/dataset_manager/annotation.py Simplified list addition in simple_3d_interpolation function.
cvat/apps/dataset_manager/bindings.py Added assert for unexpected dimension and removed unnecessary parentheses in boolean handling.
cvat/apps/dataset_manager/formats/cvat.py Removed unnecessary parentheses in boolean assignments.
cvat/apps/dataset_manager/formats/registry.py Added assertion for type safety in __call__ method.
cvat/apps/dataset_manager/tests/test_rest_api_formats.py Added assertions for unexpected key_get_values in _create_annotations and _create_annotations_in_job functions.
cvat/apps/engine/migrations/0016_attribute_spec_20190217.py Adjusted mutable attribute assignment in split_text_attribute function.
cvat/apps/engine/migrations/0024_auto_20191023_1025.py Updated get_preview() calls to include argument 0.
cvat/apps/engine/migrations/0034_auto_20201125_1426.py Reformatted indentation in create_profile function.
cvat/apps/engine/migrations/0038_manifest.py Modified regex pattern in _get_frame_step function to use raw string.
cvat/apps/engine/serializers.py Added assertion for label attachment during update operation.
cvat/apps/webhooks/signals.py Modified logic for determining event type and assignment of instance._webhooks_old_data.
serverless/pytorch/foolwood/siammask/nuclio/model_handler.py Adjusted imports for clarity and efficiency.
tests/python/rest_api/test_projects.py Added initialization for project_with_owner and project_with_assignee variables in specific test methods.
utils/dataset_manifest/create.py Added specific imports and removed redundant sys.path.append statements.
utils/dicom_converter/script.py Adjusted indentation of a conditional block for color conversion.

In lines of code, we find our way,
Assertions guard, no errors sway.
With imports clear, and comments bright,
Our code now gleams with guiding light.
A rabbit's joy in changes made,
For bugs and messes now do fade. 🐇✨


Tip

Early Access Features
  • gpt-4o model for chat

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 2

Outside diff range and nitpick comments (5)
cvat/apps/dataset_manager/bindings.py (3)

Line range hint 1907-1907: Consider using not in for membership tests to enhance readability and performance.

- if not frame_number in instance_data.frame_info:
+ if frame_number not in instance_data.frame_info:

Line range hint 1-2500: Ensure consistent error handling and validation across data import functions to prevent runtime errors and data integrity issues.

Consider implementing a centralized error handling mechanism that could manage different types of exceptions and provide a unified response strategy. This would improve the maintainability and reliability of the data import processes.


Line range hint 1-2500: Consider refactoring the file to improve modularity and maintainability.

Breaking down this large file into smaller modules based on functionality (e.g., data import, data export, frame management) could enhance readability, maintainability, and ease of testing.

cvat/apps/engine/serializers.py (2)

Line range hint 719-719: Remove unnecessary f-string.

The f-string in this line does not contain any placeholders, making the use of an f-string unnecessary. It can be replaced with a regular string for clarity and performance.

- assert False, f"Label {instance.id} is not attached to either a project or a task"
+ assert False, "Label is not attached to either a project or a task"

Line range hint 1154-1154: Remove unnecessary f-string.

Similar to the previous comment, this line uses an f-string without placeholders. Replace it with a regular string.

- raise serializers.ValidationError(f"Dimension ({instance.dimension}) of the task must be the same as other tasks in project ({project.tasks.first().dimension})")
+ raise serializers.ValidationError("Dimension of the task must be the same as other tasks in project")
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f883838 and 39e180b.
Files selected for processing (16)
  • cvat-sdk/cvat_sdk/auto_annotation/interface.py (1 hunks)
  • cvat/apps/dataset_manager/annotation.py (1 hunks)
  • cvat/apps/dataset_manager/bindings.py (2 hunks)
  • cvat/apps/dataset_manager/formats/cvat.py (2 hunks)
  • cvat/apps/dataset_manager/formats/registry.py (1 hunks)
  • cvat/apps/dataset_manager/tests/test_rest_api_formats.py (2 hunks)
  • cvat/apps/engine/migrations/0016_attribute_spec_20190217.py (1 hunks)
  • cvat/apps/engine/migrations/0024_auto_20191023_1025.py (3 hunks)
  • cvat/apps/engine/migrations/0034_auto_20201125_1426.py (1 hunks)
  • cvat/apps/engine/migrations/0038_manifest.py (1 hunks)
  • cvat/apps/engine/serializers.py (1 hunks)
  • cvat/apps/webhooks/signals.py (1 hunks)
  • serverless/pytorch/foolwood/siammask/nuclio/model_handler.py (1 hunks)
  • tests/python/rest_api/test_projects.py (3 hunks)
  • utils/dataset_manifest/create.py (2 hunks)
  • utils/dicom_converter/script.py (1 hunks)
Files skipped from review due to trivial changes (3)
  • cvat/apps/engine/migrations/0034_auto_20201125_1426.py
  • cvat/apps/engine/migrations/0038_manifest.py
  • utils/dicom_converter/script.py
Additional Context Used
Ruff (57)
cvat/apps/dataset_manager/bindings.py (1)

1907-1907: Test for membership should be not in

cvat/apps/dataset_manager/formats/cvat.py (25)

350-350: Multiple statements on one line (colon)


351-351: Multiple statements on one line (colon)


352-352: Multiple statements on one line (colon)


353-353: Multiple statements on one line (colon)


354-354: Multiple statements on one line (colon)


355-355: Multiple statements on one line (colon)


356-356: Multiple statements on one line (colon)


357-357: Multiple statements on one line (colon)


358-358: Multiple statements on one line (colon)


359-359: Multiple statements on one line (colon)


360-360: Multiple statements on one line (colon)


361-361: Multiple statements on one line (colon)


362-362: Multiple statements on one line (colon)


365-365: Multiple statements on one line (colon)


366-366: Multiple statements on one line (colon)


367-367: Multiple statements on one line (colon)


368-368: Multiple statements on one line (colon)


369-369: Multiple statements on one line (colon)


379-379: Multiple statements on one line (colon)


380-380: Multiple statements on one line (colon)


390-390: Multiple statements on one line (colon)


393-393: Multiple statements on one line (colon)


404-404: Multiple statements on one line (colon)


405-405: Multiple statements on one line (colon)


409-409: Multiple statements on one line (colon)

cvat/apps/dataset_manager/formats/registry.py (22)

101-101: Module level import not at top of file


102-102: Module level import not at top of file


103-103: Module level import not at top of file


104-104: Module level import not at top of file


105-105: Module level import not at top of file


106-106: Module level import not at top of file


107-107: Module level import not at top of file


108-108: Module level import not at top of file


109-109: Module level import not at top of file


110-110: Module level import not at top of file


111-111: Module level import not at top of file


112-112: Module level import not at top of file


113-113: Module level import not at top of file


114-114: Module level import not at top of file


115-115: Module level import not at top of file


116-116: Module level import not at top of file


117-117: Module level import not at top of file


118-118: Module level import not at top of file


119-119: Module level import not at top of file


120-120: Module level import not at top of file


121-121: Module level import not at top of file


121-121: cvat.apps.dataset_manager.formats.openimages imported but unused

cvat/apps/engine/serializers.py (2)

719-719: f-string without any placeholders


1154-1154: f-string without any placeholders

tests/python/rest_api/test_projects.py (7)

720-720: f-string without any placeholders


813-813: Ambiguous variable name: l


835-835: Ambiguous variable name: l


836-836: Ambiguous variable name: l


853-853: Ambiguous variable name: l


866-866: Ambiguous variable name: l


879-879: Ambiguous variable name: l

Additional comments not posted (19)
serverless/pytorch/foolwood/siammask/nuclio/model_handler.py (1)

10-12: Refined imports to explicitly list functions, enhancing code clarity and maintainability.

utils/dataset_manifest/create.py (1)

18-20: Removed redundant sys.path.append and refined imports, improving the script's maintainability and reducing potential for import errors.

cvat/apps/dataset_manager/formats/registry.py (1)

41-42: Added assertion to ensure f_or_cls is either a class or a function, enhancing robustness by preventing incorrect usage.

cvat/apps/engine/migrations/0016_attribute_spec_20190217.py (1)

31-31: Adjusted the assignment of the mutable attribute based on the prefix, enhancing the clarity and correctness of the migration logic.

cvat-sdk/cvat_sdk/auto_annotation/interface.py (1)

128-128: Added pylint directive to disable warnings for redefined built-ins, ensuring compatibility with existing naming conventions while acknowledging potential risks.

cvat/apps/webhooks/signals.py (1)

142-163: Modified logic to determine event_type based on the presence of old_instance, enhancing the accuracy of event type determination and improving data consistency.

cvat/apps/engine/migrations/0024_auto_20191023_1025.py (1)

82-82: The addition of the argument 0 to get_preview() ensures that the preview is generated from the first frame. This is a sensible default for migrations involving media data. Ensure that this behavior aligns with the expectations across different use cases in the application.

Also applies to: 149-149

cvat/apps/dataset_manager/annotation.py (1)

608-608: Simplifying the calculation of angles by removing unnecessary parentheses improves readability without affecting the logic. Good job on making the code cleaner and more straightforward.

tests/python/rest_api/test_projects.py (3)

70-71: Initialize project_with_owner and project_with_assignee to None.

This change ensures that the variables are defined before use, which is a good practice to avoid potential UnboundLocalError in Python.


1043-1044: Initialize project_with_owner and project_with_assignee to None.

Similar to the previous initialization, this ensures that the variables are properly defined before they are used, which is crucial for maintaining robustness in the code.


1062-1063: Initialize project_with_owner and project_with_assignee to None.

This initialization is consistent with the pattern observed in other test methods, ensuring that these variables are defined before any operations are performed on them.

cvat/apps/dataset_manager/formats/cvat.py (5)

239-239: Remove unnecessary parentheses for consistency and readability.

- shape_element['keyframe'] = el.attrib.get('keyframe') == '1'
+ shape_element['keyframe'] = el.attrib['keyframe'] == '1'

Likely invalid or redundant comment.


236-236: Remove unnecessary parentheses for consistency and readability.

- shape_element['outside'] = el.attrib.get('outside') == '1'
+ shape_element['outside'] = el.attrib['outside'] == '1'

Likely invalid or redundant comment.


231-231: Remove unnecessary parentheses for consistency and readability.

- shape_element['occluded'] = el.attrib.get('occluded') == '1'
+ shape_element['occluded'] = el.attrib['occluded'] == '1'

Likely invalid or redundant comment.


256-256: Remove unnecessary parentheses for consistency and readability.

- shape['occluded'] = el.attrib.get('occluded') == '1'
+ shape['occluded'] = el.attrib['occluded'] == '1'

Likely invalid or redundant comment.


249-249: Remove unnecessary parentheses for consistency and readability.

- shape['outside'] = el.attrib.get('outside') == '1'
+ shape['outside'] = el.attrib['outside'] == '1'

Likely invalid or redundant comment.

cvat/apps/dataset_manager/tests/test_rest_api_formats.py (2)

244-245: Asserting on unexpected key_get_values values enhances robustness by catching unexpected inputs early.


313-314: Asserting on unexpected key_get_values values in the job-related function mirrors the task-related function, maintaining consistency and robustness.

cvat/apps/dataset_manager/bindings.py (1)

Line range hint 1-2500: Review the integration of new changes with existing data management strategies to ensure they align with the overall system architecture and performance expectations.

Comment on lines +563 to +564
else:
assert False, f"Label {instance.id} is not attached to either a project or a task"
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error handling for label attachment.

The assertion in lines 563-564 is used to ensure that a label is attached to either a project or a task. However, using assertions for control flow or business logic is not recommended in production code as assertions can be disabled with the -O and -OO flags in Python, which removes these statements. Instead, consider raising an appropriate exception that can be caught and handled properly.

- assert False, f"Label {instance.id} is not attached to either a project or a task"
+ if not (instance.project or instance.task):
+     raise ValueError(f"Label {instance.id} is not attached to either a project or a task")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
else:
assert False, f"Label {instance.id} is not attached to either a project or a task"
else:
if not (instance.project or instance.task):
raise ValueError(f"Label {instance.id} is not attached to either a project or a task")

shape['outside'] = (el.attrib.get('outside') == '1')
shape['keyframe'] = (el.attrib.get('keyframe') == '1')
shape['outside'] = el.attrib.get('outside') == '1'
shape['keyframe'] = el.attrib.get('keyframe') == '1'
Copy link
Contributor

@coderabbitai coderabbitai bot May 28, 2024

Choose a reason for hiding this comment

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

Remove unnecessary parentheses for consistency and readability.

- shape['keyframe'] = el.attrib.get('keyframe') == '1'
+ shape['keyframe'] = el.attrib['keyframe'] == '1'

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
shape['keyframe'] = el.attrib.get('keyframe') == '1'
shape['keyframe'] = el.attrib['keyframe'] == '1'

Copy link
Contributor

Choose a reason for hiding this comment

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

an el.attrb can miss some of these keys (e.g., keyframe, outside). Will you still recommend the change?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nmanovic, you are correct that el.attrib can miss some keys like keyframe and outside. To handle this safely and maintain readability, I recommend using el.attrib.get('keyframe', '0') == '1' and el.attrib.get('outside', '0') == '1'. This approach ensures that a default value is used if the key is missing, preventing potential KeyError.

Here is the updated code:

shape['outside'] = el.attrib.get('outside', '0') == '1'
shape['keyframe'] = el.attrib.get('keyframe', '0') == '1'

This change maintains consistency and readability while safely handling missing keys.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for occurrences of `el.attrib` in the codebase to analyze how missing keys are handled.
rg --pcre2 '\bel\.attrib\b' --glob '!*.pyc'

Length of output: 13817

Copy link
Contributor

Choose a reason for hiding this comment

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

@coderabbitai, why do we need to provide a default value, if None is perfectly fine in the case?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nmanovic, you are correct that using None is functionally equivalent in this context because None == '1' evaluates to False. However, explicitly providing a default value like '0' can make the code's intention clearer and more readable. It explicitly states that if the key is missing, it should be treated as False.

Here is the updated code for clarity:

shape['outside'] = el.attrib.get('outside', '0') == '1'
shape['keyframe'] = el.attrib.get('keyframe', '0') == '1'

This approach ensures that the code is both clear and consistent in handling missing keys.

I tried to update Pylint to the most recent version (3.2.2), which produced
a bunch of new warnings. I couldn't fix all of them in one sitting, but I
fixed some.
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.

None yet

2 participants