mirror of
https://github.com/django/django.git
synced 2025-06-03 18:49:12 +00:00
Refs #33216 -- Made @deconstructible do not change path for subclasses.
This commit is contained in:
parent
194ca77092
commit
205f67cd5b
@ -23,7 +23,7 @@ def deconstructible(*args, path=None):
|
|||||||
and keyword arguments.
|
and keyword arguments.
|
||||||
"""
|
"""
|
||||||
# Fallback version
|
# Fallback version
|
||||||
if path:
|
if path and type(obj) is klass:
|
||||||
module_name, _, name = path.rpartition('.')
|
module_name, _, name = path.rpartition('.')
|
||||||
else:
|
else:
|
||||||
module_name = obj.__module__
|
module_name = obj.__module__
|
||||||
@ -40,7 +40,9 @@ def deconstructible(*args, path=None):
|
|||||||
"https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values"
|
"https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values"
|
||||||
% (name, module_name, get_docs_version()))
|
% (name, module_name, get_docs_version()))
|
||||||
return (
|
return (
|
||||||
path or '%s.%s' % (obj.__class__.__module__, name),
|
path
|
||||||
|
if path and type(obj) is klass
|
||||||
|
else f'{obj.__class__.__module__}.{name}',
|
||||||
obj._constructor_args[0],
|
obj._constructor_args[0],
|
||||||
obj._constructor_args[1],
|
obj._constructor_args[1],
|
||||||
)
|
)
|
||||||
|
@ -19,6 +19,10 @@ class DeconstructibleWithPathClass:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DeconstructibleWithPathChildClass(DeconstructibleWithPathClass):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@deconstructible(
|
@deconstructible(
|
||||||
path='utils_tests.deconstructible_classes.DeconstructibleInvalidPathClass',
|
path='utils_tests.deconstructible_classes.DeconstructibleInvalidPathClass',
|
||||||
)
|
)
|
||||||
@ -26,6 +30,10 @@ class DeconstructibleInvalidPathClass:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DeconstructibleInvalidPathChildClass(DeconstructibleInvalidPathClass):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DeconstructibleTests(SimpleTestCase):
|
class DeconstructibleTests(SimpleTestCase):
|
||||||
def test_deconstruct(self):
|
def test_deconstruct(self):
|
||||||
obj = DeconstructibleClass('arg', key='value')
|
obj = DeconstructibleClass('arg', key='value')
|
||||||
@ -51,6 +59,16 @@ class DeconstructibleTests(SimpleTestCase):
|
|||||||
self.assertEqual(args, ('arg',))
|
self.assertEqual(args, ('arg',))
|
||||||
self.assertEqual(kwargs, {'key': 'value'})
|
self.assertEqual(kwargs, {'key': 'value'})
|
||||||
|
|
||||||
|
def test_deconstruct_child_with_path(self):
|
||||||
|
obj = DeconstructibleWithPathChildClass('arg', key='value')
|
||||||
|
path, args, kwargs = obj.deconstruct()
|
||||||
|
self.assertEqual(
|
||||||
|
path,
|
||||||
|
'utils_tests.test_deconstruct.DeconstructibleWithPathChildClass',
|
||||||
|
)
|
||||||
|
self.assertEqual(args, ('arg',))
|
||||||
|
self.assertEqual(kwargs, {'key': 'value'})
|
||||||
|
|
||||||
def test_invalid_path(self):
|
def test_invalid_path(self):
|
||||||
obj = DeconstructibleInvalidPathClass()
|
obj = DeconstructibleInvalidPathClass()
|
||||||
docs_version = get_docs_version()
|
docs_version = get_docs_version()
|
||||||
@ -66,3 +84,13 @@ class DeconstructibleTests(SimpleTestCase):
|
|||||||
)
|
)
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
obj.deconstruct()
|
obj.deconstruct()
|
||||||
|
|
||||||
|
def test_parent_invalid_path(self):
|
||||||
|
obj = DeconstructibleInvalidPathChildClass('arg', key='value')
|
||||||
|
path, args, kwargs = obj.deconstruct()
|
||||||
|
self.assertEqual(
|
||||||
|
path,
|
||||||
|
'utils_tests.test_deconstruct.DeconstructibleInvalidPathChildClass',
|
||||||
|
)
|
||||||
|
self.assertEqual(args, ('arg',))
|
||||||
|
self.assertEqual(kwargs, {'key': 'value'})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user