#3192·sanic

Request.form and Request.get_form() declare return type that includes None yet cannot return None

Author: davidfstrCreated Sep 18, 2026Updated Sep 24, 2026
Labelsbug

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After upgrading from Sanic 22.12.0 -> 24.12.1, code like the following has a typechecker error under mypy:

python
@app.post('/code/turn_in_web')
async def code_turn_in_web(request: Request) -> HTTPResponse:
    # ERROR: Item "None" of "RequestParameters | None" has no attribute "get"  [union-attr]
    user_project_id = int(request.form.get('user_project_pk'))
    ...

Cause is that Request.form (and the related Request.get_form()) are declared as potentially returning None:

python
# sanic/request/types.py
class Request(Generic[sanic_type, ctx_type]):
    ...
    def get_form(
        self, keep_blank_values: bool = False
    ) -> Optional[RequestParameters]:  #  is Optional!
        ...

    @property
    def form(self) -> Optional[RequestParameters]:  #  is Optional!
        if self.parsed_form is None:
            self.get_form()
        return self.parsed_form

However if you read the code of get_form() you'll see that it will never return None. At worst it will return an empty RequestParameters().

Similarly if you read the form property and recognize that get_form() always sets self.parsed_form to a non-None value, you'll see that it will never return None.

Issue persists in the latest Sanic v25.12.1 (published 2026-05-31).

Code snippet

Code that typechecks clean under Sanic 22 but not Sanic 25:

python
from sanic import Sanic
from sanic.request import Request
from sanic.response import HTTPResponse

app = Sanic(name='http_proxy')

@app.post('/code/turn_in_web')
async def code_turn_in_web(request: Request) -> HTTPResponse:
    # ERROR: Item "None" of "RequestParameters | None" has no attribute "get"  [union-attr]
    user_project_id = int(request.form.get('user_project_pk'))
    ...

Expected Behavior

Typechecks clean.

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Linux

Sanic Version

25.12.1

Additional context

Recommended fix: Alter type declarations of these two defs to be RequestParameters rather than Optional[RequestParameters].

I can draft a PR if it would be helpful. Let me know.