Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: "Testing package"

on:
pull_request:
push:

jobs:
py-lint:
Expand All @@ -13,7 +13,7 @@ jobs:
- mypy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v4
with:
Expand All @@ -25,7 +25,7 @@ jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v6
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
Expand All @@ -39,13 +39,45 @@ jobs:
checks: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v6
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
override: true
- uses: auguwu/clippy-action@1.4.0
with:
token: ${{secrets.GITHUB_TOKEN}}
deny: warnings
token: ${{secrets.GITHUB_TOKEN}}
pytest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
override: true
- uses: actions/setup-python@v6
with:
python-version: 3.x
- name: Install uv
uses: astral-sh/setup-uv@v7
- id: prepare-container
name: Prepare docker container
run: docker compose up -d --wait
- id: setup-venv
name: Setup virtualenv
run: python -m venv .venv
- name: Build lib
uses: PyO3/maturin-action@v1
with:
command: dev --uv
sccache: true
- name: Run pytest
run: |
set -e
source .venv/bin/activate
pytest -vv -n auto python/tests
- name: Teardown docker
if: always()
run: docker compose down
14 changes: 12 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ repos:
args:
- fmt

- id: clippy
- id: clippy-fix
types:
- rust
name: rust clippy
name: fixing fixable clippy
language: system
pass_filenames: false
entry: cargo
Expand All @@ -51,6 +51,16 @@ repos:
- --fix
- --allow-dirty

- id: clippy-check
types:
- rust
name: rust clippy
language: system
pass_filenames: false
entry: cargo
args:
- clippy

- id: check
types:
- rust
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
[package]
name = "natsrpy"
version = "0.0.1"
# Don't update version manually. It's set during release workflow.
version = "0.0.0"
edition = "2024"
description = "Python NATS client written in Rust"
repository = "https://github.com/taskiq-python/natsrpy"
license-file = "LICENSE"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "_inner"
crate-type = ["cdylib"]
name = "_inner"

[dependencies]
async-nats = "0.46"
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022-present Pavel Kirilin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[![PyPI](https://img.shields.io/pypi/v/natsrpy?style=for-the-badge)](https://pypi.org/project/scyllapy/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/natsrpy?style=for-the-badge)](https://pypistats.org/packages/scyllapy)

# Nats client

This is a client library for [NATS](https://nats.io) written in rust.

Credits for [Intree](https://intree.com) for supporting this project.


## installation

This package can be installed from pypi:

```bash
pip install natsrpy
```

Or alternatively you ca build it yourself using maturin, and stable rust.

## Development

We use stable rust and pyo3 for writing python extension module.

In order to run the project use maturin:

```bash
# To create .venv folder
uv venv
# To build and install the package in a virtual environment
maturin dev --uv
```

For lints please use `pre-commit`.

```bash
pre-commit run -a
```
15 changes: 15 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
nats:
image: nats:2.12.5-alpine
command: -m 8222 --jetstream
healthcheck:
test:
- CMD
- "sh"
- "-c"
- "wget http://localhost:8222/healthz -q -O - | xargs | grep ok || exit 1"
interval: 5s
timeout: 3s
retries: 20
ports:
- 4222:4222
104 changes: 101 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,117 @@
[project]
name = "natsrpy"
requires-python = ">=3.8"
description = "Nats client library written in rust"
readme = "README.md"
requires-python = ">=3.10"
license-files = ["LICENSE"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Rust",
"Topic :: System :: Networking",
]
dynamic = ["version"]

[[project.authors]]
name = "Pavel Kirilin"
email = "s3riussan@gmail.com"

[dependency-groups]
dev = [
"anyio>=4,<5",
"pytest>=9,<10",
"pytest-xdist>=3,<4",
]

[build-system]
requires = ["maturin>=1.12,<2.0"]
build-backend = "maturin"

[tool.maturin]
bindings = "pyo3"
python-source = "python"
module-name = "natsrpy._inner"
features = ["pyo3/extension-module"]
module-name = "natsrpy._inner"
python-source = "python"

[tool.mypy]
python_version = "3.10"
strict = true
ignore_missing_imports = true
packages = ["natsrpy"]
pretty = true
implicit_reexport = true
allow_untyped_decorators = true
warn_return_any = false

[tool.pytest]
anyio_mode = "auto"

[tool.ruff]
target-version = "py310"
exclude = [".venv/"]
line-length = 88

[tool.ruff.lint]
mccabe = { max-complexity = 10 }
# List of enabled rulsets.
# See https://docs.astral.sh/ruff/rules/ for more information.
select = [
"E", # Error
"F", # Pyflakes
"W", # Pycodestyle
"C90", # McCabe complexity
"I", # Isort
"N", # pep8-naming
"D", # Pydocstyle
"ANN", # Pytype annotations
"S", # Bandit
"B", # Bugbear
"COM", # Commas
"C4", # Comprehensions
"ISC", # Implicit string concat
"PIE", # Unnecessary code
"T20", # Catch prints
"PYI", # validate pyi files
"Q", # Checks for quotes
"RSE", # Checks raise statements
"RET", # Checks return statements
"SLF", # Self checks
"SIM", # Simplificator
"PTH", # Pathlib checks
"ERA", # Checks for commented out code
"PL", # PyLint checks
"RUF", # Specific to Ruff checks
"UP", # Pyupgrade
]
ignore = [
"D105", # Missing docstring in magic method
"D107", # Missing docstring in __init__
"D212", # Multi-line docstring summary should start at the first line
"D401", # First line should be in imperative mood
"D104", # Missing docstring in public package
"D100", # Missing docstring in public module
"ANN401", # typing.Any are disallowed in `**kwargs
"PLR0913", # Too many arguments for function call
"D106", # Missing docstring in public nested class
"PYI021", # Docstrings should not be included in stubs
]

[tool.ruff.lint.per-file-ignores]
"python/tests/*" = [
"S101", # Use of assert detected
"S301", # Use of pickle detected
"D103", # Missing docstring in public function
"SLF001", # Private member accessed
"S311", # Standard pseudo-random generators are not suitable for security/cryptographic purposes
"D101", # Missing docstring in public class
]

[tool.ruff.lint.pydocstyle]
convention = "pep257"
ignore-decorators = ["typing.overload"]

[tool.ruff.lint.pylint]
allow-magic-value-types = ["int", "str", "float"]
2 changes: 1 addition & 1 deletion python/natsrpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from natsrpy._inner import Message, Nats, Subscription

__all__ = [
"Message",
"Nats",
"Subscription",
"Message",
]
31 changes: 17 additions & 14 deletions python/natsrpy/_inner/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
from datetime import timedelta
from typing import Any

from natsrpy._inner.js import JetStream
from natsrpy._inner.message import Message

class Subscription:
def __aiter__(self) -> "Subscription": ...
def __aiter__(self) -> Subscription: ...
async def __anext__(self) -> Message: ...

class Nats:
def __init__(
self,
/,
addrs: list[str] = ["nats://localhost:4222"],
user_and_pass=None,
nkey=None,
token=None,
custom_inbox_prefix=None,
read_buffer_capacity=65535,
sender_capacity=128,
max_reconnects=None,
connection_timeout=5.0,
request_timeout=10.0,
user_and_pass: tuple[str, str] | None = None,
nkey: str | None = None,
token: str | None = None,
custom_inbox_prefix: str | None = None,
read_buffer_capacity: int = 65535,
sender_capacity: int = 128,
max_reconnects: int | None = None,
connection_timeout: timedelta = ...,
request_timeout: timedelta = ...,
) -> None: ...
async def startup(self) -> None: ...
async def shutdown(self) -> None: ...
async def publish(
self,
subject: str,
payload: bytes,
*,
headers: dict[str, str] | None = None,
headers: dict[str, Any] | None = None,
reply: str | None = None,
err_on_disconnect: bool = False,
) -> None: ...
async def request(self, subject: str, payload: bytes) -> None: ...
async def drain(self) -> None: ...
async def flush(self) -> None: ...
async def close(self) -> None: ...
async def subscribe(self, topic: str) -> Subscription: ...
async def subscribe(self, subject: str) -> Subscription: ...
async def jetstream(self) -> JetStream: ...

__all__ = ["Subscription", "Nats", "Message"]
__all__ = ["Message", "Nats", "Subscription"]
Loading
Loading