This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.literal_eval does not handle empty set literals
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Filip Haglund, ahrvoje, ezio.melotti, r.david.murray, yota moteuchi
Priority: normal Keywords:

Created on 2015-07-18 17:28 by Filip Haglund, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg246906 - (view) Author: Filip Haglund (Filip Haglund) Date: 2015-07-18 17:28
ast.literal_eval handles sets, if they contain at least one value, but does not handle empty ones, which are represented as `set()` since `{}` is already used by dicts.
msg246908 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2015-07-18 17:34
I believe this is by design, since set() -- like str(), list(), dict(), etc -- is not a literal.
I don't think set() should be special-cased either.
Perhaps you could tell us more about your use case?
msg246909 - (view) Author: Filip Haglund (Filip Haglund) Date: 2015-07-18 17:37
Okey, then this is not a bug. I can just handle this special case myself. Thanks!
msg254016 - (view) Author: Hrvoje Abraham (ahrvoje) Date: 2015-11-03 20:58
I believe this is the issue. ast.literal_eval sets support can not be considered complete until it also handles empty sets. I do not consider it valid for me to explicitly handle this case in my projects using some weird hacks.

Python community settled on not introducing empty set literal because one can simply use set(), as discussed a few times before. But no corresponding alternative was defined for ast.literal_eval, maybe it should handle 'set()' as such.

I strongly advise reopening this issue and finding functional solution for empty set case. I need it!
msg254018 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-03 21:29
You haven't presented an actual use case, it would be interesting to see one.

Regardless of that, however, ast.literal_eval is an exposure of the literal value parsing part of the AST.  set() is not part of that, because it is not a literal, it is a function call.

There has been some discussion of adding a "safe_eval" function to the stdlib (see issue 22525), and that would be the appropriate place to introduce evaluation of set().

python-ideas would be the appropriate place to discuss safe_eval.
msg254022 - (view) Author: Hrvoje Abraham (ahrvoje) Date: 2015-11-03 21:48
I use communication protocol based on Python literals and ast.literal_eval for deserialization. I'm avoiding sets because empty set value is not supported in a clean consistent manner on language level.

If I write repr(set()) i get 'set()', this should matter, maybe.

I know I can handle it as a special case, None for empty set, and close my eyes on fact that I lose None value reserved for some other cases. But this really is not a nice thing to do. Please reconsider...
msg254024 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-03 21:57
Like I said, you should take your use case to python-ideas, for a safe_eval that would handle it.  literal_eval is not the place for handing 'set()', in my opinion.  (There is always a chance other other devs will disagree, but none have so far :).

FYI, reprs are often evalable to get back the original value, but the key word there is 'eval', not 'literal_eval'.
msg254026 - (view) Author: Hrvoje Abraham (ahrvoje) Date: 2015-11-03 22:20
For now I'll implement my_literal_eval via AST filtering, reproducing ast.literal_eval + 'set()' functionality.
msg260941 - (view) Author: yota moteuchi (yota moteuchi) Date: 2016-02-27 14:57
Well, I would disagree with R. David Murray on this.

literal_eval() is meant to safely parse literal pythons "containers" structures.

{1, 2, 3} is a valid literal set(), why would an empty one not be parse-able as well. I can not predict, when I dump the structure with repr(), if the set() will be empty or not.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68851
2016-02-27 14:57:38yota moteuchisetnosy: + yota moteuchi
messages: + msg260941
2015-11-03 22:20:10ahrvojesetmessages: + msg254026
2015-11-03 21:57:33r.david.murraysetmessages: + msg254024
2015-11-03 21:48:27ahrvojesetmessages: + msg254022
2015-11-03 21:29:39r.david.murraysetnosy: + r.david.murray
messages: + msg254018
2015-11-03 20:58:30ahrvojesetversions: + Python 3.5, - Python 3.4
nosy: + ahrvoje

messages: + msg254016

type: enhancement
2015-07-18 17:40:12ezio.melottisetstage: resolved
2015-07-18 17:37:53Filip Haglundsetstatus: pending -> closed

messages: + msg246909
2015-07-18 17:34:53ezio.melottisetstatus: open -> pending

nosy: + ezio.melotti
messages: + msg246908

resolution: not a bug
2015-07-18 17:28:16Filip Haglundcreate