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

TypedDict(...) as function does not respect "total" when setting __required_keys__ and __optional_keys__ #86225

Closed
agronholm mannequin opened this issue Oct 17, 2020 · 9 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@agronholm
Copy link
Mannequin

agronholm mannequin commented Oct 17, 2020

BPO 42059
Nosy @gvanrossum, @agronholm, @ilevkivskyi, @miss-islington, @brandtbucher, @domdfcoding
PRs
  • bpo-42059: Fixed required/optional keys with old-style TypedDict #22736
  • [3.9] bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) #23747
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-12-15.00:08:30.506>
    created_at = <Date 2020-10-17.09:23:29.914>
    labels = ['type-bug', 'library', '3.9']
    title = 'TypedDict(...) as function does not respect "total" when setting __required_keys__ and __optional_keys__'
    updated_at = <Date 2021-04-12.17:17:21.534>
    user = 'https://github.com/agronholm'

    bugs.python.org fields:

    activity = <Date 2021-04-12.17:17:21.534>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-15.00:08:30.506>
    closer = 'brandtbucher'
    components = ['Library (Lib)']
    creation = <Date 2020-10-17.09:23:29.914>
    creator = 'alex.gronholm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42059
    keywords = ['patch']
    message_count = 9.0
    messages = ['378805', '378819', '378820', '382702', '382851', '383010', '387885', '390822', '390870']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'alex.gronholm', 'levkivskyi', 'miss-islington', 'brandtbucher', 'domdfcoding']
    pr_nums = ['22736', '23747']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42059'
    versions = ['Python 3.9']

    @agronholm
    Copy link
    Mannequin Author

    agronholm mannequin commented Oct 17, 2020

    >>> DummyDict = TypedDict('DummyDict', {'x': int, 'y': str}, total=False)
    >>> DummyDict.__required_keys__
    frozenset({'x', 'y'})

    This happens because the TypedDict function does not pass the "total" metaclass argument to _TypedDictMeta() (instead passing "__total__" in the attribute namespace) and the new code that sets __required_keys__ and __optional_keys__ only checks the metaclass argument.

    @agronholm agronholm mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 17, 2020
    @gvanrossum
    Copy link
    Member

    Can you submit a PR to fix this? It looks like you already have a good understanding of the root cause of the problem, so it should be easy to fix. (Be sure to add a unit test.)

    It can be backported to earlier Python versions that have typing.TypedDict (3.8+).

    @gvanrossum
    Copy link
    Member

    Whoops, sorry, didn't see that you already have a PR. I'll review it next week during the core sprint.

    @gvanrossum
    Copy link
    Member

    Looking for another reviewer.

    @brandtbucher
    Copy link
    Member

    New changeset 67b769f by Alex Grönholm in branch 'master':
    bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736)
    67b769f

    @brandtbucher
    Copy link
    Member

    New changeset dbb0006 by Miss Islington (bot) in branch '3.9':
    bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) (GH-23747)
    dbb0006

    @gvanrossum
    Copy link
    Member

    We're looking for a volunteer to port this code to typing_extensions:
    python/typing#761

    @domdfcoding
    Copy link
    Mannequin

    domdfcoding mannequin commented Apr 12, 2021

    I have backported this to typing_extensions in python/typing#778

    On Python 3.9.2 and above typing.TypedDict is used, otherwise it's typing_extensions.TypedDict.

    @gvanrossum
    Copy link
    Member

    Dominic, thanks so much for the backport!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants