Skip to content

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

Open
@juliancoffee

Description

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions