Skip to content
Discussion options

You must be logged in to vote

Your diagnosis holds up. The class attribute carries no annotation at all, so the declared type is inferred purely from the assignment:

>>> [l.strip() for l in inspect.getsource(GenericAPIView).splitlines() if l.strip().startswith("queryset")][0]
'queryset = None'
>>> GenericAPIView.__annotations__
{}

With queryset inferred as None, assert self.queryset is not None narrows it to Never, the assert is treated as always failing, and everything after it — including get_object() — is flagged unreachable. Adding QuerySet | None fixes it because it gives the narrowing something to survive into, which is why your workaround works.

DRF doesn't ship inline types or a py.typed marker, so this isn't …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by browniebroke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants