Skip to content

Discussion: Runtime error when overriding a mutable key with a ReadOnly one #681

@JanEricNitschke

Description

@JanEricNitschke

Follow up from here: #606 (comment)

Background:
Currently CPython emits a TypeError when a mutable key from a base TypedDict is overwritten by a ReadOnly one of a child: https://github.com/python/cpython/blob/805e3368d6d07e58430654d1365283924fdf4143/Lib/typing.py#L3151

and it has an explicit test for that behavior: https://github.com/python/cpython/blob/805e3368d6d07e58430654d1365283924fdf4143/Lib/test/test_typing.py#L9077

However typing_extensions permits and tests for that:

Originally typing_extensions had the same behaviour as CPython, however that was changed: d6c50f5

This adjustment was not taken into account when the CPython implementation was done: python/cpython#116350

Although this point was later raised in the issue (after the merge of the PR): python/cpython#116127 (comment)

My opinion:
From what i understand the TypeError in CPython for this behaviour is pretty unique, as i dont think any other such attribute is runtime enforced.

typing.Final and typing.ReadOnly even specify "There is no runtime checking for this property." in the docs. (Although that obviously refers them only being read, but still).

Additionally the typing docs in general state "The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.". Lastly, neither the docs nor the PEP say anything about such a type error existing.

So i definitely feel like the optimal thing here would be to not have a runtime error.

On top of that i feel like removing a TypeError is probably a less intrusive change than adding one, even though changes in CPython are obviously more impactful than ones in typing_extensions.

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