]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
๐Ÿ‘ท Adopt Ruff format (#10517)
authorSebastiรกn Ramรญrez <tiangolo@gmail.com>
Tue, 24 Oct 2023 20:26:06 +0000 (00:26 +0400)
committerGitHub <noreply@github.com>
Tue, 24 Oct 2023 20:26:06 +0000 (00:26 +0400)
* ๐Ÿ”ง Update pre-commit, use ruff format

* โฌ†๏ธ Upgrade dependencies, use Ruff for formatting

* ๐Ÿ”ง Update Ruff config

* ๐Ÿ”จ Update lint and format scripts, use Ruff

* ๐ŸŽจ Format internals with Ruff

* ๐ŸŽจ Format docs scripts

* ๐ŸŽจ Format tests

* ๐ŸŽจ Format extra commas in src for docs

* ๐Ÿ“ Update docs mentioning `@lru_cache()`, use `@lru_cache` instead to keep consistency with the format

* ๐ŸŽจ Update src for docs, use plain `@lru_cache`

* ๐ŸŽจ Update src for docs format and docs references

46 files changed:
.pre-commit-config.yaml
docs/em/docs/advanced/settings.md
docs/em/docs/tutorial/query-params-str-validations.md
docs/en/docs/advanced/settings.md
docs/en/docs/tutorial/query-params-str-validations.md
docs/ru/docs/tutorial/query-params-str-validations.md
docs/zh/docs/advanced/settings.md
docs_src/header_params/tutorial002_an.py
docs_src/header_params/tutorial002_an_py39.py
docs_src/query_params_str_validations/tutorial004.py
docs_src/query_params_str_validations/tutorial004_an.py
docs_src/query_params_str_validations/tutorial004_an_py310.py
docs_src/query_params_str_validations/tutorial004_an_py310_regex.py
docs_src/query_params_str_validations/tutorial004_an_py39.py
docs_src/query_params_str_validations/tutorial004_py310.py
docs_src/query_params_str_validations/tutorial008.py
docs_src/query_params_str_validations/tutorial008_an.py
docs_src/query_params_str_validations/tutorial008_an_py310.py
docs_src/query_params_str_validations/tutorial008_an_py39.py
docs_src/query_params_str_validations/tutorial008_py310.py
docs_src/query_params_str_validations/tutorial010.py
docs_src/query_params_str_validations/tutorial010_an.py
docs_src/query_params_str_validations/tutorial010_an_py310.py
docs_src/query_params_str_validations/tutorial010_an_py39.py
docs_src/query_params_str_validations/tutorial010_py310.py
docs_src/settings/app02/main.py
docs_src/settings/app02_an/main.py
docs_src/settings/app02_an_py39/main.py
docs_src/settings/app03/main.py
docs_src/settings/app03_an/main.py
docs_src/settings/app03_an_py39/main.py
fastapi/_compat.py
fastapi/applications.py
fastapi/security/http.py
fastapi/security/oauth2.py
fastapi/utils.py
pyproject.toml
requirements-docs-tests.txt
requirements-docs.txt
requirements-tests.txt
scripts/docs.py
scripts/format.sh
scripts/lint.sh
scripts/mkdocs_hooks.py
tests/test_openapi_examples.py
tests/test_schema_extra_examples.py

index 9f7085f72fdca8f16ddbf180a8b42d9984f7aa58..a7f2fb3f229d31d77630f5bf748e9c22e52141f7 100644 (file)
@@ -13,23 +13,13 @@ repos:
         -   --unsafe
     -   id: end-of-file-fixer
     -   id: trailing-whitespace
--   repo: https://github.com/asottile/pyupgrade
-    rev: v3.7.0
-    hooks:
-    -   id: pyupgrade
-        args:
-        - --py3-plus
-        - --keep-runtime-typing
 -   repo: https://github.com/charliermarsh/ruff-pre-commit
-    rev: v0.0.275
+    rev: v0.1.2
     hooks:
     -   id: ruff
         args:
         - --fix
--   repo: https://github.com/psf/black
-    rev: 23.3.0
-    hooks:
-    -   id: black
+    -   id: ruff-format
 ci:
     autofix_commit_msg: ๐ŸŽจ [pre-commit.ci] Auto format from pre-commit.com hooks
     autoupdate_commit_msg: โฌ† [pre-commit.ci] pre-commit autoupdate
index bc50bf755ae433e1ea7f460f4283e9c409c00b9f..cc7a08bab83b403e17cda7a1d3dd9f4a51daa7e3 100644 (file)
@@ -221,7 +221,7 @@ $ ADMIN_EMAIL="deadpool@example.com" APP_NAME="ChimichangApp" uvicorn main:app
 ```
 
 !!! tip
-    ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”ฌ `@lru_cache()` ๐Ÿ–.
+    ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”ฌ `@lru_cache` ๐Ÿ–.
 
     ๐Ÿ”œ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค” `get_settings()` ๐Ÿ˜ ๐Ÿ”ข.
 
@@ -302,7 +302,7 @@ def get_settings():
 
 ๐Ÿ‘ฅ ๐Ÿ”œ โœ ๐Ÿ‘ˆ ๐ŸŽš ๐Ÿ”  ๐Ÿ“จ, &amp; ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘‚ `.env` ๐Ÿ“ ๐Ÿ”  ๐Ÿ“จ. ๐Ÿ‘ถ ๐Ÿ‘ถ
 
-โœ‹๏ธ ๐Ÿ‘ฅ โš™๏ธ `@lru_cache()` ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”› ๐Ÿ”, `Settings` ๐ŸŽš ๐Ÿ”œ โœ ๐Ÿ•ด ๐Ÿ•, ๐Ÿฅ‡ ๐Ÿ•ฐ โšซ๏ธ ๐Ÿค™. ๐Ÿ‘ถ ๐Ÿ‘ถ
+โœ‹๏ธ ๐Ÿ‘ฅ โš™๏ธ `@lru_cache` ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”› ๐Ÿ”, `Settings` ๐ŸŽš ๐Ÿ”œ โœ ๐Ÿ•ด ๐Ÿ•, ๐Ÿฅ‡ ๐Ÿ•ฐ โšซ๏ธ ๐Ÿค™. ๐Ÿ‘ถ ๐Ÿ‘ถ
 
 ```Python hl_lines="1  10"
 {!../../../docs_src/settings/app03/main.py!}
@@ -312,14 +312,14 @@ def get_settings():
 
 #### `lru_cache` ๐Ÿ“ก โ„น
 
-`@lru_cache()` ๐Ÿ”€ ๐Ÿ”ข โšซ๏ธ ๐ŸŽ€ ๐Ÿ“จ ๐ŸŽ ๐Ÿ’ฒ ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿฅ‡ ๐Ÿ•ฐ, โ†ฉ๏ธ ๐Ÿ’ป โšซ๏ธ ๐Ÿ”„, ๐Ÿ› ๏ธ ๐Ÿ“Ÿ ๐Ÿ”ข ๐Ÿ”  ๐Ÿ•ฐ.
+`@lru_cache` ๐Ÿ”€ ๐Ÿ”ข โšซ๏ธ ๐ŸŽ€ ๐Ÿ“จ ๐ŸŽ ๐Ÿ’ฒ ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿฅ‡ ๐Ÿ•ฐ, โ†ฉ๏ธ ๐Ÿ’ป โšซ๏ธ ๐Ÿ”„, ๐Ÿ› ๏ธ ๐Ÿ“Ÿ ๐Ÿ”ข ๐Ÿ”  ๐Ÿ•ฐ.
 
 , ๐Ÿ”ข ๐Ÿ”› โšซ๏ธ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ• ๐Ÿ”  ๐ŸŒ€ โŒ. &amp; โคด๏ธ ๐Ÿ’ฒ ๐Ÿ“จ ๐Ÿ”  ๐Ÿ‘ˆ ๐ŸŒ€ โŒ ๐Ÿ”œ โš™๏ธ ๐Ÿ”„ &amp; ๐Ÿ”„ ๐Ÿ•โ” ๐Ÿ”ข ๐Ÿค™ โฎ๏ธ โšซ๏ธโ” ๐ŸŽ ๐ŸŒ€ โŒ.
 
 ๐Ÿ–ผ, ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿ”ข:
 
 ```Python
-@lru_cache()
+@lru_cache
 def say_hi(name: str, salutation: str = "Ms."):
     return f"Hello {salutation} {name}"
 ```
@@ -371,7 +371,7 @@ participant execute as Execute function
 
 ๐Ÿ‘ˆ ๐ŸŒŒ, โšซ๏ธ ๐ŸŽญ ๐ŸŒ– ๐Ÿšฅ โšซ๏ธ ๐ŸŒ ๐Ÿ”ข. โœ‹๏ธ โšซ๏ธ โš™๏ธ ๐Ÿ”— ๐Ÿ”ข, โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ” โšซ๏ธ ๐Ÿ’ช ๐Ÿ”ฌ.
 
-`@lru_cache()` ๐Ÿ• `functools` โ” ๐Ÿ• ๐Ÿ ๐Ÿฉ ๐Ÿ—ƒ, ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ… ๐Ÿ”ƒ โšซ๏ธ <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">๐Ÿ ๐Ÿฉบ `@lru_cache()`</a>.
+`@lru_cache` ๐Ÿ• `functools` โ” ๐Ÿ• ๐Ÿ ๐Ÿฉ ๐Ÿ—ƒ, ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ… ๐Ÿ”ƒ โšซ๏ธ <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">๐Ÿ ๐Ÿฉบ `@lru_cache`</a>.
 
 ## ๐ŸŒƒ
 
@@ -379,4 +379,4 @@ participant execute as Execute function
 
 * โš™๏ธ ๐Ÿ”— ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“‰ ๐Ÿ”ฌ.
 * ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ `.env` ๐Ÿ“ โฎ๏ธ โšซ๏ธ.
-* โš™๏ธ `@lru_cache()` โžก๏ธ ๐Ÿ‘† โŽ ๐Ÿ‘‚ ๐Ÿ‡จ๐Ÿ‡ป ๐Ÿ“ ๐Ÿ”„ &amp; ๐Ÿ”„ ๐Ÿ”  ๐Ÿ“จ, โช ๐Ÿค ๐Ÿ‘† ๐Ÿ” โšซ๏ธ โฎ๏ธ ๐Ÿ”ฌ.
+* โš™๏ธ `@lru_cache` โžก๏ธ ๐Ÿ‘† โŽ ๐Ÿ‘‚ ๐Ÿ‡จ๐Ÿ‡ป ๐Ÿ“ ๐Ÿ”„ &amp; ๐Ÿ”„ ๐Ÿ”  ๐Ÿ“จ, โช ๐Ÿค ๐Ÿ‘† ๐Ÿ” โšซ๏ธ โฎ๏ธ ๐Ÿ”ฌ.
index d6b67bd518f73e99c425ef0bbbe87055c53412f5..f0e455abe151555ccd76168ff50ec182621d30a1 100644 (file)
@@ -371,7 +371,7 @@ http://localhost:8000/items/
 
 === "๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ &amp; ๐Ÿ”›"
 
-    ```Python hl_lines="12"
+    ```Python hl_lines="11"
     {!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
     ```
 
@@ -421,7 +421,7 @@ http://127.0.0.1:8000/items/?item-query=foobaritems
 
 === "๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ &amp; ๐Ÿ”›"
 
-    ```Python hl_lines="17"
+    ```Python hl_lines="16"
     {!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
     ```
 
index d39130777f92af8195f67680156b1db52858c1de..32df9000641d36ff4933cec3fb88b61868996b76 100644 (file)
@@ -276,7 +276,7 @@ Now we create a dependency that returns a new `config.Settings()`.
     ```
 
 !!! tip
-    We'll discuss the `@lru_cache()` in a bit.
+    We'll discuss the `@lru_cache` in a bit.
 
     For now you can assume `get_settings()` is a normal function.
 
@@ -388,7 +388,7 @@ def get_settings():
 
 we would create that object for each request, and we would be reading the `.env` file for each request. โš ๏ธ
 
-But as we are using the `@lru_cache()` decorator on top, the `Settings` object will be created only once, the first time it's called. โœ”๏ธ
+But as we are using the `@lru_cache` decorator on top, the `Settings` object will be created only once, the first time it's called. โœ”๏ธ
 
 === "Python 3.9+"
 
@@ -415,14 +415,14 @@ Then for any subsequent calls of `get_settings()` in the dependencies for the ne
 
 #### `lru_cache` Technical Details
 
-`@lru_cache()` modifies the function it decorates to return the same value that was returned the first time, instead of computing it again, executing the code of the function every time.
+`@lru_cache` modifies the function it decorates to return the same value that was returned the first time, instead of computing it again, executing the code of the function every time.
 
 So, the function below it will be executed once for each combination of arguments. And then the values returned by each of those combinations of arguments will be used again and again whenever the function is called with exactly the same combination of arguments.
 
 For example, if you have a function:
 
 ```Python
-@lru_cache()
+@lru_cache
 def say_hi(name: str, salutation: str = "Ms."):
     return f"Hello {salutation} {name}"
 ```
@@ -474,7 +474,7 @@ In the case of our dependency `get_settings()`, the function doesn't even take a
 
 That way, it behaves almost as if it was just a global variable. But as it uses a dependency function, then we can override it easily for testing.
 
-`@lru_cache()` is part of `functools` which is part of Python's standard library, you can read more about it in the <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">Python docs for `@lru_cache()`</a>.
+`@lru_cache` is part of `functools` which is part of Python's standard library, you can read more about it in the <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">Python docs for `@lru_cache`</a>.
 
 ## Recap
 
@@ -482,4 +482,4 @@ You can use Pydantic Settings to handle the settings or configurations for your
 
 * By using a dependency you can simplify testing.
 * You can use `.env` files with it.
-* Using `@lru_cache()` lets you avoid reading the dotenv file again and again for each request, while allowing you to override it during testing.
+* Using `@lru_cache` lets you avoid reading the dotenv file again and again for each request, while allowing you to override it during testing.
index 0e777f6a0969f7aa17cab8b2f641172021c5407b..91ae615fffb685fc2f7e524df405f43d0221fad0 100644 (file)
@@ -737,7 +737,7 @@ And a `description`:
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="12"
+    ```Python hl_lines="11"
     {!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
     ```
 
@@ -835,7 +835,7 @@ Then pass the parameter `deprecated=True` to `Query`:
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="17"
+    ```Python hl_lines="16"
     {!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
     ```
 
index 15be5dbf6bc069698e5e64a3212f604777d9ba62..cc826b8711a9c7da22771f07ebfef4389aef7819 100644 (file)
@@ -741,7 +741,7 @@ http://localhost:8000/items/
     !!! tip "ะŸะพะดัะบะฐะทะบะฐ"
         ะ ะตะบะพะผะตะฝะดัƒะตั‚ัั ะธัะฟะพะปัŒะทะพะฒะฐั‚ัŒ ะฒะตั€ัะธัŽ ั `Annotated` ะตัะปะธ ะฒะพะทะผะพะถะฝะพ.
 
-    ```Python hl_lines="12"
+    ```Python hl_lines="11"
     {!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
     ```
 
@@ -839,7 +839,7 @@ http://127.0.0.1:8000/items/?item-query=foobaritems
     !!! tip "ะŸะพะดัะบะฐะทะบะฐ"
         ะ ะตะบะพะผะตะฝะดัƒะตั‚ัั ะธัะฟะพะปัŒะทะพะฒะฐั‚ัŒ ะฒะตั€ัะธัŽ ั `Annotated` ะตัะปะธ ะฒะพะทะผะพะถะฝะพ.
 
-    ```Python hl_lines="17"
+    ```Python hl_lines="16"
     {!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
     ```
 
index 7f718acefbbacb60ade614dcb65337fc88da0070..93e48a61058f4b26d7b6da734b2109b1911bfd37 100644 (file)
@@ -239,7 +239,7 @@ $ ADMIN_EMAIL="deadpool@example.com" APP_NAME="ChimichangApp"uvicorn main:app
     ```
 
 !!! tip
-    ๆˆ‘ไปฌ็จๅŽไผš่ฎจ่ฎบ `@lru_cache()`ใ€‚
+    ๆˆ‘ไปฌ็จๅŽไผš่ฎจ่ฎบ `@lru_cache`ใ€‚
 
     ็›ฎๅ‰๏ผŒๆ‚จๅฏไปฅๅฐ† `get_settings()` ่ง†ไธบๆ™ฎ้€šๅ‡ฝๆ•ฐใ€‚
 
@@ -337,7 +337,7 @@ def get_settings():
 
 ๆˆ‘ไปฌๅฐ†ไธบๆฏไธช่ฏทๆฑ‚ๅˆ›ๅปบ่ฏฅๅฏน่ฑก๏ผŒๅนถไธ”ๅฐ†ๅœจๆฏไธช่ฏทๆฑ‚ไธญ่ฏปๅ– `.env` ๆ–‡ไปถใ€‚ โš ๏ธ
 
-ไฝ†ๆ˜ฏ๏ผŒ็”ฑไบŽๆˆ‘ไปฌๅœจ้กถ้ƒจไฝฟ็”จไบ† `@lru_cache()` ่ฃ…้ฅฐๅ™จ๏ผŒๅ› ๆญคๅชๆœ‰ๅœจ็ฌฌไธ€ๆฌก่ฐƒ็”จๅฎƒๆ—ถ๏ผŒๆ‰ไผšๅˆ›ๅปบ `Settings` ๅฏน่ฑกไธ€ๆฌกใ€‚ โœ”๏ธ
+ไฝ†ๆ˜ฏ๏ผŒ็”ฑไบŽๆˆ‘ไปฌๅœจ้กถ้ƒจไฝฟ็”จไบ† `@lru_cache` ่ฃ…้ฅฐๅ™จ๏ผŒๅ› ๆญคๅชๆœ‰ๅœจ็ฌฌไธ€ๆฌก่ฐƒ็”จๅฎƒๆ—ถ๏ผŒๆ‰ไผšๅˆ›ๅปบ `Settings` ๅฏน่ฑกไธ€ๆฌกใ€‚ โœ”๏ธ
 
 === "Python 3.9+"
 
@@ -364,13 +364,13 @@ def get_settings():
 
 #### `lru_cache` ๆŠ€ๆœฏ็ป†่Š‚
 
-`@lru_cache()` ไฟฎๆ”นไบ†ๅฎƒๆ‰€่ฃ…้ฅฐ็š„ๅ‡ฝๆ•ฐ๏ผŒไปฅ่ฟ”ๅ›ž็ฌฌไธ€ๆฌก่ฟ”ๅ›ž็š„็›ธๅŒๅ€ผ๏ผŒ่€Œไธๆ˜ฏๅ†ๆฌก่ฎก็ฎ—ๅฎƒ๏ผŒๆฏๆฌก้ƒฝๆ‰ง่กŒๅ‡ฝๆ•ฐ็š„ไปฃ็ ใ€‚
+`@lru_cache` ไฟฎๆ”นไบ†ๅฎƒๆ‰€่ฃ…้ฅฐ็š„ๅ‡ฝๆ•ฐ๏ผŒไปฅ่ฟ”ๅ›ž็ฌฌไธ€ๆฌก่ฟ”ๅ›ž็š„็›ธๅŒๅ€ผ๏ผŒ่€Œไธๆ˜ฏๅ†ๆฌก่ฎก็ฎ—ๅฎƒ๏ผŒๆฏๆฌก้ƒฝๆ‰ง่กŒๅ‡ฝๆ•ฐ็š„ไปฃ็ ใ€‚
 
 ๅ› ๆญค๏ผŒไธ‹้ข็š„ๅ‡ฝๆ•ฐๅฐ†ๅฏนๆฏไธชๅ‚ๆ•ฐ็ป„ๅˆๆ‰ง่กŒไธ€ๆฌกใ€‚็„ถๅŽ๏ผŒๆฏไธชๅ‚ๆ•ฐ็ป„ๅˆ่ฟ”ๅ›ž็š„ๅ€ผๅฐ†ๅœจไฝฟ็”จๅฎŒๅ…จ็›ธๅŒ็š„ๅ‚ๆ•ฐ็ป„ๅˆ่ฐƒ็”จๅ‡ฝๆ•ฐๆ—ถๅ†ๆฌกไฝฟ็”จใ€‚
 
 ไพ‹ๅฆ‚๏ผŒๅฆ‚ๆžœๆ‚จๆœ‰ไธ€ไธชๅ‡ฝๆ•ฐ๏ผš
 ```Python
-@lru_cache()
+@lru_cache
 def say_hi(name: str, salutation: str = "Ms."):
     return f"Hello {salutation} {name}"
 ```
@@ -422,7 +422,7 @@ participant execute as Execute function
 
 ่ฟ™ๆ ท๏ผŒๅฎƒ็š„่กŒไธบๅ‡ ไนŽๅฐฑๅƒๆ˜ฏไธ€ไธชๅ…จๅฑ€ๅ˜้‡ใ€‚ไฝ†ๆ˜ฏ็”ฑไบŽๅฎƒไฝฟ็”จไบ†ไพ่ต–้กนๅ‡ฝๆ•ฐ๏ผŒๅ› ๆญคๆˆ‘ไปฌๅฏไปฅ่ฝปๆพๅœฐ่ฟ›่กŒๆต‹่ฏ•ๆ—ถ็š„่ฆ†็›–ใ€‚
 
-`@lru_cache()` ๆ˜ฏ `functools` ็š„ไธ€้ƒจๅˆ†๏ผŒๅฎƒๆ˜ฏ Python ๆ ‡ๅ‡†ๅบ“็š„ไธ€้ƒจๅˆ†๏ผŒๆ‚จๅฏไปฅๅœจ<a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">Python ๆ–‡ๆกฃไธญไบ†่งฃๆœ‰ๅ…ณ `@lru_cache()` ็š„ๆ›ดๅคšไฟกๆฏ</a>ใ€‚
+`@lru_cache` ๆ˜ฏ `functools` ็š„ไธ€้ƒจๅˆ†๏ผŒๅฎƒๆ˜ฏ Python ๆ ‡ๅ‡†ๅบ“็š„ไธ€้ƒจๅˆ†๏ผŒๆ‚จๅฏไปฅๅœจ<a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" class="external-link" target="_blank">Python ๆ–‡ๆกฃไธญไบ†่งฃๆœ‰ๅ…ณ `@lru_cache` ็š„ๆ›ดๅคšไฟกๆฏ</a>ใ€‚
 
 ## ๅฐ็ป“
 
@@ -430,4 +430,4 @@ participant execute as Execute function
 
 * ้€š่ฟ‡ไฝฟ็”จไพ่ต–้กน๏ผŒๆ‚จๅฏไปฅ็ฎ€ๅŒ–ๆต‹่ฏ•ใ€‚
 * ๆ‚จๅฏไปฅไฝฟ็”จ `.env` ๆ–‡ไปถใ€‚
-* ไฝฟ็”จ `@lru_cache()` ๅฏไปฅ้ฟๅ…ไธบๆฏไธช่ฏทๆฑ‚้‡ๅค่ฏปๅ– dotenv ๆ–‡ไปถ๏ผŒๅŒๆ—ถๅ…่ฎธๆ‚จๅœจๆต‹่ฏ•ๆ—ถ่ฟ›่กŒ่ฆ†็›–ใ€‚
+* ไฝฟ็”จ `@lru_cache` ๅฏไปฅ้ฟๅ…ไธบๆฏไธช่ฏทๆฑ‚้‡ๅค่ฏปๅ– dotenv ๆ–‡ไปถ๏ผŒๅŒๆ—ถๅ…่ฎธๆ‚จๅœจๆต‹่ฏ•ๆ—ถ่ฟ›่กŒ่ฆ†็›–ใ€‚
index 65d972d46eec8fd60af400a99d7b4d5827bf37a7..82fe49ba2b1b90d22d594d5e89ab3b67e0b63903 100644 (file)
@@ -10,6 +10,6 @@ app = FastAPI()
 async def read_items(
     strange_header: Annotated[
         Union[str, None], Header(convert_underscores=False)
-    ] = None
+    ] = None,
 ):
     return {"strange_header": strange_header}
index 7f6a99f9c369003af5cd3b1b704df4e3b673805f..008e4b6e1a1a54470c0b6b688e44e36f6ff76745 100644 (file)
@@ -9,6 +9,6 @@ app = FastAPI()
 async def read_items(
     strange_header: Annotated[
         Union[str, None], Header(convert_underscores=False)
-    ] = None
+    ] = None,
 ):
     return {"strange_header": strange_header}
index 3639b6c38fe7593c53ad96b7e1769fc652ee7aa2..64a647a16a566153dd723a606e57455de314d6dd 100644 (file)
@@ -9,7 +9,7 @@ app = FastAPI()
 async def read_items(
     q: Union[str, None] = Query(
         default=None, min_length=3, max_length=50, pattern="^fixedquery$"
-    )
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 24698c7b34fac0fe7cd651fb7d12cf5eb0540c50..c75d45d63e777279832cfddf0cd6f3ce82e74d87 100644 (file)
@@ -10,7 +10,7 @@ app = FastAPI()
 async def read_items(
     q: Annotated[
         Union[str, None], Query(min_length=3, max_length=50, pattern="^fixedquery$")
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index b7b629ee8acec17e8815e4d521525f332b0beef0..20cf1988fdd8a0614f9fb2f4bdaefef5866ce87c 100644 (file)
@@ -9,7 +9,7 @@ app = FastAPI()
 async def read_items(
     q: Annotated[
         str | None, Query(min_length=3, max_length=50, pattern="^fixedquery$")
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 8fd375b3d549f3f4e891fffce7a8f02d58b4b940..21e0d3eb856f8b267e3b7fc9fdec5a6f9edfffe2 100644 (file)
@@ -9,7 +9,7 @@ app = FastAPI()
 async def read_items(
     q: Annotated[
         str | None, Query(min_length=3, max_length=50, regex="^fixedquery$")
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 8e9a6fc32d88fc53becfd488c4f34ac217c65106..de27097b3860b31647c424041c03232270f2fb0b 100644 (file)
@@ -9,7 +9,7 @@ app = FastAPI()
 async def read_items(
     q: Annotated[
         Union[str, None], Query(min_length=3, max_length=50, pattern="^fixedquery$")
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index f80798bcb8f8f64bdc3e9e55f4a94b6be37b3f80..7801e75000f8d8e19dcbc5ab2db496f4b019d189 100644 (file)
@@ -5,8 +5,9 @@ app = FastAPI()
 
 @app.get("/items/")
 async def read_items(
-    q: str
-    | None = Query(default=None, min_length=3, max_length=50, pattern="^fixedquery$")
+    q: str | None = Query(
+        default=None, min_length=3, max_length=50, pattern="^fixedquery$"
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index d112a9ab8aa5ad983cf1418885d48a730a02894a..e3e0b50aa14e8ff6fc074a4b706e32f9799dc420 100644 (file)
@@ -12,7 +12,7 @@ async def read_items(
         title="Query string",
         description="Query string for the items to search in the database that have a good match",
         min_length=3,
-    )
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 5699f1e88d5963d7996f130a5891c6ae1b4c0ed5..01606a9203624443604e9426904ab9bb3a86e935 100644 (file)
@@ -15,7 +15,7 @@ async def read_items(
             description="Query string for the items to search in the database that have a good match",
             min_length=3,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 4aaadf8b4785eda1e9b1ac744294b082cc2de1b3..44b3082b63bada3e26d32846c480af1257a4a8a4 100644 (file)
@@ -14,7 +14,7 @@ async def read_items(
             description="Query string for the items to search in the database that have a good match",
             min_length=3,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 1c3b36176517d88a98435ab268bec01c63c9f825..f3f2f2c0e7915f60bada3a68bb93207391955f4d 100644 (file)
@@ -14,7 +14,7 @@ async def read_items(
             description="Query string for the items to search in the database that have a good match",
             min_length=3,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 489f631d5e90875a5b9128dffbd7aac973c50317..57438527262fd3ff1ba9a89be7e0570b7c0d554c 100644 (file)
@@ -5,13 +5,12 @@ app = FastAPI()
 
 @app.get("/items/")
 async def read_items(
-    q: str
-    | None = Query(
+    q: str | None = Query(
         default=None,
         title="Query string",
         description="Query string for the items to search in the database that have a good match",
         min_length=3,
-    )
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 3314f8b6d6440ae26733599daa0ce6b2239fabb9..ff29176fe5f2879586c9496463cfa2bf24e6e381 100644 (file)
@@ -16,7 +16,7 @@ async def read_items(
         max_length=50,
         pattern="^fixedquery$",
         deprecated=True,
-    )
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index c5df00897f988140611d832dd0fa448459547a45..ed343230f4bda6198c66e05ec618f4fab521661c 100644 (file)
@@ -19,7 +19,7 @@ async def read_items(
             pattern="^fixedquery$",
             deprecated=True,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index a8e8c099b5e0a3f7f4346e5c27613f4eb6407546..775095bda86ae8d0296d961eadc1e164d7016756 100644 (file)
@@ -18,7 +18,7 @@ async def read_items(
             pattern="^fixedquery$",
             deprecated=True,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 955880dd6a65b59568d1b3e3bf7fe16eb968662f..b126c116f0cd38641a20aa93e0846b38a99876f1 100644 (file)
@@ -18,7 +18,7 @@ async def read_items(
             pattern="^fixedquery$",
             deprecated=True,
         ),
-    ] = None
+    ] = None,
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 9ea7b3c49f81f1d1101e117e4615a6d21123a620..530e6cf5b6c939d63e88f6eba082812cbf040213 100644 (file)
@@ -5,8 +5,7 @@ app = FastAPI()
 
 @app.get("/items/")
 async def read_items(
-    q: str
-    | None = Query(
+    q: str | None = Query(
         default=None,
         alias="item-query",
         title="Query string",
@@ -15,7 +14,7 @@ async def read_items(
         max_length=50,
         pattern="^fixedquery$",
         deprecated=True,
-    )
+    ),
 ):
     results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
     if q:
index 163aa261424645d20674bceed29318ec7a014d2e..941f82e6b39b6b757f98480af0158f0c1b928090 100644 (file)
@@ -7,7 +7,7 @@ from .config import Settings
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return Settings()
 
index cb679202d6ed52642b7a6e54d28fcf0cef4b1863..3a578cc3388982b21f2b367e9ba5c24214bd66b8 100644 (file)
@@ -8,7 +8,7 @@ from .config import Settings
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return Settings()
 
index 61be74fcb9a17a27ffa2efc1bf8eb279931c7e30..6d5db12a87941307a088b4b283ffb1ab23474650 100644 (file)
@@ -8,7 +8,7 @@ from .config import Settings
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return Settings()
 
index 69bc8c6e0eb6b9d0643ddcc771633b77f73cd553..ea64a5709cb49598f8fcb3dcf2e34eca079008a7 100644 (file)
@@ -7,7 +7,7 @@ from . import config
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return config.Settings()
 
index c33b98f474880a176e5ac61f8c8a358f3d6013c5..2f64b9cd175d8b0bbb3c8c4396cc506815ae12cd 100644 (file)
@@ -8,7 +8,7 @@ from . import config
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return config.Settings()
 
index b89c6b6cf44fbf8acf488ed971e1ed8a4bb5ce6e..62f3476396ff02b301c7c04f00679210577cafe5 100644 (file)
@@ -8,7 +8,7 @@ from . import config
 app = FastAPI()
 
 
-@lru_cache()
+@lru_cache
 def get_settings():
     return config.Settings()
 
index a4b305d429fdd115fcc91f2b53c22e9c76bb5c39..fc605d0ec68e383a0dbb51a5b7f26a5f9a15ac1a 100644 (file)
@@ -197,9 +197,9 @@ if PYDANTIC_V2:
         if "$ref" not in json_schema:
             # TODO remove when deprecating Pydantic v1
             # Ref: https://github.com/pydantic/pydantic/blob/d61792cc42c80b13b23e3ffa74bc37ec7c77f7d1/pydantic/schema.py#L207
-            json_schema[
-                "title"
-            ] = field.field_info.title or field.alias.title().replace("_", " ")
+            json_schema["title"] = (
+                field.field_info.title or field.alias.title().replace("_", " ")
+            )
         return json_schema
 
     def get_compat_model_name_map(fields: List[ModelField]) -> ModelNameMap:
index 8ca374a54e713d52c734b9202f6398acede34f82..3021d75937d1cebba69d73d685e8975350f93c64 100644 (file)
@@ -896,9 +896,7 @@ class FastAPI(Starlette):
                 [FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/).
                 """
             ),
-        ] = (
-            webhooks or routing.APIRouter()
-        )
+        ] = webhooks or routing.APIRouter()
         self.root_path = root_path or openapi_prefix
         self.state: Annotated[
             State,
@@ -951,7 +949,7 @@ class FastAPI(Starlette):
         )
         self.exception_handlers: Dict[
             Any, Callable[[Request, Any], Union[Response, Awaitable[Response]]]
-        ] = ({} if exception_handlers is None else dict(exception_handlers))
+        ] = {} if exception_handlers is None else dict(exception_handlers)
         self.exception_handlers.setdefault(HTTPException, http_exception_handler)
         self.exception_handlers.setdefault(
             RequestValidationError, request_validation_exception_handler
index 3627777d672604822ff5c4474bb5819d7e159801..738455de3810318df0a3a6e011fe67312412e3fb 100644 (file)
@@ -210,7 +210,7 @@ class HTTPBasic(HTTPBase):
         try:
             data = b64decode(param).decode("ascii")
         except (ValueError, UnicodeDecodeError, binascii.Error):
-            raise invalid_user_credentials_exc
+            raise invalid_user_credentials_exc  # noqa: B904
         username, separator, password = data.partition(":")
         if not separator:
             raise invalid_user_credentials_exc
index d427783add382d5af2fdf31fef8250e13090f673..9281dfb64f8a4b31f2d617ca0d7e90b84d06cc3c 100644 (file)
@@ -626,9 +626,7 @@ class SecurityScopes:
                 The list of all the scopes required by dependencies.
                 """
             ),
-        ] = (
-            scopes or []
-        )
+        ] = scopes or []
         self.scope_str: Annotated[
             str,
             Doc(
index 53b47a160460d2bd1d27cf10b44626b56a4cf530..f8463dda24675aa31e0928ad6ed7e2c79c20b411 100644 (file)
@@ -152,7 +152,8 @@ def create_cloned_field(
         ]
     if field.key_field:  # type: ignore[attr-defined]
         new_field.key_field = create_cloned_field(  # type: ignore[attr-defined]
-            field.key_field, cloned_types=cloned_types  # type: ignore[attr-defined]
+            field.key_field,  # type: ignore[attr-defined]
+            cloned_types=cloned_types,
         )
     new_field.validators = field.validators  # type: ignore[attr-defined]
     new_field.pre_validators = field.pre_validators  # type: ignore[attr-defined]
index addde1d33fc91a22352f29083e7a1cc5f804ea30..e67486ae31bf8c126a370dc2ea4baaa636f6bad8 100644 (file)
@@ -130,11 +130,13 @@ select = [
     "I",  # isort
     "C",  # flake8-comprehensions
     "B",  # flake8-bugbear
+    "UP",  # pyupgrade
 ]
 ignore = [
     "E501",  # line too long, handled by black
     "B008",  # do not perform function calls in argument defaults
     "C901",  # too complex
+    "W191", # indentation contains tabs
 ]
 
 [tool.ruff.per-file-ignores]
@@ -154,6 +156,22 @@ ignore = [
 "docs_src/query_params_str_validations/tutorial012_an_py39.py" = ["B006"]
 "docs_src/query_params_str_validations/tutorial013_an.py" = ["B006"]
 "docs_src/query_params_str_validations/tutorial013_an_py39.py" = ["B006"]
+"docs_src/security/tutorial004.py" = ["B904"]
+"docs_src/security/tutorial004_an.py" = ["B904"]
+"docs_src/security/tutorial004_an_py310.py" = ["B904"]
+"docs_src/security/tutorial004_an_py39.py" = ["B904"]
+"docs_src/security/tutorial004_py310.py" = ["B904"]
+"docs_src/security/tutorial005.py" = ["B904"]
+"docs_src/security/tutorial005_an.py" = ["B904"]
+"docs_src/security/tutorial005_an_py310.py" = ["B904"]
+"docs_src/security/tutorial005_an_py39.py" = ["B904"]
+"docs_src/security/tutorial005_py310.py" = ["B904"]
+"docs_src/security/tutorial005_py39.py" = ["B904"]
+
 
 [tool.ruff.isort]
 known-third-party = ["fastapi", "pydantic", "starlette"]
+
+[tool.ruff.pyupgrade]
+# Preserve types, even if a file imports `from __future__ import annotations`.
+keep-runtime-typing = true
index 1a4a5726792e4ce0ec6688e1cb698b49ea0220af..b82df49338a1bad6ab1bd9cc3a613461baf912bf 100644 (file)
@@ -1,3 +1,2 @@
 # For mkdocstrings and tests
 httpx >=0.23.0,<0.25.0
-black == 23.3.0
index 3e0df64839c41c751acc8c21d0257e1306003c46..69302f655e015b386bcb97f2ad0af967f72759cb 100644 (file)
@@ -15,3 +15,5 @@ pillow==9.5.0
 cairosvg==2.7.0
 mkdocstrings[python]==0.23.0
 griffe-typingdoc==0.2.2
+# For griffe, it formats with black
+black==23.3.0
index de8d3f26cea847603d86576a30935e70628b9c79..e1a976c1382340c48bef4ee3b239d6f113fa2b39 100644 (file)
@@ -4,7 +4,7 @@ pydantic-settings >=2.0.0
 pytest >=7.1.3,<8.0.0
 coverage[toml] >= 6.5.0,< 8.0
 mypy ==1.4.1
-ruff ==0.0.275
+ruff ==0.1.2
 email_validator >=1.1.1,<3.0.0
 dirty-equals ==0.6.0
 # TODO: once removing databases from tutorial, upgrade SQLAlchemy
index 0023c670cd13a909ff5a36b6ee325ccb46c908e4..73e1900ada0c485af8567e95d75309261da404ed 100644 (file)
@@ -36,7 +36,7 @@ site_path = Path("site").absolute()
 build_site_path = Path("site_build").absolute()
 
 
-@lru_cache()
+@lru_cache
 def is_mkdocs_insiders() -> bool:
     version = metadata.version("mkdocs-material")
     return "insiders" in version
@@ -104,7 +104,7 @@ def new_lang(lang: str = typer.Argument(..., callback=lang_callback)):
 def build_lang(
     lang: str = typer.Argument(
         ..., callback=lang_callback, autocompletion=complete_existing_lang
-    )
+    ),
 ) -> None:
     """
     Build the docs for a language.
@@ -251,7 +251,7 @@ def serve() -> None:
 def live(
     lang: str = typer.Argument(
         None, callback=lang_callback, autocompletion=complete_existing_lang
-    )
+    ),
 ) -> None:
     """
     Serve with livereload a docs site for a specific language.
index 3fb3eb4f19df977e21a5923426375cc119067793..11f25f1ce8a83a9c35600c4fe3bacb1fddc56260 100755 (executable)
@@ -2,4 +2,4 @@
 set -x
 
 ruff fastapi tests docs_src scripts --fix
-black fastapi tests docs_src scripts
+ruff format fastapi tests docs_src scripts
index 4db5caa9627eae5442b37c10ad40f8e89a090697..c0e24db9f64ba2580e595d982c82063e20fdd442 100755 (executable)
@@ -5,4 +5,4 @@ set -x
 
 mypy fastapi
 ruff fastapi tests docs_src scripts
-black fastapi tests --check
+ruff format fastapi tests --check
index 2b6a0564200021f6011d14472fd794e389830efa..8335a13f62914d490a49a05ee2199124f3a9e667 100644 (file)
@@ -14,14 +14,14 @@ non_traslated_sections = [
 ]
 
 
-@lru_cache()
+@lru_cache
 def get_missing_translation_content(docs_dir: str) -> str:
     docs_dir_path = Path(docs_dir)
     missing_translation_path = docs_dir_path.parent.parent / "missing-translation.md"
     return missing_translation_path.read_text(encoding="utf-8")
 
 
-@lru_cache()
+@lru_cache
 def get_mkdocs_material_langs() -> List[str]:
     material_path = Path(material.__file__).parent
     material_langs_path = material_path / "templates" / "partials" / "languages"
index 70664a8a4c07873188c7d96af985ecb0f8fbe076..6597e5058bfedae168b17d4415dea6db956b99cd 100644 (file)
@@ -28,7 +28,7 @@ def examples(
                 "value": {"data": "Data in Body examples, example2"},
             },
         },
-    )
+    ),
 ):
     return item
 
index a1505afe23083053ac3e242f782049225b360825..b313f47e90fa2f27f704bb37bf4568295dc2e2f6 100644 (file)
@@ -40,7 +40,7 @@ def create_app():
                 {"data": "Data in Body examples, example1"},
                 {"data": "Data in Body examples, example2"},
             ],
-        )
+        ),
     ):
         return item
 
@@ -54,7 +54,7 @@ def create_app():
                     {"data": "examples example_examples 1"},
                     {"data": "examples example_examples 2"},
                 ],
-            )
+            ),
         ):
             return item