Skip to content

Avoid re-using variable names in tests - #354

Merged
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:astrom-vars-in-tests
Oct 9, 2026
Merged

avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:astrom-vars-in-tests

Conversation

@eerovaher

Copy link
Copy Markdown
Collaborator

I have annotated more pyerfa ufuncs as np.ufunc instances in my local repository, but the improved annotations are triggering type-checking errors in erfa/tests/test_ufunc.py. The errors are caused by code that looks like

astrom = erfa_ufunc...
astrom = astrom.view(np.recarray)

With the improved annotations type checkers can infer that the astrom in the first line is an array or an array scalar, but the result of the view() method is an np.recarray instance, which is incompatible with the inferred type of the variable. This PR solves the problem by avoiding two separate assignments to astrom where possible, or using a different variable name for the first astrom where two assignments are needed.

The full difference between `erfa/tests/test_ufunc.py` in current `main` and in this PR
106,107c106
<     astrom = erfa_ufunc.apcg(date1, date2, ebpv, ehp)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apcg(date1, date2, ebpv, ehp).view(np.recarray)
134,135c133
<     astrom = erfa_ufunc.apcg13(date1, date2)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apcg13(date1, date2).view(np.recarray)
176,177c174
<     astrom = erfa_ufunc.apci(date1, date2, ebpv, ehp, x, y, s)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apci(date1, date2, ebpv, ehp, x, y, s).view(np.recarray)
204,205c201,202
<     astrom, eo = erfa_ufunc.apci13(date1, date2)
<     astrom = astrom.view(np.recarray)
---
>     astrom_raw, eo = erfa_ufunc.apci13(date1, date2)
>     astrom = astrom_raw.view(np.recarray)
256,257c253
<     astrom = erfa_ufunc.apco(date1, date2, ebpv, ehp, x, y, s, theta, elong, phi, hm, xp, yp, sp, refa, refb)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apco(date1, date2, ebpv, ehp, x, y, s, theta, elong, phi, hm, xp, yp, sp, refa, refb).view(np.recarray)
303,304c299,300
<     astrom, eo, j = erfa_ufunc.apco13(utc1, utc2, dut1, elong, phi, hm, xp, yp, phpa, tc, rh, wl)
<     astrom = astrom.view(np.recarray)
---
>     astrom_raw, eo, j = erfa_ufunc.apco13(utc1, utc2, dut1, elong, phi, hm, xp, yp, phpa, tc, rh, wl)
>     astrom = astrom_raw.view(np.recarray)
360,361c356
<     astrom = erfa_ufunc.apcs(date1, date2, pv, ebpv, ehp)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apcs(date1, date2, pv, ebpv, ehp).view(np.recarray)
395,396c390
<     astrom = erfa_ufunc.apcs13(date1, date2, pv)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apcs13(date1, date2, pv).view(np.recarray)
424,425c418
<     astrom = erfa_ufunc.aper(theta, astrom)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.aper(theta, astrom).view(np.recarray)
434,435c427
<     astrom = erfa_ufunc.aper13(ut11, ut12, astrom)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.aper13(ut11, ut12, astrom).view(np.recarray)
449,450c441
<     astrom = erfa_ufunc.apio(sp, theta, elong, phi, hm, xp, yp, refa, refb)
<     astrom = astrom.view(np.recarray)
---
>     astrom = erfa_ufunc.apio(sp, theta, elong, phi, hm, xp, yp, refa, refb).view(np.recarray)
475,476c466,467
<     astrom, j = erfa_ufunc.apio13(utc1, utc2, dut1, elong, phi, hm, xp, yp, phpa, tc, rh, wl)
<     astrom = astrom.view(np.recarray)
---
>     astrom_raw, j = erfa_ufunc.apio13(utc1, utc2, dut1, elong, phi, hm, xp, yp, phpa, tc, rh, wl)
>     astrom = astrom_raw.view(np.recarray)

One of the things `erfa_generator` does is it translates ERFA tests from
C to Python. Several C tests access attributes of structs, and the
corresponding Python tests use `np.recarray` views so that they too
could access named attributes, which simplifies the translation.
Previously this was often done by storing the output of a `pyerfa` ufunc
in a variable and then assigning the output of a `.view()` method call
to the same variable. Type checkers can report such re-assignments as
errors if the values involved have incompatible types. In many cases it
is simple to avoid the intermediate variables entirely. In the few cases
where the intermediate variable cannot be (easily) avoided the type
checking problems can be prevented by giving the intermediate variable a
different name.
@avalentino
avalentino merged commit 1bd2176 into liberfa:main Oct 9, 2026
22 checks passed
@eerovaher
eerovaher deleted the astrom-vars-in-tests branch October 9, 2026 16:22
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