From: Gord Thompson Date: Thu, 29 Apr 2021 18:57:06 +0000 (-0600) Subject: Fix ForeignKeyConstraint.copy() error X-Git-Tag: rel_1_4_12~3^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=416fdb1674daf72ef215c6abfed3e08343f1e05e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix ForeignKeyConstraint.copy() error 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 --- diff --git a/doc/build/changelog/unreleased_14/6353.rst b/doc/build/changelog/unreleased_14/6353.rst new file mode 100644 index 0000000000..ffeeca619a --- /dev/null +++ b/doc/build/changelog/unreleased_14/6353.rst @@ -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. diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index f2c1c86ec8..6ab58c3013 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -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( diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index ca92fb1250..727b77f7e7 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -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: diff --git a/test/sql/test_deprecations.py b/test/sql/test_deprecations.py index 1e5b1b995f..4af1f65e39 100644 --- a/test/sql/test_deprecations.py +++ b/test/sql/test_deprecations.py @@ -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):