From 178667e91bdc6a824ffffaff3239c9bd0cab1bca Mon Sep 17 00:00:00 2001 From: SKairinos Date: Tue, 6 Feb 2024 09:54:08 +0000 Subject: [PATCH] type hints --- codeforlife/tests/model_serializer.py | 22 +++++++++++-------- .../tests/auth/password_validators/base.py | 18 +++++++-------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/codeforlife/tests/model_serializer.py b/codeforlife/tests/model_serializer.py index c3fa41ba..67d26f3d 100644 --- a/codeforlife/tests/model_serializer.py +++ b/codeforlife/tests/model_serializer.py @@ -6,6 +6,7 @@ """ import typing as t +from unittest.case import _AssertRaisesContext from django.db.models import Model from django.forms.models import model_to_dict @@ -59,23 +60,26 @@ def assert_raises_validation_error(self, code: str, *args, **kwargs): The assert-raises context which will auto-assert the code. """ - context = self.assertRaises(ValidationError, *args, **kwargs) - - class ContextWrapper: + class Wrapper: """Wrap context to assert code on exit.""" - def __init__(self, context): - self.context = context + def __init__(self, ctx: "_AssertRaisesContext[ValidationError]"): + self.ctx = ctx def __enter__(self, *args, **kwargs): - return self.context.__enter__(*args, **kwargs) + return self.ctx.__enter__(*args, **kwargs) def __exit__(self, *args, **kwargs): - value = self.context.__exit__(*args, **kwargs) - assert self.context.exception.detail[0].code == code + value = self.ctx.__exit__(*args, **kwargs) + assert ( + code + == self.ctx.exception.detail[ # type: ignore[union-attr] + 0 # type: ignore[index] + ].code + ) return value - return ContextWrapper(context) + return Wrapper(self.assertRaises(ValidationError, *args, **kwargs)) # pylint: disable-next=too-many-arguments def _assert_validate( diff --git a/codeforlife/user/tests/auth/password_validators/base.py b/codeforlife/user/tests/auth/password_validators/base.py index 27bbb466..94d141d1 100644 --- a/codeforlife/user/tests/auth/password_validators/base.py +++ b/codeforlife/user/tests/auth/password_validators/base.py @@ -5,6 +5,8 @@ Base test case for all password validators. """ +from unittest.case import _AssertRaisesContext + from django.core.exceptions import ValidationError from django.test import TestCase @@ -22,20 +24,18 @@ def assert_raises_validation_error(self, code: str, *args, **kwargs): The assert-raises context which will auto-assert the code. """ - context = self.assertRaises(ValidationError, *args, **kwargs) - - class ContextWrapper: + class Wrapper: """Wrap context to assert code on exit.""" - def __init__(self, context): - self.context = context + def __init__(self, ctx: "_AssertRaisesContext[ValidationError]"): + self.ctx = ctx def __enter__(self, *args, **kwargs): - return self.context.__enter__(*args, **kwargs) + return self.ctx.__enter__(*args, **kwargs) def __exit__(self, *args, **kwargs): - value = self.context.__exit__(*args, **kwargs) - assert self.context.exception.code == code + value = self.ctx.__exit__(*args, **kwargs) + assert self.ctx.exception.code == code return value - return ContextWrapper(context) + return Wrapper(self.assertRaises(ValidationError, *args, **kwargs))