GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for `frozendict` as AST `Constant` · Issue #150045 · python/cpython

Add support for frozendict as AST Constant #150045

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
Add support for frozendict as AST Constant#150045
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-featureA feature request or enhancementA feature request or enhancement

Description

@lgeiger
lgeiger
opened on May 19, 2026
Issue body actions

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

  • gh-150045: Add frozendict support to the compiler and AST constants #150050

Activity

  1. JelleZijlstra commented on May 19, 2026

    @JelleZijlstra
    JelleZijlstra
    on May 19, 2026
    Member
    More actions

    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.

    Reacted by Lukas Geiger
    Reacted by Lukas Geiger
  2. lgeiger commented on May 19, 2026

    @lgeiger
    lgeiger
    on May 19, 2026
    ContributorAuthor
    More actions

    @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. aisk
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 19, 2026
  4. StanFromIreland
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on May 19, 2026
  5. methane commented on May 19, 2026

    @methane
    methane
    on May 19, 2026
    Member
    More actions

    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.

    Reacted by Jelle Zijlstra, Peter Bierma, Bénédikt Tran, Kirill Podoprigora and Bartosz Sławecki
  6. picnixz
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on May 19, 2026
  7. serhiy-storchaka
    added this to Inspect issues 🔍on Jul 3, 2026
  8. lgeiger commented on Aug 19, 2026

    @lgeiger
    lgeiger
    on Aug 19, 2026
    ContributorAuthor
    More actions

    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.

  9. lgeiger
    closed this as completedon Aug 19, 2026
  10. github-project-automation
    moved this to Done in Inspect issues 🔍on Aug 19, 2026
  11. StanFromIreland
    closed this as not plannedon Aug 19, 2026
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)(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Inspect issues 🔍
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.