Commit Graph

269 Commits

Author SHA1 Message Date
Martijn Pieters
4aa8060faa Merge pull request #163 from long2ice/3777dependabot/pip/main/coverage-7.2.5 2023-05-16 15:07:50 +01:00
dependabot[bot]
189f997228 Bump coverage from 6.5.0 to 7.2.5
Bumps [coverage](https://github.com/nedbat/coveragepy) from 6.5.0 to 7.2.5.
- [Release notes](https://github.com/nedbat/coveragepy/releases)
- [Changelog](https://github.com/nedbat/coveragepy/blob/master/CHANGES.rst)
- [Commits](https://github.com/nedbat/coveragepy/compare/6.5.0...7.2.5)

---
updated-dependencies:
- dependency-name: coverage
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-16 13:46:35 +00:00
Martijn Pieters
3e7deea2ba Correct merge metadata step id, loosen version (#165) 2023-05-16 13:31:55 +00:00
Martijn Pieters
452eaedf5b 🤦 put dependabot wf in correct place (#164) 2023-05-16 13:24:45 +00:00
Martijn Pieters
24e1d8d40b Merge pull request #162 from long2ice/5db3dependabot/pip/main/aiobotocore-2.5.0
Bump aiobotocore from 1.4.2 to 2.5.0
2023-05-16 14:21:41 +01:00
dependabot[bot]
621103c2b8 Bump aiobotocore from 1.4.2 to 2.5.0
Bumps [aiobotocore](https://github.com/aio-libs/aiobotocore) from 1.4.2 to 2.5.0.
- [Release notes](https://github.com/aio-libs/aiobotocore/releases)
- [Changelog](https://github.com/aio-libs/aiobotocore/blob/master/CHANGES.rst)
- [Commits](https://github.com/aio-libs/aiobotocore/compare/1.4.2...2.5.0)

---
updated-dependencies:
- dependency-name: aiobotocore
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-16 13:17:50 +00:00
Martijn Pieters
bb12a91b58 Create codeql scanning workflow (#161)
This helps find any security issues in PRs.
2023-05-16 13:14:58 +00:00
Martijn Pieters
4236b350d9 Configure dependabot and associated workflows (#160)
The auto-merge workflow automatically approves dependabot PRs that
update dependencies or github actions where the semver difference is
at most a minor upgrade. These PRs are then auto-merged once the CI
checks have passed.

The labeller workflow adds / removes the auto-merge label to make
auto-merging more visible in issue lists and to make it possible to
filter such PRs. This workflow is unfortunately not triggered
when the auto-approve workflow enables auto-merging due to GH anti-
recursion rules, so the auto-merge workflow adds the label explicitly.
2023-05-16 13:06:11 +00:00
Martijn Pieters
23bb4e9cd4 Add pytest-style linting (#159)
This does mean we need to tell pyright that the init_cache auto-use
fixture is still being used even though it now has a leading underscore.
2023-05-16 12:45:26 +00:00
Martijn Pieters
230c24a4e0 Give tests and examples separate ruff configs (#158)
This allows more fine-grained lint rule adjustments, and allows
the examples to treat fastapi_cache as a 'third party' module, just
like users of the library would use it.
2023-05-16 12:42:12 +00:00
Martijn Pieters
d938bbe4d2 Add flake8-comprehensions linter (#157) 2023-05-16 12:18:53 +00: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
cbad93c970 Merge pull request #155 from long2ice/pyupgrade
Add pyupgrade linter
2023-05-16 13:07:56 +01: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
daad4cf9a8 Merge pull request #154 from long2ice/simplify_pyproject.toml
Clean up pyproject.toml
2023-05-16 12:59:16 +01:00
Martijn Pieters
d2947d01b5 Clean up pyproject.toml
- remove usused dependency
- use poetry syntax to specify minimal python versions
- simplify path selections in mypy and pyright
- correct pytest section name
2023-05-16 12:47:47 +01:00
Martijn Pieters
637c825dc8 Merge pull request #153 from long2ice/simplify_wf
CI: tox now takes care of extras and groups
2023-05-16 12:35:22 +01:00
Martijn Pieters
1cf352bc9c CI: tox now takes care of extras and groups
The outer Poetry installation can be simplified as long as tox is part
of the dev group.
2023-05-16 12:29:18 +01:00
Martijn Pieters
f314d84d31 Merge pull request #152 from long2ice/ruff
Switch to ruff to handle linting and formatting
2023-05-16 12:24:02 +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
707b4aec95 Merge pull request #151 from long2ice/poetry_core
Use poetry-core as the build system
2023-05-16 11:08:03 +01:00
Martijn Pieters
293a06467a Use poetry-core as the build system
Poetry core is the lighter-weight build system for poetry; this makes
installing the project faster.
2023-05-16 11:03:53 +01:00
Martijn Pieters
7767d2460d Merge pull request #146 from long2ice/tox
Add tox configuration
2023-05-16 10:52:25 +01:00
Martijn Pieters
0e9a8baeb2 Add tox configuration
Tox manages test environments for all supported Python versions, as
well as linting and formatting tools. On GitHub, the test and lint
steps are kept as close as possible to the Makefile equivalents.
2023-05-16 10:49:17 +01:00
Martijn Pieters
2eabc49d24 Merge pull request #150 from long2ice/cache_mypy_cache
CI: cache the mypy cache for faster runs
2023-05-15 17:49:38 +01:00
Martijn Pieters
b5aabaaf59 CI: cache the mypy cache for faster runs 2023-05-15 17:43:49 +01:00
Martijn Pieters
2da4701c1d Merge pull request #148 from long2ice/separate_linting
CI: use a separate step to run linters
2023-05-15 17:33:34 +01:00
Martijn Pieters
31d0b007cd CI: use a separate step to run linters
This makes it easier to separate linter dependencies from older Python
releases.
2023-05-15 17:30:06 +01:00
Martijn Pieters
e57cd59c98 Merge pull request #149 from long2ice/pr_is_not_push
CI: don't run this workflow twice on a PR
2023-05-15 17:25:29 +01:00
Martijn Pieters
4a012c7cae CI: don't run this workflow twice on a PR
If a maintainer pushes to a PR, the workflow push and pull_request events
both trigger. Limit the workflow to the main branch, explicitly.
2023-05-15 17:23:26 +01:00
Martijn Pieters
915f3dd8f2 Add a cache status header to the response
The header name is configurable, and defaults to `X-FastAPI-Cache`,
the value is either `HIT` or `MISS`.

Note that the header is not set at all when the cache is disabled.
2023-05-14 17:03:57 +01:00
Martijn Pieters
29426de95f Refactor decorator, consolidate miss / hit paths
Use just three code paths: uncacheable, cache miss and cache hit. This
makes it much easier to follow what happens for each case. the only
places where the inner function now exits early are when the call is
uncacheable, or when there is a cache hit and the request included a
matching If-Not-Modified header.

- Use a utility function to capture when a request should not use the
  cache
- Use the starlette.status constant for the 'not modified' status for
  code clarity.
- Use `setattr()` for the inner function signature, avoiding the need
  for a type checker override comment.
2023-05-14 17:03:57 +01:00
Martijn Pieters
d10f4af6d6 Import supported backends
This ensures that any syntax issues are caught early (by type checkers
and tests). Backends that are missing dependencies are skipped. By
importing, this exposed an issue where the redis type annotations
raised an exception, which has been fixed by using forward annotations.

To help avoid import dependency hell, the Backend ABC has been moved to
`fastapi_cache.types`. In the process, it has been made an actual ABC.
2023-05-14 17:02:30 +01:00
long2ice
9638d70dfe Merge pull request #133 from mjpieters/correct_json_decode_return
Clean up type annotations
2023-05-12 14:53:40 +08:00
Martijn Pieters
0763cd7b95 Add pyright strict type checking 2023-05-11 12:57:28 +01:00
Martijn Pieters
ad1eae2b4b Set python version for mypy
This lets you catch compatibility issues regardless of the current python version used for development.
2023-05-11 12:57:27 +01:00
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