100-days-mlops-kodekloud

Set Up Code Quality Tools for ML Code

Problem

The xFusionCorp Industries ML team enforces code quality with ruff and black on every pull request. The project at /root/code/fraud-detection/ currently fails both tools. Make it pass them.

  1. The project at /root/code/fraud-detection/ contains a pyproject.toml and sample sources under src/.

  2. The corrected project must meet the following requirements:

    • ruff and black are both configured with a line length of 120.
    • ruff lint rule selection includes E, F, W, and I, and is declared under [tool.ruff.lint] – The schema required by ruff 0.1 and later.
    • Running ruff check src/ from the project directory exits with status 0.
    • Running black --check src/ from the project directory exits with status 0.
  3. Review the existing configuration and source files, and correct everything that prevents the two commands above from exiting cleanly.

ruff, black, and mypy are already installed.

Solution

  1. Let’s update pyproject.toml according to the task requirements:

     [project]
     name = "fraud-detection"
     version = "0.1.0"
    
     [tool.ruff]
     line-length = 120
     lint.select = ["E", "F", "W", "I"]
    
     [tool.black]
     line-length = 120
    
  2. Let’s run ruff check command:

     cd fraud-detection
     ruff check src/
    

    It can give us following error:

     F401 [*] `os` imported but unused
     --> src/data/process_data.py:1:8
     |
     1 | import os
     |        ^^
     2 | import pandas as pd
     |
     help: Remove unused import: `os`
    
     Found 1 error.
     [*] 1 fixable with the `--fix` option.
    

    let’s fix it with --fix option:

     ruff check src/ --fix
    
  3. Let’s run black --check command:

     black --check src/
    
     black --check src/
     All done! ✨ 🍰 ✨
     5 files would be left unchanged.
    
  4. Both commands exit with status 0, indicating that the code now meets the quality standards set by ruff and black.