The cast needed to make static type checkers happy fails if django.contrib.auth is installed but there is no user attached to a request.
This can happen for a couple of reasons:
in tests (look at this repo's test suite for all the times I had to attach a fake user to the request)
in a single file Django application using with a library that requires django.contrib.auth, but in the demo you don't need or care about needing an actual user.
Moving the check to before the cast allows the function to early if there is no user attached. We also make the assumption that if the item has any permissions defined and there is no request user, that the item should be hidden.
The
cast
needed to make static type checkers happy fails ifdjango.contrib.auth
is installed but there is no user attached to a request.This can happen for a couple of reasons:
django.contrib.auth
, but in the demo you don't need or care about needing an actual user.Moving the check to before the cast allows the function to early if there is no user attached. We also make the assumption that if the item has any permissions defined and there is no request user, that the item should be hidden.