Skip to content

Add a type parameter for ufunc ntypes property - #351

Merged
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:de-hardcode-ufunc-ntypes-annotation
Oct 7, 2026
Merged

avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:de-hardcode-ufunc-ntypes-annotation

Conversation

@eerovaher

Copy link
Copy Markdown
Collaborator

On current main the template for the generic ufunc type-check-only subtypes declares the return type of the ntypes property to be Literal[0] unconditionally. At runtime the value can be 0 or 1 depending on which template is used for creating the ufunc:

NULL, NULL, NULL, 0,
funcs_$pyname, data, types_$pyname, 1,

The code that chooses the template is

pyerfa/erfa_generator.py

Lines 497 to 505 in 343b9c8

if self.user_dtype:
placeholders["user_dtype"] = self.user_dtype
placeholders["register_dtypes"] = "\n".join(
f"dtypes[{i}] = {arg.dtype};"
for i, arg in enumerate(self.py_args + self.ufunc_return)
)
file = "define_ufunc_user_dtype.templ"
else:
file = "define_ufunc.templ"

so the ntypes return type should be a type parameter likewise determined by the user_dtype property of the corresponding Function in erfa_generator.

This change will allow using the ufunc type annotation template for ufuncs with ntypes=1.

Previously the template for the generic ufunc type-check-only subtypes
declared the return type of the `ntypes` property to be `Literal[0]`
unconditionally, but at runtime the `ntypes` of a `pyerfa` ufunc can
also return `1`. The return type should therefore not be hardcoded but
instead specified by a type parameter.
@avalentino
avalentino merged commit c21c98e into liberfa:main Oct 7, 2026
22 checks passed
@eerovaher
eerovaher deleted the de-hardcode-ufunc-ntypes-annotation branch October 7, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants