Commit Graph

807 Commits

Author SHA1 Message Date
Piotr Gnus 7a1108058b
Add dark theme for the documentation (#1230)
* mkdocs: expanded theme configuration to add alternative dark theme.
  The default theme will be determined by the users system theme by the
  `prefers-color-scheme` media query.

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-09-18 13:08:29 +02:00
Steve Dignam 48dea4ddf1
add typing overloads for Config.__call__ (#1097)
* add typing overloads for Config.__call__

This allows for more precise return types instead of the current `Any`.

We have 3 overload cases here:

1. handles cases where the user provides an explicit `cast` argument.

   ```
   reveal_type(config("POOL_SIZE", cast=int))
   # note: Revealed type is 'int'

   reveal_type(config("DEBUG", cast=bool, default=None))
   # note: Revealed type is 'Union[bool, None]'
   ```

2. handles no cast or default being passed

   ```
   reveal_type(config("FOO"))
   # note: Revealed type is 'str'
   ```

3. handles no cast being provided, and the default not being `str`

   ```
   reveal_type(config("DB_NAME", default=None))
   # note: Revealed type is 'Union[str, None]'

   reveal_type(config("FOO", default=False))
   # note: Revealed type is 'Union[str, bool]'
   ```

* ignore overloads from code coverage

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-09-18 12:44:11 +02:00
Pax 874ad467ac
Update responses.md (#1069)
- Fix typo ("Third party middleware" to "...responses")
- Fix w3 link (current link leads to 404; I followed links from archive.org to get the "latest" spec)
- Format it similarly to "Third party middleware" (use h4, use the class name as header)

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-09-18 12:38:52 +02:00
Amin Alaee 85ab09d1b7
Add missing await on database section on docs (#1226)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-09-16 19:49:59 +02:00
Lukas Kahwe Smith 1ae43b45b5
Add BaseHTTPMiddleware import on docs (#1285) 2021-09-16 14:48:32 +02:00
Vincent Sarago eebb46529b
Add starlette-cramjam link to the docs (#1283)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-09-16 14:22:05 +02:00
Marcelo Trylesinski e260283d37
Ignore loop deprecation warnings originating inside asyncio (#1287) 2021-09-16 14:16:25 +02:00
Tom Christie 84e2dc7bfe Include FastAPI link 2021-08-19 15:40:50 +01:00
Tom Christie b65ba8b8ca
Add FastAPI sponsorship (#1271) 2021-08-19 15:17:41 +01:00
Hannes Küttner 7e675a0b86
Fix BadSignature exception handling in SessionMiddleware (#1264) 2021-08-14 15:38:50 +01:00
Thomas Grainger e45c579361
prepare release 0.16.0 (#1233)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-07-19 08:08:36 +01:00
Thomas Grainger b0a6d6ffbd
ignore charset_normalizer related warning (#1242) 2021-07-16 11:07:55 +01:00
Emil Melnikov 8a3e41a544
Document the lifespan event handler parameter (#1110)
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
2021-07-15 17:13:43 +01:00
Thomas Grainger 537ab6afd1
use an async context manager factory for lifespan (#1227) 2021-07-03 18:43:24 +01:00
Thomas Grainger 254d0d97e4
ensure TestClient requests run in the same EventLoop as lifespan (#1213)
* ensure TestClient requests run in the same EventLoop as lifespan

* for lifespan task verification, use native task identity rather than anyio.abc.TaskInfo equality

https://github.com/agronholm/anyio/issues/324

* remove redundant pragma: no cover

* it's now a loop_id not a threading.ident

* replace Protocol with plain Callable TypeAlias

* use lifespan_context to actually open a task group

trio should complain if used incorrectly here.

* assign self.portal once, schedule reset immediately after assignment

* inline apps into their tests

* make task/loop trackers nonlocals
2021-07-03 17:39:25 +01:00
Thomas Grainger d222b87cb4
TestClient accepts backend and backend_options as arguments to constructor (#1211)
as opposed to ClassVar assignment 

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
Co-authored-by: Jordan Speicher <jordan@jspeicher.com>
Co-authored-by: Jordan Speicher <uSpike@users.noreply.github.com>
2021-06-28 21:36:13 +01:00
Thomas Grainger 906e9073a4
reset the `__eq__` and `__hash__` of HTTPConnection to allow WebSockets to be added to … (#1039) 2021-06-28 13:02:18 +01:00
Marcelo Trylesinski 0ef4418633
🔧 Add funding option (#1219) 2021-06-28 12:15:08 +02:00
Amin Alaee 070d749f66
Make Jinja2Templates.get_env private & rename (#1218)
* make-jinja2-get-env-internal

* rename _get_env to _create_env
2021-06-27 13:26:14 +01:00
Jamie Hewland 66266369bb
mkdocs: Set site_url (#1215) 2021-06-25 08:46:38 +01:00
Jamie Hewland 119c427474
Prepare version 0.15.0 (#1202)
* Prepare version 0.15.0

* Remember to add a note about websocket_connect

* Add date and blurb to release notes

* Bump version to 0.15.0

* Add note about fixing #1012
2021-06-23 09:31:02 +01:00
Aber 7ed2890146
Fixed TestClient error when response headers missing (#1200)
* Fixed https://github.com/abersheeran/asgi-ratelimit/issues/14

* lint it

* Black it

Co-authored-by: euri10 <euri10@users.noreply.github.com>
2021-06-21 10:09:19 +02:00
Jamie Hewland ab0fff9dd3
Test on Python 3.10 (#1201) 2021-06-19 18:02:53 +01:00
Jamie Hewland a839d9220d
Use coverage directly instead of pytest-cov (#1204)
* Use coverage directly instead of pytest-cov

* Use coverage's source_pkgs
2021-06-19 12:42:56 +01:00
Jamie Hewland d917501af5
Clean up last bit of aiofiles after #1157 (#1203) 2021-06-18 16:40:09 +01:00
Jordan Speicher 42592d68e5
anyio integration (#1157)
* First whack at anyio integration

* Fix formatting

* Remove debug messages

* mypy fixes

* Update README.md

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

* Fix install_requires typo

* move_on_after blocks if deadline is too small

* Linter fixes

* Improve WSGI structured concurrency

* Tests use anyio

* Checkin progress on testclient

* Prep for anyio 3

* Remove debug backend option

* Use anyio 3.0.0rc1

* Remove old style executor from GraphQLApp

* Fix extra import

* Don't cancel task scope early

* Wait for wsgi sender to finish before exiting

* Use memory object streams in websocket tests

* Test on asyncio, asyncio+uvloop, and trio

* Formatting fixes

* run_until_first_complete doesn't need a return

* Fix middleware app call

* Simplify middleware exceptions

* Use anyio for websocket test

* Set STARLETTE_TESTCLIENT_ASYNC_BACKEND in tests

* Pass async backend to portal

* Formatting fixes

* Bump anyio

* Cleanup portals and add TestClient.async_backend

* Use anyio.run_async_from_thread to send from worker thread

* Use websocket_connect as context manager

* Document changes in TestClient

* Formatting fix

* Fix websocket raises coverage

* Update to anyio 3.0.0rc3 and replace aiofiles

* Apply suggestions from code review

Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>

* Bump to require anyio 3.0.0 final

* Remove mention of aiofiles in README.md

* Pin jinja2 to releases before 3 due to DeprecationWarnings

* Add task_group as application attribute

* Remove run_until_first_complete

* Undo jinja pin

* Refactor anyio.sleep into an event

* Use one less task in test_websocket_concurrency_pattern

* Apply review suggestions

* Rename argument

* fix start_task_soon type

* fix BaseHTTPMiddleware when used without Starlette

* Testclient receive() is a non-trapping function if the response is already complete

This allows for a zero deadline when waiting for a disconnect message

* Use variable annotation for async_backend

* Update docs regarding dependency on anyio

* Use CancelScope instead of move_on_after in request.is_disconnected

* Cancel task group after returning middleware response

Add test for https://github.com/encode/starlette/issues/1022

* Add link to anyio backend options in testclient docs

* Add types-dataclasses

* Re-implement starlette.concurrency.run_until_first_complete and add a test

* Fix type on handler callable

* Apply review comments to clarify run_until_first_complete scope

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
2021-06-18 15:48:43 +01:00
laggardkernel 15761fb48e
Deduplicate failure text in CORS preflight response (#1199)
Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-06-13 16:59:17 +01:00
laggardkernel 310194ec30
Cleanup param "workers" in WSGIMiddleware (#1146)
Param "workers" in WSGIMiddleware.__init__ has not been used
since 0.6.3, which is changed in GH-164, commit 96c51c.

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-06-13 16:54:34 +01:00
Nikolay Bulatov 467eb1c212
Add compresslevel to GZipMiddleware (#1128)
* Add compresslevel to GZipMiddleware
Current default level = 9
New default level = 1

Documentation gzip: https://docs.python.org/3/library/gzip.html
Benchmarks: https://tukaani.org/lzma/benchmarks.html

Reformate code

Default compress level in gzip now = 9

* Add compresslevel to GZipMiddleware

Default level = 9

Documentation gzip: https://docs.python.org/3/library/gzip.html
Benchmarks: https://tukaani.org/lzma/benchmarks.html

Reformate code

Default compress level in gzip now = 9

Co-authored-by: Tom Christie <tom@tomchristie.com>
2021-06-11 11:55:32 +02:00
Shahriyar Rzayev f7aa776c0f
Small Pythonic code changes for datastructures.py file (#1167)
* Small Pythonic code changes for datastructures.py file

* Reverting back .gitignore changes

* reverting back newline in .gitignore

* Reverting back the changes as requested during merge review

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
2021-06-11 10:46:39 +02:00
Marcelo Trylesinski 214d1f1fe6
🐛 Fix type hint issues due to mypy upgrade to 0.902 (#1197) 2021-06-11 10:39:57 +02:00
Jordan Speicher b6f3578bb2
Updates to python 3.6+ syntax (#1189)
* Convert comment type hints to annotations

* Update to python 3.6+ syntax

* Explicitly state response type annotation

* Explicitly define queue generic type
2021-05-27 19:54:43 +01:00
Andrey Sobolev d0989bc852
Fix readme for ChannelBox package (#1188)
Fix domain name
2021-05-26 09:15:39 +01:00
Tom Christie 54e6ed38de
Update README.md (#1185)
Drop defunct "community" link, since we're just using GitHub "discussions" now.
2021-05-25 11:35:18 +01:00
Jamie Hewland d26f44b4ae
Use non-deprecated jinja2.pass_context in Jinja 3.0+ (#1183) 2021-05-13 15:11:14 +01:00
Yurii Karabas b7aeae70fd
Add better exception msg for duplicated param names (#1177) 2021-05-04 10:36:16 +01:00
hitrust 1a51dfabf7
Update authentication.md (#1165)
import 'Starlette'

Co-authored-by: Joe <nigelchiang@outlook.com>
2021-04-28 17:42:02 +08:00
Shahriyar Rzayev f58c4c22ed
Removed the empty elif clause as it has no effect (#1168)
Co-authored-by: Joe <nigelchiang@outlook.com>
2021-04-28 17:35:01 +08:00
Shahriyar Rzayev 89ab6756a2
Removed redundant parantheses as returning with comma will return tuple (#1171)
Co-authored-by: Joe <nigelchiang@outlook.com>
2021-04-28 17:28:54 +08:00
Shahriyar Rzayev f934a65ace
Replaced mutable default argument with None to eliminate anti-pattern (#1173)
Co-authored-by: Joe <nigelchiang@outlook.com>
2021-04-28 17:24:42 +08:00
Shahriyar Rzayev a4f4dff732
Replaced built-in type name with type_ as it may shadow the global scope; removed redundant variable assignment (#1174) 2021-04-28 17:20:14 +08:00
wim glenn 4750e7b613
prevent setup from generating an incorrect top_level.txt file in the wheel distribution (#1166) 2021-04-21 09:29:35 +01:00
Ben Falk f997938916
use quote instead of quote_plus for RedirectResponse location header (#1164)
* use quote instead of quote_plus for RedirectResponse location header

adjust safe characters: rem. duplicate & symbol

add test for redirect quoting

* remove unused import

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-04-14 20:14:53 +01:00
Josh Wilson 995d70c7c6
Set explicit Origin in CORS preflight response if allow_credentials is True and allow_origins is wildcard (#1113)
* Set explicit Origin in CORS preflight response if allow_credentials is True and allow_origins is wildcard

When making a preflight request, the browser makes no indication as to whether the actual subsequent
request will pass up credentials. However, unless the preflight response explicitly allows the
request's `Origin` in the `Access-Control-Response-Header`, the browser will fail the CORS check and
prevent the actual follow-up CORS request. This means that responding with the `*` wildcard is not
sufficient to allow preflighted credentialed requests. The current workaround is to provide an
equivalently permissive `allow_origin_regex` pattern.

The `simple_response()` code already performs similar logic which currently only applies to
non-preflighted requests since the browser would never make a preflighted request that hits this
code due to this issue:

```
if self.allow_all_origins and has_cookie:
    headers["Access-Control-Allow-Origin"] = origin
```

This just bring the two halves inline with each other.

* Add Vary header to preflight response if allow_credentials

* Use allow_explicit_origin() for preflight request_headers

This simplifies the code slightly by using this recently added method.

It has some trade-offs, though. We now construct a `MutableHeaders` instead of a simple `dict` when
copying the pre-computed preflight headers, and we move the `Vary` header construction out of the
pre-computation and into the call handler.

I think it makes the code more maintainable and the added per-call computation is minimal.

* Convert MutableHeaders to dict for PlainTextResponse

* Revert back to dict() for preflight headers

This also names and caches some of the boolean tests in __init__() which we use in later if-blocks.
This follows the existing pattern in order to better self-document the code.

* Clean up comments

* Remove unused self.allow_credentials attribute
2021-04-14 20:10:20 +01:00
Josh Wilson f5ecb53895
Add HEAD to CORS ALL_METHODS list (#1112)
* Add HEAD to CORS ALL_METHODS list

The HEAD method is conspicuously absent from the allowed methods list when `allow_methods="*"` is
used. This doesn't really affect CORS preflight requests, as HEAD requests aren't preflighted by the
browser, but it does prevent the actual cross-origin HEAD response from being read by the calling
app.
[This can catch people off-guard.](https://discuss.encode.io/t/for-cors-middleware-why-is-head-not-included-in-all-methods/939)

This simply adds HEAD to the `ALL_METHODS` list in the CORS middleware module and includes some
additional tests to validate the new behavior.

* Update tests/middleware/test_cors.py to use more explicit status code check

Co-authored-by: euri10 <euri10@users.noreply.github.com>

Co-authored-by: euri10 <euri10@users.noreply.github.com>
Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-04-06 21:38:48 +01:00
Josh Wilson 602212613c
Add Origin to Vary header on credentialed CORS response (#1111)
* Add Origin to Vary header on credentialed CORS response

According to the [MDN CORS docs]
(https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Access-Control-Allow-Origin), the `Origin`
item should be added to the `Vary` header when the `Access-Control-Allow-Origin` is set to an
explicit origin value, as opposed to the `*` wildcard.

>If the server specifies a single origin (that may dynamically change based on the requesting origin
as part of a white-list) rather than the "*" wildcard, then the server should also include Origin in
the Vary response header — to indicate to clients that server responses will differ based on the
value of the Origin request header.

The existing code fails to update the `Vary` list when the server is configured to allow all
origins (`*`) and the request has a `Cookie` header (ie. credentialed). In that situation, the
`Access-Control-Allow-Origin` header will be set to the request's `Origin` value.

It appears this may have just been a simple oversight in the original implementation. This updates
the code to add `Origin` to the `Vary` header under these circumstancesIf it was intentionally
omitted, I'd be delighted to learn why.

* Add type annotations

* Add test to ensure that the vary header does not contain origin if request is non-credentialed
2021-04-06 21:36:41 +01:00
oTree-org 1222e78a15
Don't use 'raise exc from None' because it suppresses exception causes (#1158)
Co-authored-by: oTree-org <chris@otree.org>
2021-03-29 16:02:22 +01:00
Mahmoud Hossam 5ee04ef9b1
Make session cookie use ASGI root path (#1147)
* Make session cookie use ASGI root path

* Check if ASGI root_path exists before using it

Co-authored-by: Tom Christie <tom@tomchristie.com>

* Remove comment

* Add test

Co-authored-by: Mahmoud Hanafy <mahmoud.hanafy@retresco.de>
Co-authored-by: Tom Christie <tom@tomchristie.com>
2021-03-12 12:49:52 +00:00
Jamie Hewland 23e15789bf
Fix docs syntax highlighting (#1136)
* mkdocs: Use python-markdown-extensions syntax highlighter

* mkdocs: Use 2-space indent consistently in config file
2021-02-07 19:06:30 +00:00
ilunev 0ac60bbeb2
StaticFiles: Fix cache validation bug for deleted files in html mode (#1023)
* StaticFiles: Fix cache validation bug for deleted files in html mode

Previously StaticFiles would return 304 for a deleted file if its
Last-Modified date was the same as that of 404.html

* Use black formatter

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
2021-02-05 20:15:25 +00:00