IIUC, Constant can't get object value according documentation:
A constant value. The value attribute of the Constant literal contains the Python object it represents. The values represented can be instances of str, bytes, int, float, complex, and bool, and the constants None and Ellipsis.
I'm not sure it's relevant. The docs don't tell what happens when the value is not from the above list. Should it raise an exception? Or blow up a nuke? E.g. ast.Expr docs says:
I think this is a GIGO case. ast.literal_eval() works for string argument. It works if the string was parsed to AST by ast.parse(). It does not work for arbitrary invalid AST, this is not its purpose. You can make compile() to fail by simply setting invalid lineno -- ast.literal_eval() ignores lineno.
I'm actually in favor of using the new isinstance checks (or type(x) in (...)). This isn't working as expected, but the real question is: Should we validate the input in the ast.literal_eval? In my opinion, yes.
Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.
[...]
I think we shouldn't document odd stdlib behavior. If there is no consensus on disallowing broken ast trees in all cases - lets just reject this bugreport.
Activity
skirpichev commented on Nov 20, 2025
CC @Eclips4
sergey-miryanov commented on Nov 20, 2025
IIUC, Constant can't get object value according documentation:
skirpichev commented on Nov 20, 2025
I'm not sure it's relevant. The docs don't tell what happens when the value is not from the above list. Should it raise an exception? Or blow up a nuke? E.g. ast.Expr docs says:
But behold:
My argument was that the literal_eval() has such validation, but only partially.
sergey-miryanov commented on Nov 20, 2025
Augh, I just noticed a difference in the first example - literal_eval works, but compile doesn't.
Sorry.
serhiy-storchaka commented on Nov 20, 2025
I think this is a GIGO case. ast.literal_eval() works for string argument. It works if the string was parsed to AST by ast.parse(). It does not work for arbitrary invalid AST, this is not its purpose. You can make compile() to fail by simply setting invalid lineno -- ast.literal_eval() ignores lineno.
I propose to close this issue as "won't fix".
skirpichev commented on Nov 20, 2025
Not only. But I realize, it's major case.
Nop.
(Though, literal_eval() also reject string '1j+1', this looks consistent.)
That's why I did fix_missing_locations() in my initial examples.
Then it should reject invalid input, not silently accept it. We do this for some cases. Why not do it for all?
BTW, the fix is trivial:
picnixz commented on Nov 22, 2025
Instead of importing type, I suggest caching type(None) and type(...) as global variables.
Eclips4 commented on Nov 22, 2025
I'm actually in favor of using the new isinstance checks (or type(x) in (...)). This isn't working as expected, but the real question is: Should we validate the input in the ast.literal_eval? In my opinion, yes.
skirpichev commented on Nov 22, 2025
I hope someone will finish patch. Fits to "easy" camp for me.
21 remaining items
skirpichev commented on Dec 25, 2025
Well, bad news - no one from core devs support that it's an issue :( I'll close opened pr's. Reclassified as docs issue.
Eclips4 commented on Dec 25, 2025
@skirpichev I do think that’s an issue :) But unfortunately, it doesn’t seem like we’ve reached a consensus.
skirpichev commented on May 26, 2026
It seems, doing so break some (wrong) assumptions of the stdlib modules as well. See the current PR.
But we have wrong documentation:
@serhiy-storchaka, @JelleZijlstra, are you suggesting to keep it as is or change? If so, how?
skirpichev commented on Aug 18, 2026
I think we shouldn't document odd stdlib behavior. If there is no consensus on disallowing broken ast trees in all cases - lets just reject this bugreport.