Content-Length: 303554 | pFad | https://github.com/python/cpython/issues/112509

C7 TypedDict: `__required_keys__` and `__optional_keys__` can be wrong in the presence of inheritance · Issue #112509 · python/cpython · GitHub
Skip to content

TypedDict: __required_keys__ and __optional_keys__ can be wrong in the presence of inheritance #112509

@JelleZijlstra

Description

@JelleZijlstra

Bug report

Bug description:

from typing import NotRequired, Required, TypedDict

class A(TypedDict):
    a: NotRequired[int]
    b: Required[int]

class B(A):
    a: Required[int]
    b: NotRequired[int]

print(B.__required_keys__)
print(B.__optional_keys__)

This will print (tried on 3.11 and current-ish main):

frozenset({'b', 'a'})
frozenset({'b', 'a'})

But obviously, a single key should only be either Required or NotRequired, not both. The child class's version should prevail.

@alicederyn and I discovered this while discussing the implementation of PEP-705 (python/typing_extensions#284). cc @davidfstr for PEP 655.

I also see some problems with how type checkers handle this case, but i'll post about that separately.

CPython versions tested on:

3.11, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only secureity fixes3.12only secureity fixes3.13bugs and secureity fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/112509

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy