From 0b9bd99fa96de73ebad9f01b7f1c06e11091aabe Mon Sep 17 00:00:00 2001 From: David Irvine Date: Thu, 7 Nov 2024 20:55:15 +0000 Subject: [PATCH] fix: clippy errors --- .cursorrules | 9 + .github/workflows/python-publish-node.yml | 190 ++++++++++++++++++++++ autonomi/src/python.rs | 73 ++++----- 3 files changed, 232 insertions(+), 40 deletions(-) create mode 100644 .cursorrules create mode 100644 .github/workflows/python-publish-node.yml diff --git a/.cursorrules b/.cursorrules new file mode 100644 index 0000000000..8bf17cd8ee --- /dev/null +++ b/.cursorrules @@ -0,0 +1,9 @@ +You are an AI assistant specialized in Python and Rust development. + +For python + +Your approach emphasizes:Clear project structure with separate directories for source code, tests, docs, and config.Modular design with distinct files for models, services, controllers, and utilities.Configuration management using environment variables.Robust error handling and logging, including context capture.Comprehensive testing with pytest.Detailed documentation using docstrings and README files.Dependency management via https://github.com/astral-sh/uv and virtual environments.Code style consistency using Ruff.CI/CD implementation with GitHub Actions or GitLab CI.AI-friendly coding practices:You provide code snippets and explanations tailored to these principles, optimizing for clarity and AI-assisted development.Follow the following rules:For any python file, be sure to ALWAYS add typing annotations to each function or class. Be sure to include return types when necessary. Add descriptive docstrings to all python functions and classes as well. Please use pep257 convention for python. Update existing docstrings if need be.Make sure you keep any comments that exist in a file.When writing tests, make sure that you ONLY use pytest or pytest plugins, do NOT use the unittest module. All tests should have typing annotations as well. All tests should be in ./tests. Be sure to create all necessary files and folders. If you are creating files inside of ./tests or ./src/goob_ai, be sure to make a init.py file if one does not exist.All tests should be fully annotated and should contain docstrings. Be sure to import the following if TYPE_CHECKING:from _pytest.capture import CaptureFixturefrom _pytest.fixtures import FixtureRequestfrom _pytest.logging import LogCaptureFixturefrom _pytest.monkeypatch import MonkeyPatchfrom pytest_mock.plugin import MockerFixture + +For Rust + +Please do not use unwraps or panics. Please ensure all methods are fully tested and annotated. \ No newline at end of file diff --git a/.github/workflows/python-publish-node.yml b/.github/workflows/python-publish-node.yml new file mode 100644 index 0000000000..e0c255a872 --- /dev/null +++ b/.github/workflows/python-publish-node.yml @@ -0,0 +1,190 @@ +name: Build and Publish Python Package + +on: + push: + tags: + - 'v*' + +permissions: + id-token: write + contents: read + +jobs: + macos: + runs-on: macos-latest + permissions: + id-token: write + contents: read + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + target: [x86_64, aarch64] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - name: Create Python module structure + run: | + mkdir -p sn_node/python/autonomi_node + cat > sn_node/python/autonomi_node/__init__.py << EOL + from ._autonomi import * + __version__ = "${{ github.ref_name }}" + EOL + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist + sccache: 'true' + working-directory: ./sn_node + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: sn_node/dist/*.whl + if-no-files-found: error + + windows: + runs-on: windows-latest + permissions: + id-token: write + contents: read + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + target: [x64] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + architecture: ${{ matrix.target }} + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - name: Create Python module structure + shell: cmd + run: | + mkdir sn_node\python\autonomi_client + echo from ._autonomi import * > autonomi\python\autonomi_node\__init__.py + echo __version__ = "0.2.33" >> autonomi\python\autonomi_node\__init__.py + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + args: --release --out dist + sccache: 'true' + working-directory: ./sn_node + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: sn_node/dist/*.whl + if-no-files-found: error + + linux: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + target: [x86_64] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + target: x86_64-unknown-linux-gnu + - name: Install dependencies + run: | + python -m pip install --user cffi + python -m pip install --user patchelf + rustup component add rustfmt + - name: Create Python module structure + run: | + mkdir -p sn_node/python/autonomi_sn_node + cat > sn_node/python/autonomi_node/__init__.py << EOL + from ._autonomi import * + __version__ = "0.2.33" + EOL + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + manylinux: auto + args: --release --out dist + sccache: 'true' + working-directory: ./sn_node + before-script-linux: | + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y + source $HOME/.cargo/env + rustup component add rustfmt + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: sn_node/dist/*.whl + if-no-files-found: error + + sdist: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - uses: actions/checkout@v4 + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - name: Create Python module structure + run: | + mkdir -p sn_node/python/autonomi_node + cat > sn_node/python/autonomi_node/__init__.py << EOL + from ._autonomi import * + __version__ = "0.2.33" + EOL + - name: Build sdist + uses: PyO3/maturin-action@v1 + with: + command: sdist + args: --out dist + working-directory: ./autonomi + - name: Upload sdist + uses: actions/upload-artifact@v3 + with: + name: wheels + path: autonomi/dist/*.tar.gz + if-no-files-found: error + + release: + name: Release + runs-on: ubuntu-latest + needs: [macos, windows, linux, sdist] + permissions: + id-token: write + contents: read + steps: + - uses: actions/download-artifact@v3 + with: + name: wheels + path: dist + - name: Display structure of downloaded files + run: ls -R dist + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: dist/ + verbose: true + print-hash: true \ No newline at end of file diff --git a/autonomi/src/python.rs b/autonomi/src/python.rs index be8a40b923..86a25f941e 100644 --- a/autonomi/src/python.rs +++ b/autonomi/src/python.rs @@ -21,17 +21,17 @@ pub(crate) struct PyClient { impl PyClient { #[staticmethod] fn connect(peers: Vec) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let peers = peers .into_iter() .map(|addr| addr.parse()) .collect::, _>>() .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Invalid multiaddr: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Invalid multiaddr: {e}")) })?; let client = rt.block_on(RustClient::connect(&peers)).map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to connect: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to connect: {e}")) })?; Ok(Self { inner: client }) @@ -42,68 +42,62 @@ impl PyClient { data: Vec, payment: &PyPaymentOption, ) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let access = rt .block_on( self.inner .private_data_put(Bytes::from(data), payment.inner.clone()), ) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!( - "Failed to put private data: {}", - e - )) + pyo3::exceptions::PyValueError::new_err(format!("Failed to put private data: {e}")) })?; Ok(PyPrivateDataAccess { inner: access }) } fn private_data_get(&self, access: &PyPrivateDataAccess) -> PyResult> { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let data = rt .block_on(self.inner.private_data_get(access.inner.clone())) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!( - "Failed to get private data: {}", - e - )) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get private data: {e}")) })?; Ok(data.to_vec()) } fn data_put(&self, data: Vec, payment: &PyPaymentOption) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let addr = rt .block_on( self.inner .data_put(bytes::Bytes::from(data), payment.inner.clone()), ) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to put data: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to put data: {e}")) })?; Ok(crate::client::address::addr_to_str(addr)) } fn data_get(&self, addr: &str) -> PyResult> { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let addr = crate::client::address::str_to_addr(addr).map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Invalid address: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Invalid address: {e}")) })?; let data = rt.block_on(self.inner.data_get(addr)).map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to get data: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get data: {e}")) })?; Ok(data.to_vec()) } fn vault_cost(&self, key: &PyVaultSecretKey) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let cost = rt .block_on(self.inner.vault_cost(&key.inner)) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to get vault cost: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get vault cost: {e}")) })?; Ok(cost.to_string()) } @@ -115,7 +109,7 @@ impl PyClient { key: &PyVaultSecretKey, content_type: u64, ) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let cost = rt .block_on(self.inner.write_bytes_to_vault( bytes::Bytes::from(data), @@ -124,27 +118,27 @@ impl PyClient { content_type, )) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to write to vault: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to write to vault: {e}")) })?; Ok(cost.to_string()) } fn fetch_and_decrypt_vault(&self, key: &PyVaultSecretKey) -> PyResult<(Vec, u64)> { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let (data, content_type) = rt .block_on(self.inner.fetch_and_decrypt_vault(&key.inner)) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to fetch vault: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to fetch vault: {e}")) })?; Ok((data.to_vec(), content_type)) } fn get_user_data_from_vault(&self, key: &PyVaultSecretKey) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let user_data = rt .block_on(self.inner.get_user_data_from_vault(&key.inner)) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to get user data: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get user data: {e}")) })?; Ok(PyUserData { inner: user_data }) } @@ -155,7 +149,7 @@ impl PyClient { payment: &PyPaymentOption, user_data: &PyUserData, ) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let cost = rt .block_on(self.inner.put_user_data_to_vault( &key.inner, @@ -163,7 +157,7 @@ impl PyClient { user_data.inner.clone(), )) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to put user data: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to put user data: {e}")) })?; Ok(cost.to_string()) } @@ -183,7 +177,7 @@ impl PyWallet { &private_key, ) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Invalid private key: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Invalid private key: {e}")) })?; Ok(Self { inner: wallet }) @@ -194,22 +188,22 @@ impl PyWallet { } fn balance(&self) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let balance = rt .block_on(async { self.inner.balance_of_tokens().await }) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to get balance: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get balance: {e}")) })?; Ok(balance.to_string()) } fn balance_of_gas(&self) -> PyResult { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().expect("Could not start tokio runtime"); let balance = rt .block_on(async { self.inner.balance_of_gas_tokens().await }) .map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Failed to get balance: {}", e)) + pyo3::exceptions::PyValueError::new_err(format!("Failed to get balance: {e}")) })?; Ok(balance.to_string()) @@ -249,7 +243,7 @@ impl PyVaultSecretKey { fn from_hex(hex_str: &str) -> PyResult { VaultSecretKey::from_hex(hex_str) .map(|key| Self { inner: key }) - .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid hex key: {}", e))) + .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid hex key: {e}"))) } fn to_hex(&self) -> String { @@ -290,7 +284,7 @@ impl PyUserData { self.inner .file_archives .iter() - .map(|(addr, name)| (format!("{:x}", addr), name.clone())) + .map(|(addr, name)| (format!("{addr:x}"), name.clone())) .collect() } @@ -315,7 +309,7 @@ impl PyPrivateDataAccess { fn from_hex(hex: &str) -> PyResult { PrivateDataAccess::from_hex(hex) .map(|access| Self { inner: access }) - .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid hex: {}", e))) + .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid hex: {e}"))) } fn to_hex(&self) -> String { @@ -329,12 +323,11 @@ impl PyPrivateDataAccess { #[pyfunction] fn encrypt(data: Vec) -> PyResult<(Vec, Vec>)> { - let (data_map, chunks) = self_encryption::encrypt(Bytes::from(data)).map_err(|e| { - pyo3::exceptions::PyValueError::new_err(format!("Encryption failed: {}", e)) - })?; + let (data_map, chunks) = self_encryption::encrypt(Bytes::from(data)) + .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Encryption failed: {e}")))?; let data_map_bytes = rmp_serde::to_vec(&data_map) - .map_err(|e| PyValueError::new_err(format!("Failed to serialize data map: {}", e)))?; + .map_err(|e| PyValueError::new_err(format!("Failed to serialize data map: {e}")))?; let chunks_bytes: Vec> = chunks .into_iter()