Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FFI type hinting #436

Merged
merged 2 commits into from
Oct 26, 2023
Merged

Add FFI type hinting #436

merged 2 commits into from
Oct 26, 2023

Conversation

JP-Ellis
Copy link
Contributor

📝 Summary

  • Adds _ffi.pyi file annotating the functions exposed by _ffi.cpython-*.so
  • Resolves type issues (including a refactor of StringResult)

As part of this PR, the following related work has been added:

  • Add mypy to the pre-commit type-checking. This will help ensure that typing error are caught more easily.

🚨 Breaking Changes

🔥 Motivation

The Rust FFI is wrapped by the Python CFFI package and a _ffi.cpython-*.so binary library is created. The binary library functions at runtime as a Python module, but obviously cannot be edited to add type hinting.

Instead, it is possible to add a _ffi.pyi file which provides type hinting in a separate file to the core implementation irrespective of whether the implementation being a Python text file, or a binary library.

🔨 Test Plan

Done as part of the pre-commit hook

🔗 Related issues/PRs

This commit adds mypy to the pre-commit hooks. This is executed through
`hatch` as mypy needs to be able to find and parse dependencies in
hatch's virtual environment.

Signed-off-by: JP-Ellis <[email protected]>
@JP-Ellis JP-Ellis added difficulty:easy A simple task appropriate for newcomers to the codebase area:v3 Relating to the pact.v3 module type:chore Part of regular code upkeep labels Oct 26, 2023
@JP-Ellis JP-Ellis self-assigned this Oct 26, 2023
The C extension module `_ffi` is now minimally typed with `pyi` files.
It provides typing for the `ffi` class which holds a number of utility
functions such as `ffi.string`, `ffi.cast`, `ffi.new`, etc.

The `lib` class is also annotated within the `pyi` file, but this merely
means that the type checking does not complain about calls to
`lib.pactffi_*` functions.

As part of this commit, the `StringResult` has been refactored to ensure
that it works better with the type checker.

Signed-off-by: JP-Ellis <[email protected]>
@JP-Ellis JP-Ellis merged commit db6374a into master Oct 26, 2023
19 of 29 checks passed
@JP-Ellis JP-Ellis deleted the feat/ffi-typing branch October 26, 2023 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:v3 Relating to the pact.v3 module difficulty:easy A simple task appropriate for newcomers to the codebase type:chore Part of regular code upkeep
Projects
Status: ✅ Completed
Development

Successfully merging this pull request may close these issues.

Add FFI type hinting
1 participant