From 658d044ec8029ec208613b9260a2d5075e73140a Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Fri, 6 Mar 2026 07:52:36 -0500 Subject: [PATCH] Switch from black to ruff format Replace black with ruff for code formatting in Makefile, CI workflows, and dev dependencies. Reformat all files with ruff defaults. Co-Authored-By: Claude Opus 4.6 --- .github/bump_version.py | 4 +--- .github/workflows/pr.yaml | 6 +++--- .github/workflows/push.yaml | 6 +++--- Makefile | 2 +- changelog.d/switch-to-ruff.changed.md | 1 + docs/api_utils.py | 14 +++++--------- policyengine_ng/variables/marginal_tax_rate.py | 16 ++++------------ policyengine_ng/variables/tax/is_tax_exempt.py | 5 +---- policyengine_ng/variables/tax/taxable_income.py | 4 +--- pyproject.toml | 1 + 10 files changed, 21 insertions(+), 38 deletions(-) create mode 100644 changelog.d/switch-to-ruff.changed.md diff --git a/.github/bump_version.py b/.github/bump_version.py index bb0fd6d..779a82e 100644 --- a/.github/bump_version.py +++ b/.github/bump_version.py @@ -19,9 +19,7 @@ def get_current_version(pyproject_path: Path) -> str: def infer_bump(changelog_dir: Path) -> str: fragments = [ - f - for f in changelog_dir.iterdir() - if f.is_file() and f.name != ".gitkeep" + f for f in changelog_dir.iterdir() if f.is_file() and f.name != ".gitkeep" ] if not fragments: print("No changelog fragments found", file=sys.stderr) diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index c9ad9c2..2e22a7d 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -7,10 +7,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Install ruff + run: pip install ruff - name: Check formatting - uses: "lgeiger/black-action@master" - with: - args: ". -l 79 --check" + run: ruff format --check . check-changelog: name: Check changelog fragment runs-on: ubuntu-latest diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 95d0717..85fa2e2 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -10,10 +10,10 @@ jobs: && (github.event.head_commit.message == 'Update PolicyEngine Nigeria') steps: - uses: actions/checkout@v4 + - name: Install ruff + run: pip install ruff - name: Check formatting - uses: "lgeiger/black-action@master" - with: - args: ". -l 79 --check" + run: ruff format --check . versioning: name: Update versioning if: | diff --git a/Makefile b/Makefile index 16cdb18..40747fa 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ documentation: myst build docs format: - black . -l 79 + ruff format . install: pip install -e .[dev] diff --git a/changelog.d/switch-to-ruff.changed.md b/changelog.d/switch-to-ruff.changed.md new file mode 100644 index 0000000..a514e08 --- /dev/null +++ b/changelog.d/switch-to-ruff.changed.md @@ -0,0 +1 @@ +Switch from black to ruff format. diff --git a/docs/api_utils.py b/docs/api_utils.py index e722a25..6b63cf7 100644 --- a/docs/api_utils.py +++ b/docs/api_utils.py @@ -67,9 +67,7 @@ def calculate(household: dict, reform: dict) -> dict: if any([math.isinf(value) for value in result]): raise ValueError("Infinite value") else: - household[entity_plural][entity_id][variable_name][ - period - ] = result + household[entity_plural][entity_id][variable_name][period] = result else: entity_index = population.get_index(entity_id) if variable.value_type == Enum: @@ -86,17 +84,15 @@ def calculate(household: dict, reform: dict) -> dict: else: entity_result = result.tolist()[entity_index] - household[entity_plural][entity_id][variable_name][ - period - ] = entity_result + household[entity_plural][entity_id][variable_name][period] = ( + entity_result + ) except Exception as e: if "axes" in household: pass else: logging.warn(f"Error computing {variable_name}: {e}") - household[entity_plural][entity_id][variable_name][ - period - ] = None + household[entity_plural][entity_id][variable_name][period] = None return household diff --git a/policyengine_ng/variables/marginal_tax_rate.py b/policyengine_ng/variables/marginal_tax_rate.py index 537a7f3..48a9b54 100644 --- a/policyengine_ng/variables/marginal_tax_rate.py +++ b/policyengine_ng/variables/marginal_tax_rate.py @@ -13,14 +13,10 @@ def formula(person, period, parameters): simulation = person.simulation adult_index_values = person("adult_index", period) DELTA = 1_000 - mtr_adult_count = parameters( - period - ).simulation.marginal_tax_rate_adults + mtr_adult_count = parameters(period).simulation.marginal_tax_rate_adults print(simulation.input_variables) for adult_index in range(1, 1 + mtr_adult_count): - alt_simulation = simulation.get_branch( - f"adult_{adult_index}_pay_rise" - ) + alt_simulation = simulation.get_branch(f"adult_{adult_index}_pay_rise") mask = adult_index_values == adult_index for variable in simulation.tax_benefit_system.variables: if variable not in simulation.input_variables: @@ -31,15 +27,11 @@ def formula(person, period, parameters): person("employment_income", period) + mask * DELTA, ) alt_person = alt_simulation.person - household_net_income = person.household( - "household_net_income", period - ) + household_net_income = person.household("household_net_income", period) household_net_income_higher_earnings = alt_person.household( "household_net_income", period ) - increase = ( - household_net_income_higher_earnings - household_net_income - ) + increase = household_net_income_higher_earnings - household_net_income mtr_values += where(mask, 1 - increase / DELTA, 0) print(household_net_income) print(household_net_income_higher_earnings) diff --git a/policyengine_ng/variables/tax/is_tax_exempt.py b/policyengine_ng/variables/tax/is_tax_exempt.py index bb928f3..20d15c3 100644 --- a/policyengine_ng/variables/tax/is_tax_exempt.py +++ b/policyengine_ng/variables/tax/is_tax_exempt.py @@ -15,7 +15,4 @@ def formula(person, period, parameters): ) gross_income = person("gross_income", period) has_non_employment_income = gross_income > employment_income - return ( - employment_income_at_or_below_threshold - & ~has_non_employment_income - ) + return employment_income_at_or_below_threshold & ~has_non_employment_income diff --git a/policyengine_ng/variables/tax/taxable_income.py b/policyengine_ng/variables/tax/taxable_income.py index acf5749..dfd2c9c 100644 --- a/policyengine_ng/variables/tax/taxable_income.py +++ b/policyengine_ng/variables/tax/taxable_income.py @@ -10,7 +10,5 @@ class taxable_income(Variable): def formula(person, period, parameters): gross_income = person("gross_income", period) - consolidated_relief_allowance = person( - "consolidated_relief_allowance", period - ) + consolidated_relief_allowance = person("consolidated_relief_allowance", period) return max_(0, gross_income - consolidated_relief_allowance) diff --git a/pyproject.toml b/pyproject.toml index 113e50c..f4cfe79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ dev = [ "build", "jupyter-book>=1.0.4", "mystmd>=1.3.17", + "ruff>=0.9.0", "towncrier>=24.8.0", ]