Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I write a type hint for a test Response? #2491

Open
juliancoffee opened this issue Jan 21, 2025 · 0 comments
Open

How do I write a type hint for a test Response? #2491

juliancoffee opened this issue Jan 21, 2025 · 0 comments

Comments

@juliancoffee
Copy link

Hi, first of all, thank you for this awesome project, but I have some questions.

I see there's _MonkeyPatchedWSGIResponse, but I've no idea how I'm supposed to use it

I tried

from django_stubs.test.client import _MonkeyPatchedWSGIResponse

but no luck, mypy warns of [import-not-found] for django_stubs.test.client, while the example with TypedModel from README works, so my configuration seems to be ok.

I suppose it is some kind of private type I'm not supposed to use, but what should I use instead?

that's how I'm using it, if that'll help 👀

class FormTestCase(TestCase):
    # I know that assertFormError exists, but it requires a fieldname, and that's
    # a weird limitation imo, so here's my version instead.
    def assertFormContainsErrorCode(
        self, response, error_code: str
    ):
        """Check that form errors contain the error with error_code"""
        form: Form = response.context["form"]
        errors = form.errors.get_json_data()

        # flatmap and find the code for each message for each field
        # NOTE: eagerly evaluate the iterable for better error messages, but
        # it'd work without it as well
        error_codes = list(
            map(lambda e: e["code"], chain.from_iterable(errors.values()))
        )

        self.assertIn(error_code, error_codes)
@juliancoffee juliancoffee changed the title How do I write typehint test response? How do I write a type hint for a test Response? Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant