Skip to content

ENH: Make PyFortranObject a heap type#31598

Open
da-woods wants to merge 3 commits into
numpy:mainfrom
da-woods:f2py-heap-type
Open

ENH: Make PyFortranObject a heap type#31598
da-woods wants to merge 3 commits into
numpy:mainfrom
da-woods:f2py-heap-type

Conversation

@da-woods

@da-woods da-woods commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

PR summary

... and also adapt it to work as an opaque object.

The main reason for this is to support the Limited API. I don't think there's much value in keeping it as a static type outside the Limited API - it isn't possible to inherit from it so there shouldn't be many user-observable differences.

I'm treating "fortranobject.h" as semi-public. It'll mainly be consumed by f2py modules but presumably people could manually link to it and use it. With that in mind I tried to keep PyFortran_Type working via a macro.

Additionally, I spotted that it really should be a GC type (because it contains a dictionary with arbitrary contents) and so added that as well.

AI Disclosure

No AI usage

... and also adapt it to work as an opaque object.

The main reason for this is to support the Limited API.
I don't think there's much value in keeping it as a static
type outside the Limited API - it isn't possible to inherit
from it so there shouldn't be many user-observable differences.

I'm treating "fortranobject.h" as semi-public. It'll mainly
be consumed by f2py modules but presumably people could
manually link to it and use it. With that in mind I tried
to keep `PyFortran_Type` working via a macro.

Additionally, I spotted that it really should be a GC type
(because it contains a dictionary with arbitrary contents) and
so added that as well.
};

static PyType_Spec fortran_spec = {
.name = "<f2py internal>.fortran",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The representation of the type name is a small observable change (although I don't think an important one).

If I just leave it as "fortran" then there's a deprecation warning on import "builtin type %.200s has no __module__ attribute". Setting it to anything else means it's observable if you do print(type(some_fortran_object)).

The solution involves either a custom metaclass as a base type, or a custom class as a descriptor for __module__. We did the metaclass in Cython because the module name was a bit important for pickle-ability, but I don't believe that's a concern here.

So my view is that this doesn't matter, but it is an observable change.

@da-woods da-woods changed the title Make PyFortranObject a heap type ENH: Make PyFortranObject a heap type Jun 10, 2026
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.

1 participant