Skip to content

http.cookies.Morsel type for flags should allow for bool #3059

Closed

Description

@FlorianLudwig

The type declaration for http.cookies.Morsel is based on Dict[str, str] and therefor the following code is treated as a type error:

m = http.cookies.Morsel()
m['httponly'] = True

The implementation explicitly allows for max-age and expires to be integers as well:
https://github.com/python/cpython/blob/159ae24895272dce5fd53dd8e54809743e4f394f/Lib/http/cookies.py#L407

I would have liked to directly open a pull request but I am not sure what the right way to fix it is:

  1. Generally all attributes can be set to any type as they are cast to strings anyway so Dict[str, any] does reflect the implementation fine.
  2. A TypedDict defining all possible keys with their intended options would have to be somewhat opinionated, for example m['path']=1 is okay from the implementation point of view of Morsel but probably is not intended.

Activity

  1. asvetlov commented on Jun 15, 2019

    @asvetlov
    Contributor

    I think TypedDict is better here: the only limited set of key names is supported by Morsel

  2. FlorianLudwig commented on Jun 16, 2019

    @FlorianLudwig
    ContributorAuthor

    I just realized that TypedDict is in mypy_extensions which is not used within typeshed. With PEP 589 in python 3.8 it comes to the stdlib.

    So for now Dict[str, any] seems the way to go.

  3. added 3 commits that reference this issue on Jun 16, 2019
    71a5b09
    b7ff17e
    95a5b72
  4. added a commit that references this issue on Jun 19, 2019
    2e10326
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions