tornado icon indicating copy to clipboard operation
tornado copied to clipboard

Optional request fields that should always have values

Open thijsvandien opened this issue 7 months ago • 1 comments

HTTPServerRequest has e.g. method and uri typed as Optional[str]. In production, I cannot image those being unavailable, so I suppose it came to be this way for testing purposes. Meanwhile there are modern tools/techniques to generate/pass stub data. Does this design decision still make sense then? I find myself writing things like self.redirect(self.request.uri or '') just to get rid of squiggly lines.

thijsvandien avatar May 15 '25 23:05 thijsvandien

When I introduced type annotations, in a lot of places I didn't go any farther than required to get mypy to pass on Tornado itself. So you're right that these really shouldn't be optional. The code is actually a bit of a mess now (comments say that uri is the only required argument, but we don't actually use it outside of tests. We use start_line instead).

There aren't that many test cases where we construct HTTPServerRequests by hand, so we should update those to pass non-None values for method and uri (and probably other fields) and then fix the type annotations to reflect what's really optional and what's always going to be there.

bdarnell avatar May 16 '25 17:05 bdarnell

Fixed in #3542

bdarnell avatar Nov 21 '25 18:11 bdarnell