]> git.ipfire.org Git - thirdparty/tornado.git/commit
Make `uri` and `method` attributes on `HTTPServerRequest` mandatory 3542/head
authorArmen Michaeli <armen@armen.michaeli.name>
Sun, 9 Nov 2025 11:53:32 +0000 (12:53 +0100)
committerArmen Michaeli <armen@armen.michaeli.name>
Sun, 9 Nov 2025 12:07:39 +0000 (13:07 +0100)
commit210368ca5ca8c4817437144598235df922ab0cfc
tree07b2680153b946defd11fc6f53c65f8e0b51893a
parentd30ef74020924be5f214091de95314ca3ce919af
Make `uri` and `method` attributes on `HTTPServerRequest` mandatory

This adds assertion checks during initialisation of `HTTPServerRequest` object, to ensure the corresponding attributes of the object being created, won't end up being `None`. To be clear, it is still permitted to provide `None` or omit specification for both `uri` and `method` parameters to the constructor if the `start_line` parameter specifies [good] values instead.

This is motivated by the idea that per HTTP, requests _always_ feature a method and a URI, and so the model implemented with `HTTPServerRequest` is amended accordingly. Beyond the seemingly idealistic motivation, this helps with _typed_ Tornado applications using e.g. `self.request.uri` as part of request handling -- i.e. in code with `self` being a `RequestHandler` -- to safely assume e.g. `uri` or `method` are `str` and not `str | None` / `Optional[str]` which would require ad-hoc assertions ala `assert self.request.uri is not None` (or `assert self.request.uri`) in _application code_, which IMO is a case of "surprising the user" -- as everyone would expect a HTTP request to have an URI and method be clearly defined as e.g. strings -- certainly excluding the `None` value.

Again, because semantics of `start_line` are preserved, the initialisation of the object _may_ omit parameters `uri` and/or `method` if `start_line` specifies valid values for these instead. In any case, it is the _attributes_ of the object being constructed, that end up being effectively validated with `assert` -- which make the type checker (tested with MyPy 1.18.2 here) assume `str` instead of `str | None`.
tornado/httputil.py
tornado/test/httputil_test.py