From 65413def0c7e810d5370962b06ce3e0f927abd28 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:13:35 +0100 Subject: [PATCH 1/8] Added pre-commit. --- .github/workflows/{CI.yml => release.yml} | 17 +++---- .github/workflows/test.yml | 51 +++++++++++++++++++ .pre-commit-config.yaml | 62 +++++++++++++++++++++++ .rustfmt.toml | 1 + python/natsrpy/__init__.py | 6 +-- python/natsrpy/_inner/js/kv.pyi | 2 +- python/natsrpy/js/kv.py | 5 +- python/natsrpy/js/stream.py | 2 +- src/exceptions/py_err.rs | 3 +- src/js/jetstream.rs | 22 ++++---- src/js/stream.rs | 12 ++--- src/lib.rs | 4 +- src/subscription.rs | 8 +-- 13 files changed, 147 insertions(+), 48 deletions(-) rename .github/workflows/{CI.yml => release.yml} (97%) create mode 100644 .github/workflows/test.yml create mode 100644 .pre-commit-config.yaml create mode 100644 .rustfmt.toml diff --git a/.github/workflows/CI.yml b/.github/workflows/release.yml similarity index 97% rename from .github/workflows/CI.yml rename to .github/workflows/release.yml index eb7ca0c..8cde0d3 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/release.yml @@ -1,9 +1,4 @@ -# This file is autogenerated by maturin v1.12.6 -# To update, run -# -# maturin generate-ci github -# -name: CI +name: Release workflow on: release: @@ -37,7 +32,7 @@ jobs: python-version: 3.x - name: Setting correct version run: | - python scripts/bump_version.py "${{ github.ref_name }}" + python scripts/bump_version.py "${{ github.ref_name }}" - name: Build wheels uses: PyO3/maturin-action@v1 with: @@ -73,7 +68,7 @@ jobs: python-version: 3.x - name: Setting correct version run: | - python scripts/bump_version.py "${{ github.ref_name }}" + python scripts/bump_version.py "${{ github.ref_name }}" - name: Build wheels uses: PyO3/maturin-action@v1 with: @@ -112,7 +107,7 @@ jobs: architecture: ${{ matrix.platform.python_arch }} - name: Setting correct version run: | - python scripts/bump_version.py "${{ github.ref_name }}" + python scripts/bump_version.py "${{ github.ref_name }}" - name: Build wheels uses: PyO3/maturin-action@v1 with: @@ -141,7 +136,7 @@ jobs: python-version: 3.x - name: Setting correct version run: | - python scripts/bump_version.py "${{ github.ref_name }}" + python scripts/bump_version.py "${{ github.ref_name }}" - name: Build wheels uses: PyO3/maturin-action@v1 with: @@ -163,7 +158,7 @@ jobs: python-version: 3.x - name: Setting correct version run: | - python scripts/bump_version.py "${{ github.ref_name }}" + python scripts/bump_version.py "${{ github.ref_name }}" - name: Build sdist uses: PyO3/maturin-action@v1 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ca53843 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,51 @@ +name: "Testing package" + +on: + pull_request: + +jobs: + py-lint: + strategy: + matrix: + cmd: + - black + - isort + - ruff + - mypy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.11" + - name: Run lint check + uses: pre-commit/action@v3.0.0 + with: + extra_args: -a ${{ matrix.cmd }} + fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: rustfmt + override: true + - name: Check code format + run: cargo fmt -- --check + + clippy: + permissions: + checks: write + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: clippy + override: true + - uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..c9bc670 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,62 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.1.0 + hooks: + - id: trailing-whitespace + - repo: https://github.com/psf/black + rev: 26.3.0 + hooks: + - id: black + name: python black + pass_filenames: false + always_run: true + args: ["python"] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.19.1 + hooks: + - id: mypy + name: python mypy + always_run: true + pass_filenames: false + args: ["python"] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.5 + hooks: + - id: ruff + name: ruff + pass_filenames: false + always_run: true + args: ["python", "--fix"] + - repo: local + hooks: + - id: fmt + types: + - rust + name: rust fmt + language: system + entry: cargo + pass_filenames: false + args: + - fmt + + - id: clippy + types: + - rust + name: rust clippy + language: system + pass_filenames: false + entry: cargo + args: + - clippy + - --fix + - --allow-dirty + + - id: check + types: + - rust + name: rust cargo check + language: system + entry: cargo + pass_filenames: false + args: + - check diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..6575192 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +use_try_shorthand=true diff --git a/python/natsrpy/__init__.py b/python/natsrpy/__init__.py index 163da45..6dfc866 100644 --- a/python/natsrpy/__init__.py +++ b/python/natsrpy/__init__.py @@ -1,8 +1,4 @@ -from natsrpy._inner import ( - Nats, - Subscription, - Message, -) +from natsrpy._inner import Message, Nats, Subscription __all__ = [ "Nats", diff --git a/python/natsrpy/_inner/js/kv.pyi b/python/natsrpy/_inner/js/kv.pyi index 22c2b4f..684bcfc 100644 --- a/python/natsrpy/_inner/js/kv.pyi +++ b/python/natsrpy/_inner/js/kv.pyi @@ -1,4 +1,4 @@ -from natsrpy._inner.js.stream import StorageType, Source, Placement, Republish +from natsrpy._inner.js.stream import Placement, Republish, Source, StorageType class KVConfig: """ diff --git a/python/natsrpy/js/kv.py b/python/natsrpy/js/kv.py index 85f593a..faabf69 100644 --- a/python/natsrpy/js/kv.py +++ b/python/natsrpy/js/kv.py @@ -1,7 +1,4 @@ -from natsrpy._inner.js.kv import ( - KVConfig, - KeyValue, -) +from natsrpy._inner.js.kv import KeyValue, KVConfig __all__ = [ "KVConfig", diff --git a/python/natsrpy/js/stream.py b/python/natsrpy/js/stream.py index aa2f33a..589c47b 100644 --- a/python/natsrpy/js/stream.py +++ b/python/natsrpy/js/stream.py @@ -1,10 +1,10 @@ from natsrpy._inner.js.stream import ( External, + Placement, Republish, Source, StorageType, SubjectTransform, - Placement, ) __all__ = [ diff --git a/src/exceptions/py_err.rs b/src/exceptions/py_err.rs index 264a238..30482a8 100644 --- a/src/exceptions/py_err.rs +++ b/src/exceptions/py_err.rs @@ -1,5 +1,4 @@ -use pyo3::create_exception; -use pyo3::pymodule; +use pyo3::{create_exception, pymodule}; create_exception!( natsrpy.exceptions, diff --git a/src/js/jetstream.rs b/src/js/jetstream.rs index b65bd52..afb8eb0 100644 --- a/src/js/jetstream.rs +++ b/src/js/jetstream.rs @@ -1,17 +1,17 @@ -use std::ops::Deref; -use std::sync::Arc; +use std::{ops::Deref, sync::Arc}; -use async_nats::Subject; -use async_nats::client::traits::Publisher; -use async_nats::connection::State; -use pyo3::types::{PyBytesMethods, PyDict}; -use pyo3::{Bound, PyAny, Python, pyclass, pymethods, types::PyBytes}; +use async_nats::{Subject, client::traits::Publisher, connection::State}; +use pyo3::{ + Bound, PyAny, Python, pyclass, pymethods, + types::{PyBytes, PyBytesMethods, PyDict}, +}; use tokio::sync::RwLock; -use crate::exceptions::rust_err::NatsrpyError; -use crate::js::kv::{KVConfig, KeyValue}; -use crate::utils::headers::NatsrpyHeadermapExt; -use crate::{exceptions::rust_err::NatsrpyResult, utils::natsrpy_future}; +use crate::{ + exceptions::rust_err::{NatsrpyError, NatsrpyResult}, + js::kv::{KVConfig, KeyValue}, + utils::{headers::NatsrpyHeadermapExt, natsrpy_future}, +}; #[pyclass] pub struct JetStream { diff --git a/src/js/stream.rs b/src/js/stream.rs index cecda21..53d7e6e 100644 --- a/src/js/stream.rs +++ b/src/js/stream.rs @@ -1,10 +1,7 @@ use std::ops::Deref; -use crate::exceptions::rust_err::NatsrpyError; -use crate::exceptions::rust_err::NatsrpyResult; -use pyo3::Bound; -use pyo3::pyclass; -use pyo3::pymethods; +use crate::exceptions::rust_err::{NatsrpyError, NatsrpyResult}; +use pyo3::{Bound, pyclass, pymethods}; #[pyclass(from_py_object)] #[derive(Clone, Copy, Default)] @@ -52,7 +49,7 @@ pub struct External { impl External { #[new] #[pyo3(signature = (api_prefix, delivery_prefix=None))] - #[must_use] + #[must_use] pub const fn __new__(api_prefix: String, delivery_prefix: Option) -> Self { Self { api_prefix, @@ -114,7 +111,6 @@ impl TryFrom for async_nats::jetstream::stream::Source { domain: value.domain.clone(), subject_transforms: value .subject_transforms - .into_iter() .map(std::convert::Into::into) .collect(), @@ -169,7 +165,7 @@ pub struct Placement { impl Placement { #[new] #[pyo3(signature=(cluster=None, tags=None))] - #[must_use] + #[must_use] pub fn __new__(cluster: Option, tags: Option>) -> Self { Self { cluster, diff --git a/src/lib.rs b/src/lib.rs index f9d35f3..aca93a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,13 +9,13 @@ #![ allow( // I don't care about this. - clippy::module_name_repetitions, + clippy::module_name_repetitions, // Yo, the hell you should put // it in docs, if signature is clear as sky. clippy::missing_errors_doc, // Because pythonic way is // to have many args with defaults. - clippy::too_many_arguments + clippy::too_many_arguments )] pub mod exceptions; pub mod js; diff --git a/src/subscription.rs b/src/subscription.rs index 4ba35fb..591690c 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -5,8 +5,10 @@ use std::{sync::Arc, time::Duration}; use pyo3::{Bound, PyAny, PyRef, Python, pyclass, pymethods}; use tokio::sync::Mutex; -use crate::exceptions::rust_err::NatsrpyError; -use crate::{exceptions::rust_err::NatsrpyResult, utils::natsrpy_future}; +use crate::{ + exceptions::rust_err::{NatsrpyError, NatsrpyResult}, + utils::natsrpy_future, +}; #[pyclass] pub struct Subscription { @@ -14,7 +16,7 @@ pub struct Subscription { } impl Subscription { - #[must_use] + #[must_use] pub fn new(sub: async_nats::Subscriber) -> Self { Self { inner: Some(Arc::new(Mutex::new(sub))), From 310a1a08ad0f0d448a790d6c912bbaacb9cb2782 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:17:47 +0100 Subject: [PATCH 2/8] Fixed tests CI. --- .github/workflows/test.yml | 1 - .pre-commit-config.yaml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca53843..7109060 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,6 @@ jobs: matrix: cmd: - black - - isort - ruff - mypy runs-on: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c9bc670..e5cfcf7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.1.0 + rev: v6.0.0 hooks: - id: trailing-whitespace - repo: https://github.com/psf/black From d66b80de415b2f67ed46afe2db57d9fbab3fa5e9 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:18:53 +0100 Subject: [PATCH 3/8] Updated ruff check --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e5cfcf7..9268f63 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: name: ruff pass_filenames: false always_run: true - args: ["python", "--fix"] + args: ["check", "python", "--fix"] - repo: local hooks: - id: fmt From 6bc124b22904b00d427d0a87fe6224c69255b413 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:20:01 +0100 Subject: [PATCH 4/8] Fixed pre-commit ruff. --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9268f63..e5cfcf7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: name: ruff pass_filenames: false always_run: true - args: ["check", "python", "--fix"] + args: ["python", "--fix"] - repo: local hooks: - id: fmt From 2793780a18a61956534769c29813b6d1ed3917f2 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:26:16 +0100 Subject: [PATCH 5/8] Added clippy lints. --- src/nats_cls.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/nats_cls.rs b/src/nats_cls.rs index 8ab8ae7..1170b39 100644 --- a/src/nats_cls.rs +++ b/src/nats_cls.rs @@ -168,18 +168,18 @@ impl NatsCls { .map(async_nats::HeaderMap::from_pydict) .transpose()?; Ok(natsrpy_future(py, async move { - if let Some(session) = session.read().await.as_ref() { - let request = async_nats::Request { - payload: data, - headers: headermap, - inbox, - timeout: timeout.map(|t| Some(std::time::Duration::from_secs_f32(t))), - }; - session.send_request(subject, request).await?; - Ok(()) - } else { - Err(NatsrpyError::NotInitialized) - } + let session = session.read().await; + let Some(session) = session.as_ref() else { + return Err(NatsrpyError::NotInitialized); + }; + let request = async_nats::Request { + payload: data, + headers: headermap, + inbox, + timeout: timeout.map(|t| Some(std::time::Duration::from_secs_f32(t))), + }; + session.send_request(subject, request).await?; + Ok(()) })?) } From 9f211f12c690d0a2c9fe726f9451877cf3208d65 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:31:59 +0100 Subject: [PATCH 6/8] Updated CI for strictier checks. --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7109060..ce8cc3d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,6 +45,6 @@ jobs: toolchain: stable components: clippy override: true - - uses: actions-rs/clippy-check@v1 + - uses: auguwu/clippy-action@1.4.0 with: - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{secrets.GITHUB_TOKEN}} From 7924c5ed3ca6dac6146c29014eb6c53227ce0615 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:33:07 +0100 Subject: [PATCH 7/8] Updated CI again. --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ce8cc3d..9e4c607 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,3 +48,4 @@ jobs: - uses: auguwu/clippy-action@1.4.0 with: token: ${{secrets.GITHUB_TOKEN}} + deny: warnings From 8ccee4aa0cb49b4793044c1c6f01b38ac6d37885 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 12 Mar 2026 02:37:20 +0100 Subject: [PATCH 8/8] Fixed clippy. --- src/nats_cls.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/nats_cls.rs b/src/nats_cls.rs index 1170b39..8ab8ae7 100644 --- a/src/nats_cls.rs +++ b/src/nats_cls.rs @@ -168,18 +168,18 @@ impl NatsCls { .map(async_nats::HeaderMap::from_pydict) .transpose()?; Ok(natsrpy_future(py, async move { - let session = session.read().await; - let Some(session) = session.as_ref() else { - return Err(NatsrpyError::NotInitialized); - }; - let request = async_nats::Request { - payload: data, - headers: headermap, - inbox, - timeout: timeout.map(|t| Some(std::time::Duration::from_secs_f32(t))), - }; - session.send_request(subject, request).await?; - Ok(()) + if let Some(session) = session.read().await.as_ref() { + let request = async_nats::Request { + payload: data, + headers: headermap, + inbox, + timeout: timeout.map(|t| Some(std::time::Duration::from_secs_f32(t))), + }; + session.send_request(subject, request).await?; + Ok(()) + } else { + Err(NatsrpyError::NotInitialized) + } })?) }