astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.35k stars 1.05k forks source link

Add an exception for `N802` for `BaseHTTPRequestHandler` subclasses' methods #9400

Open ZeeD opened 8 months ago

ZeeD commented 8 months ago

According to the doc you should use BaseHTTPRequestHandler subclassing it and implementing various do_SPAM methods, one for each request method. But if in this basic snippet

from http.server import BaseHTTPRequestHandler
class MyRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self) -> None: ...

ruff raises the error

N802 Function name `do_GET` should be lowercase

I would suggest to add an exception for the various do_XXX methods of the subclasses of BaseHTTPRequestHandler

charliermarsh commented 8 months ago

One thing you can do to help Ruff here is add the @typing.override or @typing_extensions.override decorator to your method to indicate to tools that it's overriding an existing method.

ZeeD commented 8 months ago

Hi, thanks for the reply. In this case I don't think it will work, as BaseHTTPRequestHandler does not implement such methods. Instead they are part of an (informal) protocol, and retrieved dynamically via getattr - as you can see from https://github.com/python/cpython/blob/e56c53334f1adf5b4ea940036c76d18e80fe809d/Lib/http/server.py#L417-L424

BTW: if I add @typing.override I may trick ruff, but now mypy raises an error (because there is no do_SPAM method to override in BaseHTTPRequestHandler)