-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds type stubs for python imports (#556)
The primary goals of this PR are 1) to resolve a pylint error when running `from hyperdrive_math_py import HyperdriveState` 2) to enable Python type hints for the `HyperdriveState` object. To achieve this, I had to change the install pattern from using [maturin](https://github.com/PyO3/maturin) to the lower-level [setuptools-rust](https://github.com/PyO3/setuptools-rust). This allows us to specify types & interface stubs so that pylint & pyright can parse type hints. While Maturin technically [claims to support this behavior as well](https://github.com/PyO3/maturin/blob/0dee40510083c03607834c821eea76964140a126/Readme.md#mixed-rustpython-projects), Matt and I were unable to get it to work with the automatic `maturin develop` install process. The new directory structure follows the [recommended example from pyo3](https://github.com/PyO3/setuptools-rust/tree/main/examples/html-py-ever), which looks like this: ``` crates/hyperdrive-math-py/ ├── Cargo.toml ├── MANIFEST.in ├── pyproject.toml ├── python │ └── hyperdrive_math_py │ ├── __init__.py │ ├── hyperdrive_math_py.pyi │ └── types.py ├── setup.py └── src └── lib.rs ``` Most notably is the new `python` folder, which holds type hints, and the `setup.py` file, which specifies how `setuptools` is supposed to install the package via `pip`. The `README.md` file has been updated to reflect the install process, which is simply to enter your preferred python environment and run `pip install crates/hyperdrive-math-py` from the project root.
- Loading branch information
Showing
13 changed files
with
185 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Python test | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
name: python test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# NOTE: This is needed to ensure that hyperdrive-wrappers builds correctly. | ||
- name: Install foundry | ||
uses: foundry-rs/foundry-toolchain@v1 | ||
with: | ||
version: nightly | ||
|
||
- name: Install pip | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.10" | ||
cache: "pip" | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
- run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install --upgrade pytest | ||
- name: Install hyperdrive-math-py | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.10" | ||
cache: "pip" | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
- run: | | ||
python -m pip install crates/hyperdrive-math-py | ||
- name: Run pytest | ||
run: | | ||
python -m pytest python/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include Cargo.toml | ||
recursive-include src * | ||
recursive-include python * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
[build-system] | ||
requires = ["maturin>=1.2,<2.0"] | ||
build-backend = "maturin" | ||
|
||
[project] | ||
name = "hyperdrive_math_lib" | ||
name = "hyperdrive_math_py" | ||
version = "0.1.0" | ||
requires-python = ">=3.7" | ||
classifiers = [ | ||
"Programming Language :: Rust", | ||
"Programming Language :: Python :: Implementation :: CPython", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
] | ||
|
||
[build-system] | ||
requires = ["setuptools", "wheel", "setuptools-rust"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[tool.pylint] | ||
extension-pkg-allow-list="hyperdrive-math-py" | ||
|
||
[tool.maturin] | ||
features = ["pyo3/extension-module"] | ||
[tool.setuptools.packages.find] | ||
where = ["python", "src"] |
2 changes: 2 additions & 0 deletions
2
crates/hyperdrive-math-py/python/hyperdrive_math_py/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"""Python module wrapping the Rust implementation of HyperdriveMath""" | ||
from .hyperdrive_math_py import * |
32 changes: 32 additions & 0 deletions
32
crates/hyperdrive-math-py/python/hyperdrive_math_py/hyperdrive_math_py.pyi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Stubs for hyperdrive math.""" | ||
from __future__ import annotations | ||
|
||
from . import types | ||
|
||
class HyperdriveState: | ||
"""A class representing the hyperdrive contract state.""" | ||
|
||
def __new__( | ||
cls, pool_config: types.PoolConfig, pool_info: types.PoolInfo | ||
) -> HyperdriveState: | ||
"""Create the HyperdriveState instance.""" | ||
def __init__( | ||
self, pool_config: types.PoolConfig, pool_info: types.PoolInfo | ||
) -> None: | ||
"""Initializes the hyperdrive state. | ||
Arguments | ||
--------- | ||
pool_config : PoolConfig | ||
Static configuration for the hyperdrive contract. Set at deploy time. | ||
pool_info : PoolInfo | ||
Current state information of the hyperdrive contract. Includes things like reserve levels and share prices. | ||
""" | ||
def get_spot_price(self) -> str: | ||
"""Gets the spot price of the bond. | ||
Returns | ||
------- | ||
str | ||
The spot price as a string representation of a solidity uint256 value. | ||
""" |
43 changes: 43 additions & 0 deletions
43
crates/hyperdrive-math-py/python/hyperdrive_math_py/types.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Types for the hyperdrive contract.""" | ||
from typing import NamedTuple | ||
|
||
|
||
class Fees(NamedTuple): | ||
"""Protocal Fees.""" | ||
|
||
curve: str | ||
flat: str | ||
governance: str | ||
|
||
|
||
class PoolConfig(NamedTuple): | ||
"""Static configuration for the hyperdrive contract. Set at deploy time.""" | ||
|
||
base_token: str | ||
initial_share_price: str | ||
minimum_share_reserves: str | ||
position_duration: str | ||
checkpoint_duration: str | ||
time_stretch: str | ||
governance: str | ||
fee_collector: str | ||
fees: Fees | ||
oracle_size: str | ||
update_gap: str | ||
|
||
|
||
class PoolInfo(NamedTuple): | ||
"""Current state information of the hyperdrive contract. Includes things like reserve levels and share prices.""" | ||
|
||
share_reserves: str | ||
bond_reserves: str | ||
lp_total_supply: str | ||
share_price: str | ||
longs_outstanding: str | ||
long_average_maturity_time: str | ||
shorts_outstanding: str | ||
short_average_maturity_time: str | ||
short_base_volume: str | ||
withdrawal_shares_ready_to_withdraw: str | ||
withdrawal_shares_proceeds: str | ||
lp_share_price: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""Entry point for installing hyperdrive math python package""" | ||
from setuptools import setup | ||
from setuptools_rust import Binding, RustExtension | ||
|
||
setup( | ||
name="hyperdrive_math_py", | ||
version="0.1.0", | ||
packages=["hyperdrive_math_py"], | ||
package_dir={"": "python"}, | ||
rust_extensions=[ | ||
RustExtension("hyperdrive_math_py.hyperdrive_math_py", binding=Binding.PyO3), | ||
], | ||
# rust extensions are not zip safe, just like C-extensions. | ||
zip_safe=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters