]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix repr for MySQL SET, generic Enum
authorpetit87 <west12capri12@gmail.com>
Sat, 26 Feb 2022 21:46:32 +0000 (16:46 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 4 Mar 2022 18:01:30 +0000 (13:01 -0500)
Fixed issues in :class:`_mysql.SET` datatype as well as :class:`.Enum`
where the ``__repr__()`` method would not render all optional parameters in
the string output, impacting the use of these types in Alembic
autogenerate. Pull request for MySQL courtesy Yuki Nishimine.

Fixes: #7720
Fixes: #7789
Closes: #7772
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7772
Pull-request-sha: d58845479f497f6b2e12d7df2e9eb2d6ac22109b
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Change-Id: Idcec23eab4258511d9f32f4e3d78e511ea6021f1
(cherry picked from commit a926dea6b78c91b627f0f0b86cdc6a9279872e99)

doc/build/changelog/unreleased_14/7720_7789.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mysql/enumerated.py
lib/sqlalchemy/sql/sqltypes.py
test/dialect/mysql/test_types.py
test/sql/test_types.py

diff --git a/doc/build/changelog/unreleased_14/7720_7789.rst b/doc/build/changelog/unreleased_14/7720_7789.rst
new file mode 100644 (file)
index 0000000..5c1026e
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, sql, mysql
+    :tickets: 7720, 7789
+
+    Fixed issues in :class:`_mysql.SET` datatype as well as :class:`.Enum`
+    where the ``__repr__()`` method would not render all optional parameters in
+    the string output, impacting the use of these types in Alembic
+    autogenerate. Pull request for MySQL courtesy Yuki Nishimine.
+
index 9857a820e66a11f5e03bea6bd8b86325dd19b00f..6c9ef28ec16ae1518f197694d089d4ba72ff0863 100644 (file)
@@ -252,3 +252,12 @@ class SET(_StringType):
     def adapt(self, impltype, **kw):
         kw["retrieve_as_bitwise"] = self.retrieve_as_bitwise
         return util.constructor_copy(self, impltype, *self.values, **kw)
+
+    def __repr__(self):
+        return util.generic_repr(
+            self,
+            to_inspect=[SET, _StringType],
+            additional_kw=[
+                ("retrieve_as_bitwise", False),
+            ],
+        )
index 69cb858e509ba14c7c914b00417335d8c11a6836..726313fcf405067312e460c67b7692de8d3f9cd9 100644 (file)
@@ -1672,7 +1672,10 @@ class Enum(Emulated, String, SchemaType):
     def __repr__(self):
         return util.generic_repr(
             self,
-            additional_kw=[("native_enum", True)],
+            additional_kw=[
+                ("native_enum", True),
+                ("create_constraint", False),
+            ],
             to_inspect=[Enum, SchemaType],
         )
 
index 7bdf6f8ceb7557592ecad0cc3cbd99920986de6c..3afe6c38538175cdb073508502982703b97749dc 100644 (file)
@@ -1312,6 +1312,24 @@ class EnumSetTest(
             [("", ""), ("", ""), ("two", "two"), (None, None)],
         )
 
+    @testing.combinations(
+        (
+            [""],
+            {"retrieve_as_bitwise": True},
+            "SET('', retrieve_as_bitwise=True)",
+        ),
+        (["a"], {}, "SET('a')"),
+        (["a", "b", "c"], {}, "SET('a', 'b', 'c')"),
+        (
+            ["a", "b", "c"],
+            {"collation": "utf8_bin"},
+            "SET('a', 'b', 'c', collation='utf8_bin')",
+        ),
+        argnames="value,kw,expected",
+    )
+    def test_set_repr(self, value, kw, expected):
+        eq_(repr(mysql.SET(*value, **kw)), expected)
+
 
 def colspec(c):
     return testing.db.dialect.ddl_compiler(
index 935c2354dd0acdce14a8029e10ff9f012a3cef4d..8530b904331558c1735401244ed2a2b8ce4f215b 100644 (file)
@@ -2507,6 +2507,13 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest):
             "inherit_schema=True, native_enum=False)",
         )
 
+    def test_repr_two(self):
+        e = Enum("x", "y", name="somename", create_constraint=True)
+        eq_(
+            repr(e),
+            "Enum('x', 'y', name='somename', create_constraint=True)",
+        )
+
     def test_length_native(self):
         e = Enum("x", "y", "long", length=42)