-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from danthedeckie/dictcomp-support
Dictcomp support (rebased #133)
- Loading branch information
Showing
2 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
- edgarrmondragon (Edgar Ramírez-Mondragón) Address Python 3.12+ deprecation warnings | ||
- cedk (Cédric Krier) <[email protected]> Allow running tests with Werror | ||
- decorator-factory <[email protected]> More security fixes | ||
- lkruitwagen (Lucas Kruitwagen) Adding support for dict comprehensions | ||
------------------------------------- | ||
Basic Usage: | ||
|
@@ -661,6 +662,7 @@ def __init__(self, operators=None, functions=None, names=None): | |
ast.Set: self._eval_set, | ||
ast.ListComp: self._eval_comprehension, | ||
ast.GeneratorExp: self._eval_comprehension, | ||
ast.DictComp: self._eval_comprehension, | ||
} | ||
) | ||
|
||
|
@@ -699,7 +701,10 @@ def _eval_set(self, node): | |
return set(self._eval(x) for x in node.elts) | ||
|
||
def _eval_comprehension(self, node): | ||
to_return = [] | ||
if isinstance(node, ast.DictComp): | ||
to_return = {} | ||
else: | ||
to_return = [] | ||
|
||
extra_names = {} | ||
|
||
|
@@ -738,7 +743,10 @@ def do_generator(gi=0): | |
if len(node.generators) > gi + 1: | ||
do_generator(gi + 1) | ||
else: | ||
to_return.append(self._eval(node.elt)) | ||
if isinstance(to_return, dict): | ||
to_return[self._eval(node.key)] = self._eval(node.value) | ||
elif isinstance(to_return, list): | ||
to_return.append(self._eval(node.elt)) | ||
|
||
try: | ||
do_generator() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters