From 29048b826baa88001fb1fcc640b3ec6f03bad8b3 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 15 Apr 2022 20:18:23 +0200 Subject: [PATCH 1/3] Annotate `samesite` parameter on `set_cookie` --- starlette/middleware/sessions.py | 2 +- starlette/responses.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/starlette/middleware/sessions.py b/starlette/middleware/sessions.py index 597de38a2..74e57352f 100644 --- a/starlette/middleware/sessions.py +++ b/starlette/middleware/sessions.py @@ -18,7 +18,7 @@ def __init__( session_cookie: str = "session", max_age: typing.Optional[int] = 14 * 24 * 60 * 60, # 14 days, in seconds path: str = "/", - same_site: str = "lax", + same_site: typing.Literal["lax", "strict", "none"] = "lax", https_only: bool = False, ) -> None: self.app = app diff --git a/starlette/responses.py b/starlette/responses.py index b33bdd713..e8aaf3f53 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -103,7 +103,7 @@ def set_cookie( domain: str = None, secure: bool = False, httponly: bool = False, - samesite: str = "lax", + samesite: typing.Optional[typing.Literal["lax", "strict", "none"]] = "lax", ) -> None: cookie: http.cookies.BaseCookie = http.cookies.SimpleCookie() cookie[key] = value @@ -136,7 +136,7 @@ def delete_cookie( domain: str = None, secure: bool = False, httponly: bool = False, - samesite: str = "lax", + samesite: typing.Optional[typing.Literal["lax", "strict", "none"]] = "lax", ) -> None: self.set_cookie( key, From 3ba1040581f4604965932f1e78980a661276c422 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 24 Apr 2022 06:54:29 +0200 Subject: [PATCH 2/3] Add version conditional --- starlette/middleware/sessions.py | 8 +++++++- starlette/responses.py | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/starlette/middleware/sessions.py b/starlette/middleware/sessions.py index 74e57352f..b1e32ec16 100644 --- a/starlette/middleware/sessions.py +++ b/starlette/middleware/sessions.py @@ -1,4 +1,5 @@ import json +import sys import typing from base64 import b64decode, b64encode @@ -9,6 +10,11 @@ from starlette.requests import HTTPConnection from starlette.types import ASGIApp, Message, Receive, Scope, Send +if sys.version_info >= (3, 8): # pragma: no cover + from typing import Literal +else: # pragma: no cover + from typing_extensions import Literal + class SessionMiddleware: def __init__( @@ -18,7 +24,7 @@ def __init__( session_cookie: str = "session", max_age: typing.Optional[int] = 14 * 24 * 60 * 60, # 14 days, in seconds path: str = "/", - same_site: typing.Literal["lax", "strict", "none"] = "lax", + same_site: Literal["lax", "strict", "none"] = "lax", https_only: bool = False, ) -> None: self.app = app diff --git a/starlette/responses.py b/starlette/responses.py index e8aaf3f53..c122b0981 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -17,6 +17,11 @@ from starlette.datastructures import URL, MutableHeaders from starlette.types import Receive, Scope, Send +if sys.version_info >= (3, 8): # pragma: no cover + from typing import Literal +else: + from typing_extensions import Literal + # Workaround for adding samesite support to pre 3.8 python http.cookies.Morsel._reserved["samesite"] = "SameSite" # type: ignore @@ -103,7 +108,7 @@ def set_cookie( domain: str = None, secure: bool = False, httponly: bool = False, - samesite: typing.Optional[typing.Literal["lax", "strict", "none"]] = "lax", + samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax", ) -> None: cookie: http.cookies.BaseCookie = http.cookies.SimpleCookie() cookie[key] = value @@ -136,7 +141,7 @@ def delete_cookie( domain: str = None, secure: bool = False, httponly: bool = False, - samesite: typing.Optional[typing.Literal["lax", "strict", "none"]] = "lax", + samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax", ) -> None: self.set_cookie( key, From 7a1dbc74130c8d1cc955afcda4515aace773fdd7 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 24 Apr 2022 07:21:02 +0200 Subject: [PATCH 3/3] Add pragma --- starlette/responses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starlette/responses.py b/starlette/responses.py index 5ba4ee5e6..98c8caf1b 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -19,7 +19,7 @@ if sys.version_info >= (3, 8): # pragma: no cover from typing import Literal -else: +else: # pragma: no cover from typing_extensions import Literal # Workaround for adding samesite support to pre 3.8 python