5.5 to 5.7 The fixes

Read the tutorial. Build it yourself is the other way to read this part.

This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at b947593e84b7, partway through step-05. The code before this part

To look at this code on your machine, run these commands. The first one makes a clone, if this folder has none:

[ -d cheesecracker ] || git clone https://github.com/rahuldave/cheesecracker
cd cheesecracker
git fetch --quiet --tags --force origin
git switch --detach b947593e84b7
just setup

The formatter fixes the layout, and the linter removes the import that nothing uses. A person shortens the long line.

5.5 the formatter fixes the spaces, the blank lines and the line breaks

The formatter writes every file in one layout, so nobody fixes spaces by hand.

This change came from running just fmt.

ruff prints 2 files reformatted. In service.py, z=power(x,2)+power(y,2) now has its spaces, and two blank lines separate the routes. The long return now spreads its dictionary over six lines, one key on each line. In utils.py, the blank lines are two.

just fmt --check

ruff prints 6 files already formatted.

What changed

  • src/api-service/api/service.py: the spaces, the blank lines, and the long return on six lines. 10 lines added, 2 removed. It comes from just fmt. The file
    • look at z = power(x, 2) + power(y, 2)
  • src/api-service/api/utils.py: the space after the comma, and two blank lines before unused_function(). 1 line added, 2 removed. It comes from just fmt. The file
    • look at def power(base, exponent):

The diff of src/api-service/api/service.py
@@ -20,10 +20,13 @@ app.add_middleware(
     allow_headers=["*"],
 )
 
+
 # Routes
 @app.get("/")
 async def get_index():
     return {"message": "Welcome to the Cheese App CI/CD Tutorial!"}
+
+
 @app.get("/health")
 async def health_check():
     return {"status": "healthy"}
@@ -34,6 +37,11 @@ async def calculate_euclidean_distance(x: float = 1, y: float = 2):
     """Calculate the Euclidean distance from origin: sqrt(x^2 + y^2)
     Returns the distance of a point (x, y) from the origin (0, 0)
     """
-    z=power(x,2)+power(y,2)
+    z = power(x, 2) + power(y, 2)
     result = math.sqrt(z)
-    return {"x": x, "y": y, "distance": result, "message": "This is a very long line that exceeds 120 characters and Black will complain about it so we need to fix this formatting issue"}
+    return {
+        "x": x,
+        "y": y,
+        "distance": result,
+        "message": "This is a very long line that exceeds 120 characters and Black will complain about it so we need to fix this formatting issue",
+    }

The diff of src/api-service/api/utils.py
@@ -4,11 +4,10 @@ Used by the Cheese App API Service
 """
 
 
-def power(base,exponent):
+def power(base, exponent):
     """Calculate base raised to the power of exponent"""
     return base**exponent
 
 
-
 def unused_function():
     pass

This change on GitHub The code after it

5.6 the linter removes the import that nothing uses

ruff can remove an import that nothing uses, because the change cannot alter what the code does. The linter still fails after the fix, because one error is left. || true makes the command count as a success all the same.

This change came from running just lint --fix || true.

ruff removes the line import os from service.py, and still reports the long line.

just lint

ruff finds 1 error, E501, and fails. The line of the message is 147 characters long, and no tool can shorten words.

What changed

  • src/api-service/api/service.py: the import of os, removed. 0 lines added, 1 removed. It comes from just lint --fix || true. The file

The diff of src/api-service/api/service.py
@@ -5,7 +5,6 @@ Cheese App API Service
 from fastapi import FastAPI
 from starlette.middleware.cors import CORSMiddleware
 import math
-import os
 from api.utils import power
 
 # Setup FastAPI app

This change on GitHub The code after it

5.7 a shorter line, by hand

A long message is a choice of words, so a person has to shorten it. The new message is short enough for one line.

± See the change to src/api-service/api/service.py

The value of "message" in the return of calculate_euclidean_distance() is now shorter than 120 characters.

just lint

ruff prints All checks passed!. Note that ruff never reports unused_function(). No rule of E, W or F looks for a function that nothing calls.

What changed

  • src/api-service/api/service.py: a shorter message. 1 line added, 1 removed. The file
    • look at "message": "This is a very long line that exceeds 120 characters blah",

The diff of src/api-service/api/service.py
@@ -42,5 +42,5 @@ async def calculate_euclidean_distance(x: float = 1, y: float = 2):
         "x": x,
         "y": y,
         "distance": result,
-        "message": "This is a very long line that exceeds 120 characters and Black will complain about it so we need to fix this formatting issue",
+        "message": "This is a very long line that exceeds 120 characters blah",
     }

This change on GitHub

This part ends at the code at 693a713a458d, partway through step-05. The code after this part