Commit Graph

30 Commits

Author SHA1 Message Date
John Lyu
19c4d0271a fix latest bug 2024-05-09 14:09:42 +08:00
Martijn Pieters
50e3f91a87 Add flake8-bandit linting (#156)
The linter has been used in the past, so most assertions for these were
already there but needed to be updated to use `noqa: S` instead of
`nosec: B` annotations.
2023-05-16 12:11:10 +00:00
Martijn Pieters
f7db5cceb1 Add pyupgrade linter
This ensures we use the best syntax for the minimal Python version used.
The linter found one issue, fixed with the auto-fixer.
2023-05-16 13:00:18 +01:00
Martijn Pieters
1d9e126037 Switch to ruff to handle linting and formatting
Ruff handles black, flake8 and isort in one package, and is way faster.
The isort rules had not been enforced, so this commit includes a lot
of import resorting changes.

I switched to flake8-bugbear and the standard black-compatible line
length of 80 + 10% (so max 88 characters), so some line reflowing is
included too.

Finally, because bugbear rightly points out that `setattr()` is less
performant, I've switched the `__signature__` assigment back to using
a direct assignment with type ignore comment.
2023-05-16 12:18:24 +01:00
Martijn Pieters
0763cd7b95 Add pyright strict type checking 2023-05-11 12:57:28 +01:00
Martijn Pieters
941cd044c7 Full mypy --strict type checking pass 2023-05-11 12:34:08 +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
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
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
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
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
Ivan Moiseev
9a39db7a73 Merge branch 'long2ice:main' into fix/piclke-coder 2023-01-11 16:26:05 +03:00
vvanglro
614ee25d0d feat: merge master 2023-01-07 13:46:48 +08:00
long2ice
8f0920d0d7 ci: fix 2022-11-07 16:39:17 +08:00
Ivan Moiseev
cb9fe5c065 fix: PickleCoder and add tests for it. 2022-11-05 13:45:16 +04:00
vvanglro
4cb4afeff0 feat: support cache JSONResponse 2022-11-04 17:31:37 +08:00
Ivan Moiseev
5881bb9122 Merge branch 'main' into feat/type-hints-covering
# Conflicts:
#	fastapi_cache/coder.py
#	fastapi_cache/decorator.py
2022-11-03 15:53:22 +04:00
Ivan Moiseev
e842d6408e feat: make PickleCoder compatible with backends 2022-10-22 21:06:38 +04:00
Ivan Moiseev
4c6abcf786 feat: add more type hints 2022-10-22 20:59:37 +04:00
vvanglro
7e64cd6490 feat: support cache jinja2 template response 2022-09-28 17:37:05 +08:00
long2ice
824e2e145f Replace aioredis with redis-py 2022-06-17 11:01:47 +08:00
long2ice
c3be2eca19 Fix default json coder for datetime. 2021-10-09 16:51:05 +08:00
long2ice
37a2fa85db update CONVERTERS 2021-09-17 10:19:56 +08:00
long2ice
767241be41 - Fix redis cache.
- Encode key builder.
2021-07-26 16:33:22 +08:00
long2ice
7c2007847f Merge pull request #8 from rushilsrivastava/patch-2
Use FastAPI's built in jsonable_encoder
2021-04-30 16:48:07 +08:00
alexandr.yagolovich
eb55b01be9 timezone serializer to coder.JsonEncoder for datetime.datetime instances 2021-04-28 20:44:35 +03:00
Rushil Srivastava
30e5246cf5 Use FastAPI's built in jsonable_encoder 2021-01-11 03:11:35 -08:00
long2ice
dc2ac9cc90 Add default config when init
update JsonEncoder
2020-10-16 16:55:33 +08:00
long2ice
c20bb73f27 first commit 2020-08-26 18:04:57 +08:00