Skip to content

Commit

Permalink
Make typing on UniqueOpts.by_state true JobState enums instead of…
Browse files Browse the repository at this point in the history
… strings (#32)

Make the typing on `UniqueOpts.by_state` strong by making it a list of
`JobState` enums instead of a list of strings. Change usages in the test
suite over to enums instead of strings.
  • Loading branch information
brandur authored Jul 7, 2024
1 parent 622a659 commit efe6997
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
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

0 comments on commit efe6997

Please sign in to comment.