Skip to content

Commit 476da50

Browse files
MaxGhenisclaude
andauthored
Switch code formatter from Black to Ruff (#3317)
Replace Black with Ruff for code formatting across the project: - Update Makefile format target to use `ruff format .` - Replace Black GitHub Actions with `ruff format --check .` in CI - Add ruff>=0.9.0 as a dev dependency in setup.py - Reformat all Python files with ruff defaults Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9c7e1c8 commit 476da50

File tree

73 files changed

+403
-1019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+403
-1019
lines changed

.github/workflows/pr.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
uses: actions/checkout@v4
1515
- name: Setup Python
1616
uses: actions/setup-python@v5
17-
- name: Format with Black
18-
uses: psf/black@stable
19-
with:
20-
options: ". -l 79 --check"
17+
- name: Install ruff
18+
run: pip install ruff>=0.9.0
19+
- name: Format check with ruff
20+
run: ruff format --check .
2121
check-version:
2222
name: Check version
2323
runs-on: ubuntu-latest

.github/workflows/push.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ jobs:
2020
steps:
2121
- name: Checkout repo
2222
uses: actions/checkout@v4
23-
- name: Check formatting
24-
uses: "lgeiger/black-action@master"
25-
with:
26-
args: ". -l 79 --check"
23+
- name: Setup Python
24+
uses: actions/setup-python@v5
25+
- name: Install ruff
26+
run: pip install ruff>=0.9.0
27+
- name: Format check with ruff
28+
run: ruff format --check .
2729
ensure-model-version-aligns-with-sim-api:
2830
name: Ensure model version aligns with simulation API
2931
runs-on: ubuntu-latest

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ debug-test:
1616
MAX_HOUSEHOLDS=1000 FLASK_DEBUG=1 pytest -vv --durations=0 tests
1717

1818
format:
19-
black . -l 79
19+
ruff format .
2020

2121
deploy:
2222
python gcp/export.py

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: minor
2+
changes:
3+
changed:
4+
- Switched code formatter from Black to Ruff.

dashboard/app.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919

2020
st.subheader("Look up a policy")
2121

22-
policy_id = int(
23-
st.text_input("Enter a policy ID", "1", key="policy_lookup_text")
24-
)
25-
country_id = st.text_input(
26-
"Enter a country ID", "uk", key="policy_lookup_country"
27-
)
22+
policy_id = int(st.text_input("Enter a policy ID", "1", key="policy_lookup_text"))
23+
country_id = st.text_input("Enter a country ID", "uk", key="policy_lookup_country")
2824
if st.button("Look up policy", key="policy_lookup"):
2925
try:
3026
results = database.query(
@@ -40,9 +36,7 @@
4036

4137
policy_id = int(st.text_input("Enter a policy ID", "1"))
4238
country_id = st.text_input("Enter a country ID", "uk")
43-
new_label = st.text_input(
44-
"Enter a new label", "New label", key="policy_label_text"
45-
)
39+
new_label = st.text_input("Enter a new label", "New label", key="policy_label_text")
4640
if st.button("Set policy label", key="policy_label"):
4741
try:
4842
database.set_policy_label(policy_id, country_id, new_label)
@@ -54,12 +48,8 @@
5448

5549
st.subheader("Delete a policy")
5650

57-
policy_id = int(
58-
st.text_input("Enter a policy ID", "1", key="policy_delete_text")
59-
)
60-
country_id = st.text_input(
61-
"Enter a country ID", "uk", key="policy_delete_country"
62-
)
51+
policy_id = int(st.text_input("Enter a policy ID", "1", key="policy_delete_text"))
52+
country_id = st.text_input("Enter a country ID", "uk", key="policy_delete_country")
6353
if st.button("Delete policy", key="policy_delete"):
6454
try:
6555
database.delete_policy(policy_id, country_id)

dashboard/experiments/gpt4_api_user.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
@st.cache_data
1515
def get_embeddings_df():
16-
embeddings_df = pd.read_csv(
17-
"parameter_embeddings.csv.gz", compression="gzip"
18-
)
19-
embeddings_df.parameter_embedding = (
20-
embeddings_df.parameter_embedding.apply(lambda x: eval(x))
16+
embeddings_df = pd.read_csv("parameter_embeddings.csv.gz", compression="gzip")
17+
embeddings_df.parameter_embedding = embeddings_df.parameter_embedding.apply(
18+
lambda x: eval(x)
2119
)
2220
return embeddings_df
2321

@@ -55,11 +53,7 @@ def embed(prompt, engine="text-embedding-ada-002"):
5553
lambda x: cosine_similarity(x, embedding)
5654
)
5755

58-
top5 = (
59-
embeddings_df.sort_values("similarities", ascending=False)
60-
.head(5)["json"]
61-
.values
62-
)
56+
top5 = embeddings_df.sort_values("similarities", ascending=False).head(5)["json"].values
6357
# display in streamlit
6458

6559

gcp/bump_country_package.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ def main():
1616
help="Country package to bump",
1717
choices=["policyengine-uk", "policyengine-us", "policyengine-canada"],
1818
)
19-
parser.add_argument(
20-
"--version", type=str, required=True, help="Version to bump to"
21-
)
19+
parser.add_argument("--version", type=str, required=True, help="Version to bump to")
2220
args = parser.parse_args()
2321
country = args.country
2422
version = args.version
@@ -44,9 +42,9 @@ def bump_country_package(country, version):
4442
with open(setup_py_path, "w") as f:
4543
f.write(setup_py)
4644

47-
country_package_full_name = country.replace(
48-
"policyengine", "PolicyEngine"
49-
).replace("-", " ")
45+
country_package_full_name = country.replace("policyengine", "PolicyEngine").replace(
46+
"-", " "
47+
)
5048
country_id = country.replace("policyengine-", "")
5149
country_package_full_name = country_package_full_name.replace(
5250
country_id, country_id.upper()

gcp/export.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@
3232
dockerfile = dockerfile.replace(
3333
".github_microdata_token", GITHUB_MICRODATA_TOKEN
3434
)
35-
dockerfile = dockerfile.replace(
36-
".anthropic_api_key", ANTHROPIC_API_KEY
37-
)
35+
dockerfile = dockerfile.replace(".anthropic_api_key", ANTHROPIC_API_KEY)
3836
dockerfile = dockerfile.replace(".openai_api_key", OPENAI_API_KEY)
39-
dockerfile = dockerfile.replace(
40-
".hugging_face_token", HUGGING_FACE_TOKEN
41-
)
37+
dockerfile = dockerfile.replace(".hugging_face_token", HUGGING_FACE_TOKEN)
4238

4339
with open(dockerfile_location, "w") as f:
4440
f.write(dockerfile)

policyengine_api/ai_prompts/simulation_analysis_prompt.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,12 @@ def generate_simulation_analysis_prompt(params: InboundParameters) -> str:
9595
)
9696

9797
impact_budget: str = json.dumps(parameters.impact["budget"])
98-
impact_intra_decile: dict[str, Any] = json.dumps(
99-
parameters.impact["intra_decile"]
100-
)
98+
impact_intra_decile: dict[str, Any] = json.dumps(parameters.impact["intra_decile"])
10199
impact_decile: str = json.dumps(parameters.impact["decile"])
102100
impact_inequality: str = json.dumps(parameters.impact["inequality"])
103101
impact_poverty: str = json.dumps(parameters.impact["poverty"]["poverty"])
104-
impact_deep_poverty: str = json.dumps(
105-
parameters.impact["poverty"]["deep_poverty"]
106-
)
107-
impact_poverty_by_gender: str = json.dumps(
108-
parameters.impact["poverty_by_gender"]
109-
)
102+
impact_deep_poverty: str = json.dumps(parameters.impact["poverty"]["deep_poverty"])
103+
impact_poverty_by_gender: str = json.dumps(parameters.impact["poverty_by_gender"])
110104

111105
all_parameters: AllParameters = AllParameters.model_validate(
112106
{

policyengine_api/api.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ def log_timing(message):
132132

133133
app.route("/<country_id>/calculate-full", methods=["POST"])(
134134
cache.cached(make_cache_key=make_cache_key)(
135-
lambda *args, **kwargs: get_calculate(
136-
*args, **kwargs, add_missing=True
137-
)
135+
lambda *args, **kwargs: get_calculate(*args, **kwargs, add_missing=True)
138136
)
139137
)
140138
log_timing("Calculate-full endpoint registered")
@@ -153,9 +151,7 @@ def log_timing(message):
153151
app.route("/<country_id>/user-policy", methods=["PUT"])(update_user_policy)
154152
log_timing("User policy update endpoint registered")
155153

156-
app.route("/<country_id>/user-policy/<user_id>", methods=["GET"])(
157-
get_user_policy
158-
)
154+
app.route("/<country_id>/user-policy/<user_id>", methods=["GET"])(get_user_policy)
159155
log_timing("User policy get endpoint registered")
160156

161157
app.register_blueprint(user_profile_bp)
@@ -177,19 +173,15 @@ def log_timing(message):
177173

178174
@app.route("/liveness-check", methods=["GET"])
179175
def liveness_check():
180-
return flask.Response(
181-
"OK", status=200, headers={"Content-Type": "text/plain"}
182-
)
176+
return flask.Response("OK", status=200, headers={"Content-Type": "text/plain"})
183177

184178

185179
log_timing("Liveness check endpoint registered")
186180

187181

188182
@app.route("/readiness-check", methods=["GET"])
189183
def readiness_check():
190-
return flask.Response(
191-
"OK", status=200, headers={"Content-Type": "text/plain"}
192-
)
184+
return flask.Response("OK", status=200, headers={"Content-Type": "text/plain"})
193185

194186

195187
log_timing("Readiness check endpoint registered")

0 commit comments

Comments
 (0)