]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix ForeignKeyConstraint.copy() error
authorGord Thompson <gord@gordthompson.com>
Thu, 29 Apr 2021 18:57:06 +0000 (12:57 -0600)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 29 Apr 2021 20:24:14 +0000 (16:24 -0400)
Fixed an issue with the (deprecated in 1.4)
:meth:`_schema.ForeignKeyConstraint.copy` method that caused an error when
invoked with the ``schema`` argument.

Fixes: #6353
Change-Id: I03330d9ec254d64377f2b2e86af69a4eaff43ac6

doc/build/changelog/unreleased_14/6353.rst [new file with mode: 0644]
lib/sqlalchemy/sql/schema.py
lib/sqlalchemy/util/compat.py
test/sql/test_deprecations.py

diff --git a/doc/build/changelog/unreleased_14/6353.rst b/doc/build/changelog/unreleased_14/6353.rst
new file mode 100644 (file)
index 0000000..ffeeca6
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 6353
+
+    Fixed an issue with the (deprecated in 1.4)
+    :meth:`_schema.ForeignKeyConstraint.copy` method that caused an error when
+    invoked with the ``schema`` argument.
index f2c1c86ec8e30c5ce6af9be59f728f8a9d40b52c..6ab58c3013487e2247fd72f63e3319e3baee0890 100644 (file)
@@ -2138,10 +2138,10 @@ class ForeignKey(DialectKWArgs, SchemaItem):
         "The :meth:`_schema.ForeignKey.copy` method is deprecated "
         "and will be removed in a future release.",
     )
-    def copy(self, schema=None):
-        return self._copy(schema)
+    def copy(self, schema=None, **kw):
+        return self._copy(schema=schema, **kw)
 
-    def _copy(self, schema=None):
+    def _copy(self, schema=None, **kw):
         """Produce a copy of this :class:`_schema.ForeignKey` object.
 
         The new :class:`_schema.ForeignKey` will not be bound
@@ -3309,7 +3309,7 @@ class ColumnCollectionConstraint(ColumnCollectionMixin, Constraint):
         "is deprecated and will be removed in a future release.",
     )
     def copy(self, target_table=None, **kw):
-        return self._copy(target_table, **kw)
+        return self._copy(target_table=target_table, **kw)
 
     def _copy(self, target_table=None, **kw):
         # ticket #5276
@@ -3439,7 +3439,7 @@ class CheckConstraint(ColumnCollectionConstraint):
         "and will be removed in a future release.",
     )
     def copy(self, target_table=None, **kw):
-        return self._copy(target_table, **kw)
+        return self._copy(target_table=target_table, **kw)
 
     def _copy(self, target_table=None, **kw):
         if target_table is not None:
@@ -3732,7 +3732,7 @@ class ForeignKeyConstraint(ColumnCollectionConstraint):
         "and will be removed in a future release.",
     )
     def copy(self, schema=None, target_table=None, **kw):
-        return self._copy(target_table, **kw)
+        return self._copy(schema=schema, target_table=target_table, **kw)
 
     def _copy(self, schema=None, target_table=None, **kw):
         fkc = ForeignKeyConstraint(
index ca92fb12503f33a37b4b420b697c23314b00513d..727b77f7e743523b29400ef58e3620e2073a95b9 100644 (file)
@@ -228,6 +228,10 @@ if py3k:
 
     from abc import ABC
 
+    def _qualname(fn):
+        return fn.__qualname__
+
+
 else:
     import base64
     import ConfigParser as configparser  # noqa
@@ -338,6 +342,17 @@ else:
 
     TYPE_CHECKING = False
 
+    def _qualname(meth):
+        """return __qualname__ equivalent for a method on a class"""
+
+        for cls in meth.im_class.__mro__:
+            if meth.__name__ in cls.__dict__:
+                break
+        else:
+            return meth.__name__
+
+        return "%s.%s" % (cls.__name__, meth.__name__)
+
 
 if py3k:
 
index 1e5b1b995f712d8687302492416f49c1a4e943a1..4af1f65e3996db272ebe2a7fd36ce14ee302c015 100644 (file)
@@ -58,6 +58,7 @@ from sqlalchemy.testing import mock
 from sqlalchemy.testing import not_in
 from sqlalchemy.testing.schema import Column
 from sqlalchemy.testing.schema import Table
+from sqlalchemy.util import compat
 from .test_update import _UpdateFromTestBase
 
 
@@ -194,6 +195,28 @@ class DeprecationWarningsTest(fixtures.TestBase, AssertsCompiledSQL):
         ):
             self.assert_compile(or_(and_()), "")
 
+    @testing.combinations(
+        (schema.Column),
+        (schema.UniqueConstraint,),
+        (schema.PrimaryKeyConstraint,),
+        (schema.CheckConstraint,),
+        (schema.ForeignKeyConstraint,),
+        (schema.ForeignKey,),
+        (schema.Identity,),
+    )
+    def test_copy_dep_warning(self, cls):
+        obj = cls.__new__(cls)
+        with mock.patch.object(cls, "_copy") as _copy:
+            with testing.expect_deprecated(
+                r"The %s\(\) method is deprecated" % compat._qualname(cls.copy)
+            ):
+                obj.copy(schema="s", target_table="tt", arbitrary="arb")
+
+        eq_(
+            _copy.mock_calls,
+            [mock.call(target_table="tt", schema="s", arbitrary="arb")],
+        )
+
 
 class ConvertUnicodeDeprecationTest(fixtures.TestBase):