Skip to content

Add support for frozendict as AST Constant #150045

Description

@lgeiger

Currently the AST doesn't allow frozendict as constants when compiled:

import ast
node = ast.Expression(body=ast.Constant(value=frozendict({"a": 1})))
compile(ast.fix_missing_locations(node), "<string>", "eval")
Traceback (most recent call last):
  File "<python-input-0>", line 4, in <module>
    compile(ast.fix_missing_locations(node), "<string>", "eval")
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: got an invalid type in Constant: frozendict

This is implemented for frozenset

node = ast.Expression(body=ast.Constant(value=frozenset({"a", "b"})))
compile(ast.fix_missing_locations(node), "<string>", "eval")
<code object <module> at 0x1067203b0, file "<string>", line 1>

so it makes sense to do the same for frozendict.

Linked PRs

Activity

  1. JelleZijlstra commented on May 19, 2026

    @JelleZijlstra
    Member

    Can you clarify what you need? This already works:

    >>> import ast
    >>> ast.Constant(frozendict({"a": 1, "b": 1}))
    ... 
    Constant(value=frozendict({'a': 1, 'b': 1}), kind=None)
    >>> ast.unparse(_)
    "frozendict({'a': 1, 'b': 1})"
    

    And I see no special casing for frozenset in the AST module.

  2. lgeiger commented on May 19, 2026

    @lgeiger
    ContributorAuthor

    @JelleZijlstra sorry for the incomplete issue. I hit cmd-enter accidentally but you where faster than I could fix it 🚀.

    Indeed defining a frozendict constant works. However it fails when compiling:

    import ast
    node = ast.Expression(body=ast.Constant(value=frozendict({"a": 1})))
    compile(ast.fix_missing_locations(node), "<string>", "eval")

    I updated the issue comment. I am happy to submit a PR to fix this. I think I have it mostly working.

  3. methane commented on May 19, 2026

    @methane
    Member

    In the case of frozenset, we use frozenset to optimize code like if x in {"spam", "ham", "egg"}.

    But would this kind of optimization really be useful for frozendict?
    Or is there some other reason for frozendict to be supported by the AST?

    I am opposed to adding features that are not used just for the sake of symmetry with frozenset.

  4. added
    pendingThe issue will be closed if no feedback is provided
    on May 19, 2026
  5. lgeiger commented on Aug 19, 2026

    @lgeiger
    ContributorAuthor

    I had another look at this, and indeed the additional optimisations are not really useful. I didn't realise that CPython itself won't actually create frozendicts as ast constants on its own which indeed doesn't make this a very useful optimisation. 37d8189 might still be useful in the future if having the ability to compile a frozendict ast constant similar to frozenset is desired. Closing this issue for now. Sorry about the noise.

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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions