zimm0140 / AI-Playground

AI PC starter app for doing AI image creation, image stylizing, and chatbot on a PC powered by an Intel® Arc™ GPU.
MIT License
0 stars 0 forks source link

Add Type Hints to Enhance Code Clarity and Maintenance #3

Open SZim92 opened 2 months ago

SZim92 commented 2 months ago

Description:

To improve code readability and maintainability, type hints will be added to the codebase. Type hints help developers understand the expected types of variables, function parameters, and return values, making the code easier to read and reducing the likelihood of errors. This issue will track the progress of adding type hints throughout the project.

Objectives:

  1. Identify key classes and functions lacking type hints.
  2. Add appropriate type hints to all identified classes and functions.
  3. Ensure all unit tests pass after the addition of type hints.

Tasks:

  1. Identify Key Classes and Functions:

    • Review the codebase to identify classes and functions missing type hints.
  2. Add Type Hints:

    • Add type hints to class attributes, function parameters, and return values.
    • Ensure the type hints are accurate and provide meaningful information.
  3. Verify Changes:

    • Run all existing unit tests to ensure they pass.
    • Use a type checker (e.g., mypy) to verify the correctness of the type hints.

Implementation Plan:

  1. Identify Missing Type Hints:

    • Review the codebase manually or use tools to identify areas lacking type hints.
  2. Example Additions:

    • Current Code:

      class ModelDownloaderApi:
       def __init__(self):
           self.file_queue = list()
           self.fs = HfFileSystem()
           self.total_size = 0
      
       def get_info(self, repo_id, is_sd=False):
           self.repo_id = repo_id
           self.repo_folder = repo_id.replace('/', '---')
           self.file_queue.clear()
           self.total_size = 0
           self.enum_file_list(repo_id, is_sd, True)
           print(dumps({"total_size": self.total_size, "file_list": self.file_queue}))
    • Improved Code:

      from typing import List
      
      class ModelDownloaderApi:
       repo_id: str
       file_queue: List[dict]
       total_size: int
       fs: HfFileSystem
       repo_folder: str
      
       def __init__(self):
           self.file_queue: List[dict] = list()
           self.fs: HfFileSystem = HfFileSystem()
           self.total_size: int = 0
      
       def get_info(self, repo_id: str, is_sd: bool = False) -> None:
           self.repo_id = repo_id
           self.repo_folder = repo_id.replace('/', '---')
           self.file_queue.clear()
           self.total_size = 0
           self.enum_file_list(repo_id, is_sd, True)
           print(dumps({"total_size": self.total_size, "file_list": self.file_queue}))
  3. Verify with Unit Tests and Type Checker:

    • Run pytest to execute all unit tests.
    • Use mypy to check type hints:
      mypy <directory>

Steps to Reproduce:

  1. Clone the repository:
    git clone <repository-url>
  2. Navigate to the directory containing the code to be refactored.
  3. Identify areas lacking type hints.
  4. Add appropriate type hints to classes and functions.
  5. Run the tests to verify the changes:
    pytest
  6. Run mypy to check type hints:
    mypy <directory>

Expected Behavior:

Current Behavior:

Additional Information:

Progress Log:

Use this section to document ongoing progress, updates, and any changes made: