Skip to content

Fix param names automatically#2602

Draft
edgarcosta wants to merge 2 commits intoflintlib:mainfrom
edgarcosta:fix-param-names
Draft

Fix param names automatically#2602
edgarcosta wants to merge 2 commits intoflintlib:mainfrom
edgarcosta:fix-param-names

Conversation

@edgarcosta
Copy link
Member

This is a follow-up to #1895 and the manual fixes in #2601. It adds dev/check_param_names.py, a tool that compares function parameter names across headers (.h), documentation (.rst), and source files (.c) to catch mismatches.

The tool treats the header as the source of truth and can auto-fix docs and sources to match. It currently finds 1758 functions with mismatches across 97 modules, totalling 3288 individual parameter name differences. Of these, 2573 can be auto-fixed; 21 are skipped due to local variable name collisions.

Example: fmpzi module

$ python3 dev/check_param_names.py --module fmpzi --check-src

======================================================================
Module: fmpzi
======================================================================

  --- header vs doc ---

  fmpzi_gcd:
    param 0: header has 'g', doc has 'res'
    header (src/fmpzi.h:176)
    doc (doc/source/fmpzi.rst:130)

  fmpzi_gcd_shortest:
    param 0: header has 'g', doc has 'res'
    header (src/fmpzi.h:175)
    doc (doc/source/fmpzi.rst:129)

  --- header vs source ---

  fmpzi_gcd:
    param 0: header has 'g', source has 'res'
    header (src/fmpzi.h:176)
    source (src/fmpzi/gcd.c:1)

  fmpzi_gcd_binary:
    param 1: header has 'x', source has 'X'
    param 2: header has 'y', source has 'Y'
    header (src/fmpzi.h:174)
    source (src/fmpzi/gcd_binary.c:53)

  ...

With --fix --dry-run, the tool shows what it would change and what it must skip:

$ python3 dev/check_param_names.py --module fmpzi --check-src --fix --dry-run

[dry-run] would fix fmpzi_gcd in src/fmpzi.h
  SKIP fmpzi_gcd_binary rename X->x in src/fmpzi/gcd_binary.c: collision with existing 'x'
  SKIP fmpzi_gcd_euclidean rename X->x in src/fmpzi/gcd_euclidean.c: collision with existing 'x'
  ...
[dry-run] would fix fmpzi_gcd_shortest in src/fmpzi.h
[dry-run] would fix fmpzi_gcd_shortest in src/fmpzi/gcd_shortest.c

======================================================================
Fixed: 3, Skipped: 0

Scope of auto-fixable changes per module

Module Functions Params Module Functions Params
gr_poly 97 199 fmpq 22 57
arb_poly 95 189 ca 21 25
acb_poly 84 170 radix 21 79
arb 82 113 fmpz_mpoly_factor 20 25
fmpz 66 118 padic_poly 16 32
gr_mat 66 86 arb_fmpz_poly 15 28
nmod_poly 62 165 nmod_poly_mat 15 19
mag 61 85 ulong_extras 15 24
acb 59 94 fmpz_poly_mat 14 18
acb_hypgeom 52 108 nmod_mpoly_factor 14 21
fmpq_poly 51 145 fq_nmod 13 33
arb_hypgeom 50 99 qqbar 13 19
fmpz_poly 49 90 acb_dft 12 13
arf 47 60 acb_elliptic 12 14
fmpz_mpoly 46 91 bool_mat 12 25
fmpz_mod_poly 42 104 fmpz_mod_mpoly_factor 11 19
mpoly 32 42 nf_elem 11 22
ca_mat 30 49 arith 10 12
fmpz_mod_mpoly 29 43 fq_zech 10 20
fq_nmod_mpoly 28 62 + 57 more modules
acb_mat 27 48 TOTAL 1758 3288
fmpq_mpoly 25 58
nmod_mpoly 23 50

How to proceed?

This tool is ready to auto-fix ~2500 parameter name mismatches across the codebase. However, given the scale, I'd like your input on how to proceed:

  1. One big PR with all auto-fixes applied at once?
  2. Batched PRs grouped by module family (e.g., all arb_*, all fmpz_*, etc.)?
  3. Something else?

The remaining ~21 skipped cases (local variable collisions) would need separate manual fixes regardless of approach.

Usage

# Check all modules (header vs doc)
python3 dev/check_param_names.py

# Check specific module with source comparison
python3 dev/check_param_names.py --module fmpz --check-src

# Dry-run auto-fix
python3 dev/check_param_names.py --fix --check-src --dry-run

# Apply fixes
python3 dev/check_param_names.py --fix --check-src

# Run tests
python3 dev/test_check_param_names.py

Test plan

  • 86 unit tests pass (python3 dev/test_check_param_names.py)

@fredrik-johansson
Copy link
Collaborator

Looks useful! With that said, automated renaming risks

  1. creating inconsistencies with variable names used in surrounding text in the documentation and in code comments.
  2. choosing the less descriptive name in some cases.

I'd rather run this on a few modules at a time, starting with modules that have not been updated recently, to be able to review everything manually.

Python tool (dev/check_param_names.py) that compares function parameter
names across header declarations, RST documentation, and .c source
definitions. Supports auto-fixing mismatches with --fix and --dry-run.

Includes 86 unit tests (dev/test_check_param_names.py).
When two parameters have their names exchanged between header and
source (e.g. header has val,len but source has len,val), the tool
now prints a WARNING flagging a possible bug. This catches cases
like _padic_poly_fprint_pretty where the header and source disagree
on parameter order.
@edgarcosta
Copy link
Member Author

Sounds good, I'll submit fixes a few modules at a time for manual review and to keep improving the script.

Here's the full list grouped by review effort, with the first batch already up in #2604:

Batch 1 (done, #2604)

  • arb_calc (1 fn, 2 params, last modified 2025-10-28)
  • ca_vec (7 fn, 9 params, last modified 2025-11-01)
  • fmpz_extras (1 fn, 3 params, last modified 2025-10-28)
  • padic_poly (16 fn, 32 params, last modified 2025-11-19)
  • thread_pool (1 fn, 2 params, last modified 2025-10-28)
  • Swap fixes in arb, gr_poly, nmod_mat

Batch 2: trivial modules (1-3 functions each, ~50 params total)

  • acb_calc (2 fn, 6 params, last modified 2025-10-28)
  • acf (1 fn, 1 param, last modified 2025-10-28)
  • bernoulli (2 fn, 2 params, last modified 2025-10-28)
  • ca_field (2 fn, 2 params, last modified 2025-11-01)
  • calcium (2 fn, 2 params, last modified 2025-11-01)
  • dlog (2 fn, 2 params, last modified 2025-12-03)
  • fmpq_vec (2 fn, 2 params, last modified 2026-01-24)
  • fmpz_factor (3 fn, 6 params, last modified 2026-01-09)
  • fmpz_mod (3 fn, 3 params, last modified 2025-10-28)
  • fmpz_mod_vec (2 fn, 5 params, last modified 2025-10-28)
  • fmpz_poly_factor (3 fn, 5 params, last modified 2025-10-28)
  • fq_default (2 fn, 4 params, last modified 2025-12-16)
  • gr (1 fn, 1 param, last modified 2026-03-05)
  • gr_vec (1 fn, 1 param, last modified 2025-12-23)
  • hypgeom (3 fn, 5 params, last modified 2025-10-28)
  • mpn_mod (1 fn, 2 params, last modified 2026-03-05)
  • nmod (2 fn, 2 params, last modified 2025-12-16)
  • partitions (2 fn, 2 params, last modified 2025-11-04)
  • qfb (1 fn, 1 param, last modified 2025-10-28)

Batch 3: small modules (4-10 functions each, ~165 params total)

  • acb_modular (8 fn, 9 params, last modified 2025-10-28)
  • acb_theta (6 fn, 7 params, last modified 2026-01-24)
  • aprcl (5 fn, 6 params, last modified 2025-11-04)
  • dirichlet (7 fn, 14 params, last modified 2025-12-03)
  • fexpr (7 fn, 9 params, last modified 2025-11-01)
  • fft_small (5 fn, 9 params, last modified 2026-01-24)
  • fmpz_mod_mat (5 fn, 8 params, last modified 2025-10-28)
  • fmpz_mod_mpoly_q (4 fn, 4 params, last modified 2025-10-28)
  • fmpz_mod_poly_factor (5 fn, 7 params, last modified 2026-01-16)
  • fmpz_mpoly_q (4 fn, 4 params, last modified 2025-11-01)
  • fmpz_vec (6 fn, 11 params, last modified 2025-12-02)
  • fmpzi (5 fn, 11 params, last modified 2025-10-28)
  • fq (6 fn, 10 params, last modified 2025-10-28)
  • fq_nmod_mpoly_factor (6 fn, 10 params, last modified 2025-10-28)
  • fq_zech (9 fn, 14 params, last modified 2026-03-10)
  • gr_generic (4 fn, 11 params, last modified 2025-12-23)
  • gr_mpoly (6 fn, 12 params, last modified 2025-11-01)
  • gr_series (5 fn, 6 params, last modified 2025-11-01)
  • gr_special (6 fn, 7 params, last modified 2025-11-01)
  • nfloat (5 fn, 8 params, last modified 2026-03-05)
  • nmod_mat (4 fn, 8 params, last modified 2026-03-10)
  • nmod_poly_factor (7 fn, 12 params, last modified 2025-10-28)
  • padic (6 fn, 10 params, last modified 2025-11-19)
  • padic_mat (5 fn, 7 params, last modified 2025-01-21)
  • qadic (5 fn, 10 params, last modified 2026-02-19)

Batch 4: medium modules (11-25 functions each, ~530 params total)

  • acb_dirichlet (17 fn, 32 params, last modified 2026-01-10)
  • acb_dft (12 fn, 12 params, last modified 2026-03-10)
  • acb_elliptic (12 fn, 14 params, last modified 2025-10-28)
  • arb_fmpz_poly (15 fn, 28 params, last modified 2025-12-31)
  • arb_mat (22 fn, 37 params, last modified 2026-01-09)
  • arith (10 fn, 12 params, last modified 2026-01-10)
  • bool_mat (12 fn, 25 params, last modified 2024-05-14)
  • ca (21 fn, 25 params, last modified 2026-01-10)
  • ca_poly (19 fn, 47 params, last modified 2025-11-01)
  • fmpq (22 fn, 57 params, last modified 2025-12-20)
  • fmpq_mat (5 fn, 12 params, last modified 2025-10-28)
  • fmpz_mat (17 fn, 30 params, last modified 2026-03-08)
  • fmpz_mod_mpoly_factor (11 fn, 19 params, last modified 2025-10-28)
  • fmpz_mpoly_factor (20 fn, 25 params, last modified 2026-01-18)
  • fmpz_poly_mat (14 fn, 18 params, last modified 2025-10-28)
  • fq_nmod (13 fn, 30 params, last modified 2026-03-10)
  • nf_elem (11 fn, 22 params, last modified 2026-01-24)
  • nmod_mpoly_factor (14 fn, 21 params, last modified 2026-01-10)
  • nmod_poly_mat (15 fn, 19 params, last modified 2026-01-03)
  • qqbar (13 fn, 19 params, last modified 2026-01-10)
  • radix (21 fn, 79 params, last modified 2026-03-04)
  • ulong_extras (15 fn, 24 params, last modified 2026-03-10)

Batch 5: large modules (26+ functions each, ~2470 params total)

  • acb (59 fn, 94 params, last modified 2025-10-28)
  • acb_hypgeom (52 fn, 108 params, last modified 2025-10-28)
  • acb_mat (27 fn, 48 params, last modified 2026-01-09)
  • acb_poly (84 fn, 170 params, last modified 2026-01-09)
  • arb (82 fn, 113 params, last modified 2026-03-11)
  • arb_hypgeom (50 fn, 99 params, last modified 2025-10-28)
  • arb_poly (95 fn, 189 params, last modified 2025-12-20)
  • arf (47 fn, 60 params, last modified 2026-01-10)
  • ca_mat (30 fn, 49 params, last modified 2026-01-10)
  • fmpq_mpoly (25 fn, 57 params, last modified 2026-03-10)
  • fmpq_poly (51 fn, 145 params, last modified 2026-01-24)
  • fmpz (66 fn, 117 params, last modified 2026-03-11)
  • fmpz_mod_mpoly (29 fn, 43 params, last modified 2026-01-24)
  • fmpz_mod_poly (42 fn, 104 params, last modified 2026-01-24)
  • fmpz_mpoly (46 fn, 91 params, last modified 2026-01-10)
  • fmpz_poly (49 fn, 90 params, last modified 2026-01-24)
  • fq_nmod_mpoly (28 fn, 62 params, last modified 2026-01-10)
  • gr_mat (66 fn, 86 params, last modified 2026-01-24)
  • gr_poly (97 fn, 198 params, last modified 2026-03-11)
  • mag (61 fn, 85 params, last modified 2026-01-10)
  • mpn_extras (17 fn, 67 params, last modified 2026-03-10)
  • mpoly (32 fn, 42 params, last modified 2026-03-10)
  • nmod_mpoly (23 fn, 50 params, last modified 2026-01-24)
  • nmod_poly (62 fn, 164 params, last modified 2026-01-19)

@edgarcosta
Copy link
Member Author

I might need to think a bit more about the next batches. They look horrendous to review. Maybe an interim approach is to have this merged and get CI to check the modules that we declare "done".

What do you think?

I would also take suggestions how to make this less painful to review. As we saw from the last one #2604, it took a couple of iterations between @fredrik-johansson and me to get it right, mostly the blame is on me.

@fredrik-johansson
Copy link
Collaborator

I might need to think a bit more about the next batches. They look horrendous to review. Maybe an interim approach is to have this merged and get CI to check the modules that we declare "done".

That's an option.

I would also take suggestions how to make this less painful to review. As we saw from the last one #2604, it took a couple of iterations between @fredrik-johansson and me to get it right, mostly the blame is on me.

If you want, we could schedule a session where I review the whole PR and we discuss the issues to fix in real time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants