Code Monkey home page Code Monkey logo

Comments (4)

berkerpeksag avatar berkerpeksag commented on August 17, 2024

Sounds good to me. Would you like to submit a PR?

from astor.

Kodiologist avatar Kodiologist commented on August 17, 2024

Maybe sometime. It's not a big issue for Hy.

from astor.

Kodiologist avatar Kodiologist commented on August 17, 2024

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.

berkerpeksag avatar berkerpeksag commented on August 17, 2024

@Kodiologist thank you very much, I will convert it to a pull request!

from astor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.