generated from daniil-berg/boilerplate-py
🚧 Expand generic utility mixin to handle up to 5 type arguments
This commit is contained in:
@ -12,7 +12,11 @@ class GenericInsightMixinTestCase(TestCase):
|
||||
mock_super.return_value = MagicMock(__init_subclass__=mock_super_meth)
|
||||
|
||||
# Should be `None` by default:
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg) # type: ignore[misc]
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg_0) # type: ignore[misc]
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg_1) # type: ignore[misc]
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg_2) # type: ignore[misc]
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg_3) # type: ignore[misc]
|
||||
self.assertIsNone(_util.GenericInsightMixin._type_arg_4) # type: ignore[misc]
|
||||
|
||||
# If the mixin type argument was not specified (still generic),
|
||||
# ensure that the attribute remains `None` on the subclass:
|
||||
@ -24,30 +28,55 @@ class GenericInsightMixinTestCase(TestCase):
|
||||
class Bar(Generic[t]):
|
||||
pass
|
||||
|
||||
class TestSchema1(Bar[str], _util.GenericInsightMixin[t]):
|
||||
class TestCls(Bar[str], _util.GenericInsightMixin[t, None, int, str, bool]):
|
||||
pass
|
||||
|
||||
self.assertIsNone(TestSchema1._type_arg) # type: ignore[misc]
|
||||
self.assertIsNone(TestCls._type_arg_0) # type: ignore[misc]
|
||||
self.assertIsNone(TestCls._type_arg_1) # type: ignore[misc]
|
||||
self.assertIs(int, TestCls._type_arg_2) # type: ignore[misc]
|
||||
self.assertIs(str, TestCls._type_arg_3) # type: ignore[misc]
|
||||
self.assertIs(bool, TestCls._type_arg_4) # type: ignore[misc]
|
||||
mock_super.assert_called_once()
|
||||
mock_super_meth.assert_called_once_with()
|
||||
|
||||
mock_super.reset_mock()
|
||||
mock_super_meth.reset_mock()
|
||||
|
||||
# If the mixin type argument was specified,
|
||||
# ensure it was assigned to the attribute on the child class:
|
||||
# If the mixin type arguments were omitted,
|
||||
# ensure the attributes remained `None`:
|
||||
|
||||
class TestSchema2(Bar[str], _util.GenericInsightMixin[Foo]):
|
||||
class UnspecifiedCls(_util.GenericInsightMixin): # type: ignore[type-arg]
|
||||
pass
|
||||
|
||||
self.assertIs(Foo, TestSchema2._type_arg) # type: ignore[misc]
|
||||
self.assertIsNone(UnspecifiedCls._type_arg_0) # type: ignore[misc]
|
||||
self.assertIsNone(UnspecifiedCls._type_arg_1) # type: ignore[misc]
|
||||
self.assertIsNone(UnspecifiedCls._type_arg_2) # type: ignore[misc]
|
||||
self.assertIsNone(UnspecifiedCls._type_arg_3) # type: ignore[misc]
|
||||
self.assertIsNone(UnspecifiedCls._type_arg_4) # type: ignore[misc]
|
||||
mock_super.assert_called_once()
|
||||
mock_super_meth.assert_called_once_with()
|
||||
|
||||
def test__get_type_arg(self) -> None:
|
||||
with self.assertRaises(AttributeError):
|
||||
_util.GenericInsightMixin._get_type_arg()
|
||||
_util.GenericInsightMixin._get_type_arg(0)
|
||||
|
||||
_type = object()
|
||||
with patch.object(_util.GenericInsightMixin, "_type_arg", new=_type):
|
||||
self.assertIs(_type, _util.GenericInsightMixin._get_type_arg())
|
||||
_type_0 = object()
|
||||
_type_1 = object()
|
||||
_type_2 = object()
|
||||
_type_3 = object()
|
||||
_type_4 = object()
|
||||
with patch.multiple(
|
||||
_util.GenericInsightMixin,
|
||||
_type_arg_0=_type_0,
|
||||
_type_arg_1=_type_1,
|
||||
_type_arg_2=_type_2,
|
||||
_type_arg_3=_type_3,
|
||||
_type_arg_4=_type_4,
|
||||
):
|
||||
self.assertIs(_type_0, _util.GenericInsightMixin._get_type_arg(0))
|
||||
self.assertIs(_type_1, _util.GenericInsightMixin._get_type_arg(1))
|
||||
self.assertIs(_type_2, _util.GenericInsightMixin._get_type_arg(2))
|
||||
self.assertIs(_type_3, _util.GenericInsightMixin._get_type_arg(3))
|
||||
self.assertIs(_type_4, _util.GenericInsightMixin._get_type_arg(4))
|
||||
with self.assertRaises(ValueError):
|
||||
_util.GenericInsightMixin._get_type_arg(5) # type: ignore[call-overload]
|
||||
|
@ -17,7 +17,7 @@ class GenericSchemaTestCase(TestCase):
|
||||
# Explicit annotation to possibly catch mypy errors:
|
||||
output: Foo = schema_obj.instantiate(mock_data)
|
||||
self.assertIs(mock_cls.return_value, output)
|
||||
mock__get_type_arg.assert_called_once_with()
|
||||
mock__get_type_arg.assert_called_once_with(0)
|
||||
mock_cls.assert_called_once_with(**mock_data)
|
||||
|
||||
def test_load_and_loads(self) -> None:
|
||||
|
Reference in New Issue
Block a user