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

CLI: remove imports that were deprecated in Python 3.9 #8640

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented Nov 4, 2024

Motivation and context

This is a continuation of #8626.

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

  • New Features

    • No new features added; focus on type hint improvements.
  • Bug Fixes

    • No bug fixes included; changes are syntactical and do not affect functionality.
  • Refactor

    • Updated type hints from List and Dict to built-in list and dict for various methods and attributes across multiple files.
    • Adjusted import statements for type hinting best practices.
  • Documentation

    • No changes to documentation noted.

Copy link
Contributor

coderabbitai bot commented Nov 4, 2024

Walkthrough

The changes in this pull request involve updating type hints across multiple files in the CVAT CLI codebase. The modifications primarily transition from using types from the typing module (like List, Dict, and Tuple) to their built-in counterparts (list, dict, and tuple) in accordance with modern Python practices. Additionally, some import statements have been adjusted to reflect these changes. The overall functionality and control flow of the code remain unchanged.

Changes

File Path Change Summary
cvat-cli/src/cvat_cli/__main__.py Updated args type hint from List[str] to list[str].
cvat-cli/src/cvat_cli/_internal/command_base.py Updated _commands type hint from Dict[str, Command] to dict[str, Command]. Modified import statement to use Mapping from collections.abc.
cvat-cli/src/cvat_cli/_internal/commands.py Updated labels parameter in TaskCreate.execute from List[Dict[str, str]] to list[dict[str, str]]. Updated function_parameters in TaskAutoAnnotate.execute from Dict[str, Any] to dict[str, Any]. Removed List from imports.
cvat-cli/src/cvat_cli/_internal/parsers.py Updated return type of parse_function_parameter from Tuple[str, Any] to tuple[str, Any]. Removed Tuple from imports.

Poem

🐰 In the land of code where bunnies play,
Type hints have changed in a modern way.
From typing's old forms to built-ins so bright,
Our code now shines with clarity and light!
Hopping along, we embrace the new,
With lists and dicts, our joy only grew! 🌟


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 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.

CodeRabbit Configuration 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

sonarqubecloud bot commented Nov 4, 2024

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

🧹 Outside diff range and nitpick comments (1)
cvat-cli/src/cvat_cli/_internal/commands.py (1)

473-475: LGTM: Type hint modernization with a suggestion.

The change from Dict[str, Any] to dict[str, Any] aligns with PEP 585. Given the dynamic nature of function_parameters, consider adding a docstring to document expected parameter types and formats.

Consider adding a docstring like:

def execute(
    self,
    client: Client,
    *,
    task_id: int,
    function_parameters: dict[str, Any],
    clear_existing: bool = False,
    allow_unmatched_labels: bool = False,
) -> None:
    """Execute auto-annotation on a task.
    
    Args:
        client: CVAT client instance
        task_id: ID of the task to annotate
        function_parameters: Dictionary of parameters passed to the annotation function.
            The values can be of any type supported by the function.
        clear_existing: Whether to remove existing annotations
        allow_unmatched_labels: Whether to allow labels not in task configuration
    """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 67f9afd and 45968bf.

📒 Files selected for processing (4)
  • cvat-cli/src/cvat_cli/__main__.py (1 hunks)
  • cvat-cli/src/cvat_cli/_internal/command_base.py (2 hunks)
  • cvat-cli/src/cvat_cli/_internal/commands.py (3 hunks)
  • cvat-cli/src/cvat_cli/_internal/parsers.py (2 hunks)
🔇 Additional comments (7)
cvat-cli/src/cvat_cli/__main__.py (1)

20-20: LGTM! Type hint modernization looks good.

The change from List[str] to list[str] aligns with PEP 585 and improves Python 3.9+ compatibility.

Let's verify if there are any remaining uses of deprecated type hints that need similar updates:

✅ Verification successful

Based on the search results, there are still many occurrences of deprecated type hints from the typing module (List, Dict, Tuple) across the codebase. However, since this PR is focused on modernizing type hints in the CLI component, and the change in __main__.py is consistent with that scope, I'll generate the final response:

Type hint modernization is correctly scoped to CLI component

The change from List[str] to list[str] in __main__.py is appropriate and aligns with PEP 585. While there are other files in the codebase still using the deprecated typing module types, those changes should be handled in separate PRs to maintain focused scope and minimize risk.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for remaining usage of typing.List, typing.Dict, typing.Tuple
# that might need to be updated to use built-in types instead

echo "Checking for remaining deprecated type hints..."
rg -l "from typing import .*(?:List|Dict|Tuple)" || true
rg "\b(?:List|Dict|Tuple)\[" || true

Length of output: 31111

cvat-cli/src/cvat_cli/_internal/parsers.py (2)

9-9: LGTM! Correctly retained Any import.

The code correctly maintains the import of Any from typing, which is necessary as it has no built-in equivalent, while removing deprecated imports.


31-31: LGTM! Appropriate use of built-in generic type.

The change from Tuple[str, Any] to tuple[str, Any] aligns with PEP 585, which enables the use of built-in collections as generic types in Python 3.9+.

Let's verify the Python version requirement is properly documented:

✅ Verification successful

Let me gather more information by checking other potential locations where Python version requirements might be documented.


Python version requirement is properly documented

The Python version requirement (>=3.9) is correctly documented in the setup.py file:

python_requires=">=3.9"

This aligns with the usage of built-in generic types like tuple[str, Any] which were introduced in Python 3.9. Additionally, the black configuration in pyproject.toml also targets Python 3.9 with target-version = ['py39'].

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if Python version requirement is documented
# Expected: Should find Python version requirement >= 3.9 in setup files or requirements

rg -l "python.*3\.9" --type yaml --type toml
cat pyproject.toml 2>/dev/null || true
cat setup.cfg 2>/dev/null || true

Length of output: 945


Script:

#!/bin/bash
# Check setup.py, requirements files, and README for Python version requirements
fd -e py setup.py -x cat {}
fd -e txt -e in requirements -x cat {}
cat README.md 2>/dev/null || true

Length of output: 20873

cvat-cli/src/cvat_cli/_internal/command_base.py (2)

7-7: LGTM! Correct migration of Mapping import.

The change to import Mapping from collections.abc instead of typing aligns with PEP 585 and Python 3.9+ best practices.


25-25: LGTM! Appropriate modernization of type hints.

The change from Dict[str, Command] to dict[str, Command] follows PEP 585, which enables the use of built-in collections as generic types. This is part of the broader effort to modernize type hints across the codebase while maintaining type safety.

cvat-cli/src/cvat_cli/_internal/commands.py (2)

12-12: LGTM: Import changes align with PEP 585.

The changes correctly migrate from deprecated typing collection types to their built-in counterparts, following Python 3.9+ best practices.

Also applies to: 14-14


207-207: LGTM: Type hint update follows modern Python practices.

The change from List[Dict[str, str]] to list[dict[str, str]] aligns with PEP 585.

Let's verify backward compatibility with older Python versions:

✅ Verification successful

Type hint change is safe and compatible with the codebase requirements

The verification confirms this change is safe because:

  • The file already uses from __future__ import annotations, which provides forward compatibility
  • The package requires Python >= 3.9 (specified in setup.py), which fully supports PEP 585 built-in collection type hints
  • No conditional typing imports or version-specific type handling is present in the file
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if there are any version-specific imports or type checking
# that might break with this change.

# Look for any Python version checks or typing-related conditional imports
rg -l "sys.version_info|typing.TYPE_CHECKING|from __future__ import annotations" 

# Look for any typing-related configuration in pyproject.toml or setup.cfg
fd -e toml -e cfg -x cat {} | rg "^python_requires|typing"

Length of output: 1282


Script:

#!/bin/bash
# Let's check the specific file for any Python version requirements or typing imports
rg "from typing|from __future__|import typing|python_version|python_requires" cvat-cli/src/cvat_cli/_internal/commands.py -A 2

# Check for Python version requirements in the package configuration
fd "setup.(py|cfg)" "cvat-cli" -x cat {}

# Check for pyproject.toml in cvat-cli directory
fd "pyproject.toml" "cvat-cli" -x cat {}

Length of output: 2535

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.26%. Comparing base (ceba1b7) to head (45968bf).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8640      +/-   ##
===========================================
- Coverage    74.30%   74.26%   -0.04%     
===========================================
  Files          401      401              
  Lines        43419    43421       +2     
  Branches      3951     3951              
===========================================
- Hits         32263    32248      -15     
- Misses       11156    11173      +17     
Components Coverage Δ
cvat-ui 78.56% <100.00%> (-0.09%) ⬇️
cvat-server 70.58% <ø> (ø)

@SpecLad SpecLad merged commit 2560a0d into cvat-ai:develop Nov 5, 2024
34 checks passed
@SpecLad SpecLad deleted the deprecated-typing branch November 5, 2024 10:53
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