Commit Graph

333 Commits

Author SHA1 Message Date
Martijn Pieters
eda2a437a4 More type hint compatibility updates 2023-05-11 12:57:27 +01:00
Martijn Pieters
a892a21b92 Run mypy during CI checks 2023-05-11 12:34:08 +01:00
Martijn Pieters
941cd044c7 Full mypy --strict type checking pass 2023-05-11 12:34:08 +01:00
Martijn Pieters
e92604802e Remove non-existing argument to unicorn.run 2023-05-11 12:31:07 +01:00
Martijn Pieters
94a02733c8 Run tests on all supported Python versions. 2023-05-11 12:31:07 +01:00
Martijn Pieters
013be85f97 Typing cleanup
- Compatibility with older Python versions
  - use `Optional` and `Union` instead of `... | None` and `a | b`
  - use `typing_extensions.Protocol` instead of `typing.Protocol`
  - use `typing.Dict`, `typing.List`, etc. instead of the concrete types.

- Fix backend `.get()` annotations; not all were marked as `Optional[str]`
- Don't return anything from `Backend.set()` methods.
- The `Coder.decode_as_type()` type parameter must be a type to be
  compatible with `ModelField(..., type_=...)`.
- Clean up `Optional[]` use, remove where it is not needed.
- Clean up variable use in decorator, keeping the raw cached value
  separate from the return value from the wrapped endpoint.
- Annotate the wrapper as returning either the original type _or_ a
  Response (returning a 304 Not Modified response).
- Clean up small edge-case where `response` could be `None`.
- Correct type annotation on `JsonCoder.decode()` to match `Coder.decode()`.
2023-05-11 12:31:05 +01:00
long2ice
564026e189 Merge pull request #135 from mjpieters/refactor_prefix
Make decorator responsibe for applying the prefix
2023-05-11 09:25:57 +08:00
long2ice
fba7726280 Merge pull request #134 from mjpieters/backend_coder_bytes
Make backends store bytes instead of strings
2023-05-11 09:24:38 +08:00
Martijn Pieters
d9965a45e5 Make decorator responsibe for applying the prefix
The key builder should not have to fetch the prefix separately, as this
makes creating custom key builders that much harder.
2023-05-10 18:59:59 +01:00
Martijn Pieters
23d439f83a Make backends store bytes instead of strings
This is, for the majority of backends, the native format anyway, and so
we save encoding and decoding when using the PickleCodec or if (in future)
a orjson Coder was to be added.

For the JsonCodec, the only thing that changed is the location where the
JSON data is encoded to bytes and decoded back again to a string.
2023-05-10 17:35:15 +01:00
long2ice
5f2fcf3581 Merge pull request #132 from mjpieters/cache_pydantic_fields
Cache pydantic model fields for faster decoding
2023-05-10 21:21:43 +08:00
Martijn Pieters
7c30402907 Cache pydantic model fields for faster decoding
In `timeit` tests, 10.000 calls to `ModelField()` could take up to half
a second on my Macbook Pro M1, depending on the type annotation used.
Given that the method is called for every cache hit, this can really add
up. The number of different return types for endpoints is very much
finite however, so caching is a definite win here.
2023-05-09 17:25:32 +01:00
long2ice
4d67e0c464 Merge pull request #124 from mjpieters/type_refinement
key_builder type; args and kwargs are always given
2023-05-09 18:16:37 +08:00
long2ice
276c7c725f Merge pull request #125 from mjpieters/update_lock
Refresh poetry lock
2023-05-09 18:16:13 +08:00
long2ice
da9a03ede8 Merge pull request #127 from mjpieters/exported_names
Explicitly list what names are exported
2023-05-09 18:15:57 +08:00
long2ice
eece971f0a Merge pull request #128 from mjpieters/namespaced_injection
Inject dependencies using a namespace
2023-05-09 18:15:32 +08:00
Martijn Pieters
e09ede2e4c Inject dependencies using a namespace
Instead of assuming that the Request and Response injected keyword
arguments can be named `request` and `response`, use namespaced
keyword names starting with a double underscore.

By default the parameter names now start with `__fastapi_cache_` and so
are a) clearly marked as internal, and b) highly unlikely to clash with
existing keyword arguments. The prefix is configurable in the unlikely
event that the names would clash in specific cases.
2023-05-09 11:09:29 +01:00
long2ice
2788006b8c Merge pull request #130 from mjpieters/non_get_requests
Fix handling non-GET requests
2023-05-09 18:03:24 +08:00
Martijn Pieters
4cc946eb00 Fix handling non-GET requests
The `request` parameter being passed in was just a hold-over from an
earlier refactoring. Added tests to ensure that this edge case keeps
working.
2023-05-09 10:58:37 +01:00
long2ice
1ded0ed50e Merge pull request #131 from mjpieters/json_decoder_pydantic
Decode cache data to the correct endpoint type
2023-05-09 10:16:17 +08:00
Martijn Pieters
f78a599bbc Decode cache data to the correct endpoint type
Use the return annotation to decode cached data to the correct type.
This follows the same logic FastAPI uses to JSON request bodies.

For the PickleCoder, this is a no-op as pickle already stores type
information in the serialised data.
2023-05-08 16:55:05 +01:00
Martijn Pieters
b1dc05a89a key_builder type; args and kwargs are always given
These arguments are never set to None so don't need to be optional. They
are always a tuple and a dict but can be empty.
2023-04-28 16:50:30 +01:00
Martijn Pieters
416a4ec850 Explicitly list what names are exported
This signals to automated tools what names usually can be imported from
the package, as otherwise you'd get warnings like `"default_key_builder"
is not exported from module "fastapi_cache"`.
2023-04-28 14:40:31 +01:00
Martijn Pieters
3a3964db1b Refresh poetry lock
The older releases in the lock can cause the CI build to fail with
more recent Poetry versions, see python-poetry/poetry#7611. Updating
the lock should remedy this.
2023-04-28 12:27:52 +01:00
long2ice
550ba76df4 Merge pull request #123 from mjpieters/method_signature
Attach updated endpoint signature to inner
2023-04-28 14:29:06 +08:00
long2ice
b26059b654 Merge pull request #122 from mjpieters/type-hinting
Complete type hints
2023-04-28 14:28:49 +08:00
Martijn Pieters
832650347b Attach updated endpoint signature to inner
Not all endpoints accept a __signature__ attribute, nor should the
cache decorator modify the decorated endpoint. Attach the signature
to the returned inner function instead.

While here, refactor the signature updating code, and extract it to
a separate function.
2023-04-27 18:14:59 +01:00
Martijn Pieters
72c42325ab The backend needs an async redis client with a pipeline method
The Abstract* classes lack the pipeline method so are not sufficient.
2023-04-27 16:33:43 +01:00
Martijn Pieters
6af14be049 Provide annotation for the session attribute 2023-04-27 16:32:07 +01:00
Martijn Pieters
9c966286b4 Use complete type hints with all generic parameters filled
This makes the core fastapi_cache pass all strict type checker tests.
2023-04-27 16:31:42 +01:00
Martijn Pieters
a52f6b1406 Simplify key_builder calling
The keybuilder is either returning a string, or a coroutine or other
awaitable. If the latter, await on the return value to get the string.
2023-04-27 16:29:10 +01:00
Martijn Pieters
255f40117b Define keybuilder protocol
This lets others create key builders that are type checked fully.
2023-04-27 16:26:41 +01:00
Martijn Pieters
d4cd787527 JSONResponse.body is UTF-8 bytes and must be decoded 2023-04-27 16:20:12 +01:00
Martijn Pieters
059793d585 Remove a type: ignore comment
GIve the type checker more information about the converters instead.
2023-04-27 16:19:02 +01:00
Martijn Pieters
0d0fe1f0d0 Mark up the class variables as such
There is never an instance of this class, so these are not instance attributes.
2023-04-27 16:15:06 +01:00
Martijn Pieters
32acafa5e0 Correct type hint: namespace is not optional
The namespace argument is positional and will never be `None` so should
not be marked as Optional. It is always a string, and the default is
to pass in an empty string.
2023-04-27 16:11:59 +01:00
long2ice
ee58f979d4 ci: fix workflows 2023-02-15 10:53:24 +08:00
long2ice
38ddd063c3 test: add httpx for test 2023-02-15 10:49:35 +08:00
long2ice
27acce3160 ci: fix poetry 2023-02-15 10:45:19 +08:00
long2ice
d04be274e9 feat: upgrade deps v0.2.1 2023-02-15 10:43:01 +08:00
long2ice
80563fd6e7 Merge pull request #118 from naoki-jarvisml/var_keyword
Support functions with VAR_KEYWORD parameter
2023-02-15 10:30:12 +08:00
Naoki Shima
98cf8a78a1 adding test coverage 2023-02-15 10:35:41 +09:00
Naoki Shima
01c895dbbb Support functions with VAR_KEYWORD parameter
decorating function with **kwargs parameter with @cache causes ValueError.

ValueError: wrong parameter order: variadic keyword parameter before keyword-only parameter

We need to inject request / response parameters before VAR_KEYWORD parameter.
2023-02-09 15:14:20 +09:00
long2ice
e3b08dda2c Merge pull request #114 from hackjammer/feature/redisCluster
Add RedisCluster Support
2023-02-01 15:33:11 +08:00
long2ice
552a7695e8 Update fastapi_cache/decorator.py
Co-authored-by: mkdir700 <56359329+mkdir700@users.noreply.github.com>
2023-02-01 15:33:04 +08:00
hackjammer
ea1ffcd7b4 Add logging to decorator.py on backend failures 2023-01-17 12:15:53 +00:00
hackjammer
e8193b5c22 enabled redis in cluster mode 2023-01-15 21:54:16 +00:00
hackjammer
ab26fad604 passthrough for any type of backend exception 2023-01-15 17:07:37 +00:00
long2ice
7a89f28b54 Merge pull request #112 from schmocker/main
add cache-control and etag to header of fist response
2023-01-15 12:27:56 +08:00
Tobias Schmocker
334b829a80 Merge branch 'master'
# Conflicts:
#	fastapi_cache/decorator.py
2023-01-14 19:11:42 +01:00