DEV Community

Benji πŸ™
Benji πŸ™

Posted on

TIL you can trick Mypys typechecker using typing.cast

Problem

You might have something like this:

# models.py
class MyModelQuerySet(QuerySet[MyModel]):
    # ...

# views.py
class MyView():
    def get_queryset(self) -> QuerySet:
        # ...
        qs = self.model.stuff()

        # ...
        qs = need_other_stuff.get_other_stuff(queryset=qs)
Enter fullscreen mode Exit fullscreen mode

And when you run Mypy it will spit out the below error:

Incompatible types in assignment (expression has type "_QuerySet[MyModel, MyModel]", variable has type "MyModelQuerySet[MyModel]")  [assignment]
Enter fullscreen mode Exit fullscreen mode

This error points to the following lines:

# ...
qs = self.model.stuff()
# ...
qs = need_other_stuff.get_other_stuff(queryset=qs)
Enter fullscreen mode Exit fullscreen mode

You could explicitly type annotate them but then you'll meet another error

Name "qs" already defined on line x 
Enter fullscreen mode Exit fullscreen mode

The solution

Python 3.10 has the cast function, and you wrap the above examples like this:

from typing import cast
# ...
qs = cast(MyModelQuerySet, self.model.stuff())
# ...
qs = cast(MyModelQuerySet, need_other_stuff.get_other_stuff(queryset=qs))
Enter fullscreen mode Exit fullscreen mode

The cast function essentially just takes in a type as the first parameter, your value as the 2nd and simply returns the 2nd one unchanged.

# .../lib/python3.10/typing.py
def cast(typ, val):
    """Cast a value to a type.

    This returns the value unchanged.  To the type checker this
    signals that the return value has the designated type, but at
    runtime we intentionally don't check anything (we want this
    to be as fast as possible).
    """
    return val
Enter fullscreen mode Exit fullscreen mode

A word of caution:

Although doing this is a way to inform mypy your intent it doesn't guarantee that the assigned value will be of the correct type at runtime.

Top comments (0)