Skip to content
New issue

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

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

Already on GitHub? # to your account

Inconsistent name mangling in TypedDict in function and class forms #129567

Closed
sobolevn opened this issue Feb 2, 2025 · 2 comments
Closed

Inconsistent name mangling in TypedDict in function and class forms #129567

sobolevn opened this issue Feb 2, 2025 · 2 comments
Assignees
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Feb 2, 2025

Bug report

Let's say that you have a dict like {"__key": 1} and you want to type it.

You can write:

>>> import typing
>>> class A(typing.TypedDict):
...     __key: int

>>> A.__mutable_keys__
frozenset({'_A__key'})

and:

>>> B = typing.TypedDict("B", [("__key", int)])
>>> B.__mutable_keys__
frozenset({'__key'})

Note that A mangles __key as a regular name. While B does not.

I guess that it is expected, but!
Docs (https://docs.python.org/3/library/typing.html#typing.TypedDict) does not say anything about this behavior. We only mention that functional form should be used for invalid identifiers. But, __key is a valid indentifier.
We don't have explicit tests for this either.
And Typing Spec does not mention this as well: https://typing.readthedocs.io/en/latest/spec/typeddict.html

So, what we can do:

  • Do not mangle names in this case (hard and problematic: it can break existing stuff)
  • Document and test current behavior (my vote)

Please, share your thoughts on this. And I willing to send a PR with the fix.

Linked PRs

@sobolevn sobolevn added topic-typing type-bug An unexpected behavior, bug, or error labels Feb 2, 2025
@sobolevn sobolevn self-assigned this Feb 2, 2025
@picnixz
Copy link
Member

picnixz commented Feb 2, 2025

I'm +1 for documenting this and linking to the section about mangling so that users know how to write a mangling/demangling helper if needs arise (if they don't want to use the functional syntax)

sobolevn added a commit to sobolevn/graphql-core that referenced this issue Feb 16, 2025
`TypedDict` mangles private names as regular Python classes do. So, `__schema` name will become `_IntrospectionQuery__schema` instead.
See python/cpython#129567

Found during python/mypy#16715
sobolevn added a commit to sobolevn/hydra-zen that referenced this issue Feb 17, 2025
While working on python/mypy#16715 I've noticed that `_StoreCallSig` uses class form, while having `__kw` field. This field will be mangled. This is not something typing users want. See python/cpython#129567
sobolevn added a commit to sobolevn/cpython that referenced this issue Feb 17, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 4, 2025
…gling (pythonGH-130233)

(cherry picked from commit 63ffb40)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Mar 4, 2025
…ngling (GH-130233) (#130841)

gh-129567: Add a note to `typing.TypedDict` docs about name mangling (GH-130233)
(cherry picked from commit 63ffb40)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 4, 2025
@ZeroIntensity
Copy link
Member

This looks done :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants