Skip to content

Python bindings#4

Merged
mmaker merged 32 commits intomainfrom
python-bindings
Sep 29, 2025
Merged

Python bindings#4
mmaker merged 32 commits intomainfrom
python-bindings

Conversation

@LDiazN
Copy link
Copy Markdown
Contributor

@LDiazN LDiazN commented Sep 18, 2025

This PR will introduce Python bindings for the ooniauth-core crate

The bindings are implemented in the ooniauh-py crate, using pyo3 and maturin to make the bindings

My main goals with this crate were:

  • A thin abstraction over ooniauth-core, so that the usage of the library is driven by that crate
  • Idiomatic and well-typed Python to make it easier to work with from the Python side
  • making a testing UserState class for testing with Python
  • Being able o reproduce the basic usage example from python

In the following repo I provide a minimal fastapi project to show how the library will be used in Python: https://github.com/LDiazN/anoncred/blob/main/tests/test_creds.py (Still WIP)

Note: the fastapi project assumes that you have the library installed in your virtual environment, this is due to how the maturin workflow works (it assumes that you are running in a virtualenv, and during development it installs the preview library in that virtualenv)

Feedback

The intention with this first iteration is to lay the foundational work to direct the development of the bindings library, so all the feedback is welcome

That said, here are some things that I think might need a bit more thought:

  • bytes interface? : If you look at the example usage here and here the Python client still needs to perform to_bin and to_str conversions for transfers. This is because we're using json as encoding, so we could change the encoding to keep the bytes interface if we really need it, but if there's no reason to keep it, I would say we change it to a string interface (and it's probably more efficient to do those conversions in Rust land anyways)
  • Exception handling: In exceptions.rs you can see the different exception types we have in Python, these are mappings from our errors to Python exceptions. We might want to think a bit more about how we want to represent this errors

closes #6

@LDiazN LDiazN requested a review from mmaker September 18, 2025 14:07
@LDiazN LDiazN self-assigned this Sep 18, 2025
@LDiazN LDiazN requested review from hellais and removed request for mmaker September 18, 2025 14:08
@LDiazN LDiazN marked this pull request as ready for review September 18, 2025 14:26
@@ -0,0 +1,66 @@
# This file is automatically generated by pyo3_stub_gen
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need to include this file in the source code of this package?

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.

I kept this file in version control to have a history of the Python API, in case we need to troubleshoot API changes. Old code that relies on a specific API that we break in a future version.

We can still do the same thing generating the file for every version we want to check but it's easier to browse if it's here

And since we're using an external dependency to generate this file, might be helpful if the dependency changes it's behavior in the future

Do you see any significant drawback of having this file here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

that's ok, just making sure it's not been forgotten

Comment thread Cargo.toml Outdated
thiserror = "2.0.12"

[profile.release]
lto = "fat" No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need this LTO change?

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.

Not really, I was testing this when I was addressing the performance issues but this didn't have any effect on performance

Will remove it 👍

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.

Create Python bindings

2 participants