Comments (4)
Sounds good to me. Would you like to submit a PR?
from astor.
Maybe sometime. It's not a big issue for Hy.
from astor.
I've fixed this, but I don't have convenient access to my SSH keys right now, so here's a patch:
From 415c2f853cfbe99d9eec52c9f77a3de6d076d804 Mon Sep 17 00:00:00 2001
From: Kodi Arfer <[email protected]>
Date: Mon, 11 Jun 2018 10:16:32 -0700
Subject: [PATCH] Generate correct code for empty sets
---
astor/code_gen.py | 10 ++++++++--
docs/changelog.rst | 5 +++++
tests/test_code_gen.py | 13 ++++++++++++-
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/astor/code_gen.py b/astor/code_gen.py
index ecaf388..157d2cc 100644
--- a/astor/code_gen.py
+++ b/astor/code_gen.py
@@ -663,8 +663,14 @@ class SourceGenerator(ExplicitNodeVisitor):
self.comma_list(node.elts)
def visit_Set(self, node):
- with self.delimit('{}'):
- self.comma_list(node.elts)
+ if node.elts:
+ with self.delimit('{}'):
+ self.comma_list(node.elts)
+ else:
+ # If we tried to use "{}" to represent an empty set, it would be
+ # interpreted as an empty dictionary. We can't use "set()" either
+ # because the name "set" might be rebound.
+ self.write('{1}.__class__()')
def visit_Dict(self, node):
set_precedence(Precedence.Comma, *node.values)
diff --git a/docs/changelog.rst b/docs/changelog.rst
index aeeeca2..af9256e 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -42,6 +42,11 @@ Bug fixes
.. _`Issue 85`: https://github.com/berkerpeksag/astor/issues/85
.. _`Issue 100`: https://github.com/berkerpeksag/astor/issues/100
+* Improved code generation to support empty sets.
+ (Reported and fixed by Kodi Arfer in `Issue 108`_.)
+
+.. _`Issue 108`: https://github.com/berkerpeksag/astor/issues/108
+
0.6.2 - 2017-11-11
------------------
diff --git a/tests/test_code_gen.py b/tests/test_code_gen.py
index b8c1c6a..9223764 100644
--- a/tests/test_code_gen.py
+++ b/tests/test_code_gen.py
@@ -23,8 +23,11 @@ import astor
def canonical(srctxt):
return textwrap.dedent(srctxt).strip()
+def astorexpr(x):
+ return eval(astor.to_source(ast.Expression(body=x)))
+
def astornum(x):
- return eval(astor.to_source(ast.Expression(body=ast.Num(n=x))))
+ return astorexpr(ast.Num(n=x))
class Comparisons(object):
@@ -93,6 +96,14 @@ class CodegenTestCase(unittest.TestCase, Comparisons):
source = "from ..aaa import foo, bar as bar2"
self.assertSrcRoundtrips(source)
+ def test_empty_iterable_literals(self):
+ self.assertSrcRoundtrips('()')
+ self.assertSrcRoundtrips('[]')
+ self.assertSrcRoundtrips('{}')
+ # Python has no literal for empty sets, but code_gen should produce an
+ # expression that evaluates to one.
+ self.assertEquals(astorexpr(ast.Set([])), set())
+
def test_dictionary_literals(self):
source = "{'a': 1, 'b': 2}"
self.assertSrcRoundtrips(source)
--
2.17.1.windows.2
from astor.
@Kodiologist thank you very much, I will convert it to a pull request!
from astor.
Related Issues (20)
- Deleted
- 0.8.1: test suite i sfailing HOT 1
- Cannot roundtrip Python 3.9 stdlib xml/dom/minidom.py
- Uneeded triplequotes added in round trip
- 0.8.1 keeps failing with NotADirectoryError HOT 3
- Missing parentheses in dictionary unpacking HOT 1
- to_source raises AssertionError
- AttributeError: 'FunctionDef' object has no attribute 'decorator_list'
- Can't find implementation of method `SourceGenerator.get__pp` HOT 2
- Retire `astor`, as `ast.unparse` exists, and migrate it into API-compatible shims of `ast` for older versions of python HOT 3
- Example to insert python code through AST
- Python 3.10.7 is now more careful with huge integers HOT 2
- SyntaxError: invalid syntax HOT 2
- AttributeError: No defined handler for node of type Match HOT 2
- Pypi release? HOT 2
- [3.12] DeprecationWarning: ast.Num is deprecated and will be removed in Python 3.14; use ast.Constant instead
- Switch to use GitHub Actions
- why the type of variable βNEW_PARAM_JIN is wrong
- astor.to_source pretty_string HOT 3
- aster converts triple-quote comment to single-quote comment if \x00 is in comment. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from astor.