Skip to content

Make typing on UniqueOpts.by_state true JobState enums instead of strings #32

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

Merged
merged 1 commit into from
Jul 7, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- `UniqueOpts.by_state` now has the stronger type of `list[JobState]` (the enum) instead of `list[str]`. [PR #32](https://github.com/riverqueue/riverqueue-python/pull/32).

## [0.6.1] - 2024-07-06

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion src/riverqueue/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Tuple,
List,
Callable,
cast,
runtime_checkable,
)

Expand Down Expand Up @@ -644,7 +645,7 @@ def _build_unique_get_params_and_lock_key(
if unique_opts.by_state:
any_unique_opts = True
get_params.by_state = True
get_params.state = unique_opts.by_state
get_params.state = cast(list[str], unique_opts.by_state)
lock_str += f"&state={','.join(unique_opts.by_state)}"
else:
get_params.state = UNIQUE_STATES_DEFAULT
Expand Down
4 changes: 3 additions & 1 deletion src/riverqueue/insert_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from datetime import datetime
from typing import Any, Literal, Optional

from riverqueue.job import JobState


@dataclass
class InsertOpts:
Expand Down Expand Up @@ -119,7 +121,7 @@ class UniqueOpts:
enabled, uniqueness will be enforced for a kind across all queues.
"""

by_state: Optional[list[str]] = None
by_state: Optional[list[JobState]] = None
"""
Indicates that uniqueness should be enforced across any of the states in
the given set. For example, if the given states were `(scheduled,
Expand Down
4 changes: 2 additions & 2 deletions tests/driver/riversqlalchemy/sqlalchemy_driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async def test_insert_with_unique_opts_by_queue(self, client, simple_args):
@pytest.mark.asyncio
async def test_insert_with_unique_opts_by_state(self, client, simple_args):
insert_opts = InsertOpts(
unique_opts=UniqueOpts(by_state=["available", "running"])
unique_opts=UniqueOpts(by_state=[JobState.AVAILABLE, JobState.RUNNING])
)
insert_res = await client.insert(simple_args, insert_opts=insert_opts)
assert insert_res.job
Expand Down Expand Up @@ -299,7 +299,7 @@ def test_insert_with_unique_opts_by_queue(self, client, simple_args):

def test_insert_with_unique_opts_by_state(self, client, simple_args):
insert_opts = InsertOpts(
unique_opts=UniqueOpts(by_state=["available", "running"])
unique_opts=UniqueOpts(by_state=[JobState.AVAILABLE, JobState.RUNNING])
)
insert_res = client.insert(simple_args, insert_opts=insert_opts)
assert insert_res.job
Expand Down