You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for frozendict as AST Constant #150045
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
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.
Activity
JelleZijlstra commentedon May 19, 2026
Can you clarify what you need? This already works:
And I see no special casing for
frozensetin the AST module.lgeiger commentedon May 19, 2026
@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:
I updated the issue comment. I am happy to submit a PR to fix this. I think I have it mostly working.
methane commentedon May 19, 2026
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.
lgeiger commentedon Aug 19, 2026
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.