Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type annotations in responses.py #1711

Merged
merged 2 commits into from Jun 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions starlette/responses.py
Expand Up @@ -112,7 +112,7 @@ def set_cookie(
httponly: bool = False,
samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax",
) -> None:
cookie: http.cookies.BaseCookie = http.cookies.SimpleCookie()
cookie: "http.cookies.BaseCookie[str]" = http.cookies.SimpleCookie()
Copy link
Sponsor Member

@Kludex Kludex Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this correct? I can see it fixes the issue with pyright, but I don't understand how a single argument works, as BaseCookie is a dict 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you asking why str is the correct type? Both key and value are str (from the function signature) and str made type checker happy so pretty sure str is correct.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed on our conversation outside GitHub... It's because of the definition on typeshed: https://github.com/python/typeshed/blob/544c7c963333a4a415b951f13a6a6bbec2421161/stdlib/http/cookies.pyi#L54

Thanks @adriangb 😗

cookie[key] = value
if max_age is not None:
cookie[key]["max-age"] = max_age
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(
self,
content: typing.Any,
status_code: int = 200,
headers: typing.Optional[dict] = None,
headers: typing.Optional[typing.Dict[str, str]] = None,
media_type: typing.Optional[str] = None,
background: typing.Optional[BackgroundTask] = None,
) -> None:
Expand Down