Skip to content

Correct and complete type annotations in Python bindings#300

Open
dhdaines wants to merge 5 commits intogarvys-org:mainfrom
dhdaines:correct_type_annotations
Open

Correct and complete type annotations in Python bindings#300
dhdaines wants to merge 5 commits intogarvys-org:mainfrom
dhdaines:correct_type_annotations

Conversation

@dhdaines
Copy link
Copy Markdown
Contributor

@dhdaines dhdaines commented Feb 19, 2026

The type annotations in the Python bindings were quite incorrect in a number of places.

In various other places they were simply missing!

Also you should not use from __future__ import annotations, it was a misfeature that is deprecated and will be removed soon, see: https://docs.python.org/3/library/__future__.html#id2

This also revealed what appear to be a number of coding errors. In particular you don't need to use ctypes.pointer in most places where you can just use ctypes.byref. I have removed this except in the one case where it is absolutely necessary (in VectorFst.to_bytes where we have to pass a reference to a pointer, because the pointer itself is getting replaced)

Also the various arguments to Tr.__init__ really should absolutely never be None except in the curious case where one is being created from an existing pointer.

It would probably be better to use Tr.__new__ or create a separate class method to reference an existing underlying pointer (also ... how does this interact with gc?) but I have kept the interface the same for now, just added the proper overload declarations and ensured that the code actually does what they say.

Also MutableTrsIterator isn't a real Python iterator and probably shouldn't define __iter__ and __next__ if it isn't going to behave like one, but again, that would be a different change.

@dhdaines dhdaines changed the title Correct type annotations in Python bindings Correct and complete type annotations in Python bindings Feb 19, 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