Skip to content

Commit

Permalink
Use typing_extensions.ParamSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
awolverp committed Nov 25, 2024
1 parent 09b6eb5 commit 9c1348e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 4.3.2 - 2024-11-25
### Added
- New dependency is added: `typing_extensions`. We used this library to use `ParamSpec` that makes `cachebox` type-hint
better, and easier than ever for you.

### Changed
- the behaviour of the iteratores changed. Previously, iterators used capacity and length of the cache
to know "is cache have changes?". But now, each cache has a number called "state" that increments
with each change; iterators now uses this "state" number.

## 4.3.1 - 2024-11-18
### Changed
- `__str__` changed to `__repr__`
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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cachebox"
version = "4.3.1"
version = "4.3.2"
edition = "2021"
description = "The fastest memoizing and caching Python library written in Rust"
readme = "README.md"
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ It uses Google's high-performance SwissTable hash map. thanks to [hashbrown](htt
**✨ Low memory usage** \
It has very low memory usage.

**⭐ Zero-Dependecy** \
As we said, `cachebox` written in Rust so you don't have to install any other dependecies.

**🧶 Thread-safe** \
It's completely thread-safe and uses locks to prevent problems.

Expand Down
18 changes: 11 additions & 7 deletions cachebox/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._cachebox import BaseCacheImpl, FIFOCache
from typing_extensions import ParamSpec
from collections import namedtuple
import functools
import inspect
Expand Down Expand Up @@ -187,7 +188,10 @@ def make_typed_key(args: tuple, kwds: dict):
EVENT_HIT = 2


class _cached_wrapper(typing.Generic[VT]):
PS = ParamSpec("PS")


class _cached_wrapper(typing.Generic[VT, PS]):
def __init__(
self,
cache: BaseCacheImpl[typing.Any, VT],
Expand Down Expand Up @@ -233,7 +237,7 @@ def __get__(self, instance, *args):
self.instance = instance
return self

def __call__(self, *args, **kwds) -> VT:
def __call__(self, *args: PS.args, **kwds: PS.kwargs) -> VT:
if self.instance is not _NOT_SETTED:
args = (self.instance, *args)

Expand Down Expand Up @@ -261,8 +265,8 @@ def __call__(self, *args, **kwds) -> VT:
return _copy_if_need(result, force=self.always_copy)


class _async_cached_wrapper(_cached_wrapper[VT]):
async def __call__(self, *args, **kwds) -> VT:
class _async_cached_wrapper(_cached_wrapper[VT, PS]):
async def __call__(self, *args: PS.args, **kwds: PS.kwargs) -> VT:
if self.instance is not _NOT_SETTED:
args = (self.instance, *args)

Expand Down Expand Up @@ -348,11 +352,11 @@ def sum_as_string(a, b):

@typing.overload
def decorator(
func: typing.Callable[..., typing.Awaitable[VT]],
) -> _async_cached_wrapper[VT]: ...
func: typing.Callable[PS, typing.Awaitable[VT]],
) -> _async_cached_wrapper[VT, PS]: ...

@typing.overload
def decorator(func: typing.Callable[..., VT]) -> _cached_wrapper[VT]: ...
def decorator(func: typing.Callable[PS, VT]) -> _cached_wrapper[VT, PS]: ...

def decorator(func):
if inspect.iscoroutinefunction(func):
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dynamic = [
'version'
]

dependencies = [
'typing-extensions>=4.6.2',
]

[project.urls]
Homepage = 'https://github.com/awolverp/cachebox'

Expand Down

0 comments on commit 9c1348e

Please sign in to comment.