From: Ben Darnell Date: Sun, 7 Oct 2018 04:19:52 +0000 (-0400) Subject: web,util: Work around "initialize incompatible with superclass" errors X-Git-Tag: v6.0.0b1~28^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4794f301009651abe9982bba13a67eaa02b4bfc7;p=thirdparty%2Ftornado.git web,util: Work around "initialize incompatible with superclass" errors There are still a few more of these, but they're internal-only. --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 0552ec91b..78945c10a 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -161,7 +161,7 @@ class HTTPServer(TCPServer, Configurable, httputil.HTTPServerConnectionDelegate) # completely) pass - def initialize( # type: ignore + def initialize( self, request_callback: Union[ httputil.HTTPServerConnectionDelegate, diff --git a/tornado/netutil.py b/tornado/netutil.py index 5a92690fa..7c07c48ff 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -525,7 +525,7 @@ class OverrideResolver(Resolver): Added support for host-port-family triplets. """ - def initialize(self, resolver: Resolver, mapping: dict) -> None: # type: ignore + def initialize(self, resolver: Resolver, mapping: dict) -> None: self.resolver = resolver self.mapping = mapping diff --git a/tornado/util.py b/tornado/util.py index 4d0d656a3..c4c019961 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -302,18 +302,17 @@ class Configurable(object): """Returns the implementation class to be used if none is configured.""" raise NotImplementedError() - def initialize(self) -> None: - """Initialize a `Configurable` subclass instance. + def _initialize(self) -> None: + pass - Configurable classes should use `initialize` instead of ``__init__``. + initialize = _initialize # type: Callable[..., None] + """Initialize a `Configurable` subclass instance. - When used with ``mypy``, subclasses will often need ``# type: ignore`` - annotations on this method because ``mypy`` does not recognize that - its arguments may change in subclasses (as it does for ``__init__``). + Configurable classes should use `initialize` instead of ``__init__``. - .. versionchanged:: 4.2 - Now accepts positional arguments in addition to keyword arguments. - """ + .. versionchanged:: 4.2 + Now accepts positional arguments in addition to keyword arguments. + """ @classmethod def configure(cls, impl, **kwargs): diff --git a/tornado/web.py b/tornado/web.py index 87e02308d..bbc977d7d 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -227,26 +227,28 @@ class RequestHandler(object): ) self.initialize(**kwargs) # type: ignore - def initialize(self) -> None: - """Hook for subclass initialization. Called for each request. + def _initialize(self) -> None: + pass - A dictionary passed as the third argument of a url spec will be - supplied as keyword arguments to initialize(). + initialize = _initialize # type: Callable[..., None] + """Hook for subclass initialization. Called for each request. - Example:: + A dictionary passed as the third argument of a url spec will be + supplied as keyword arguments to initialize(). - class ProfileHandler(RequestHandler): - def initialize(self, database): - self.database = database + Example:: - def get(self, username): - ... + class ProfileHandler(RequestHandler): + def initialize(self, database): + self.database = database - app = Application([ - (r'/user/(.*)', ProfileHandler, dict(database=database)), - ]) - """ - pass + def get(self, username): + ... + + app = Application([ + (r'/user/(.*)', ProfileHandler, dict(database=database)), + ]) + """ @property def settings(self) -> Dict[str, Any]: @@ -2425,7 +2427,7 @@ class MissingArgumentError(HTTPError): class ErrorHandler(RequestHandler): """Generates an error response with ``status_code`` for all requests.""" - def initialize(self, status_code: int) -> None: # type: ignore + def initialize(self, status_code: int) -> None: self.set_status(status_code) def prepare(self) -> None: @@ -2471,7 +2473,7 @@ class RedirectHandler(RequestHandler): destination URL. """ - def initialize(self, url: str, permanent: bool = True) -> None: # type: ignore + def initialize(self, url: str, permanent: bool = True) -> None: self._url = url self._permanent = permanent @@ -2558,7 +2560,7 @@ class StaticFileHandler(RequestHandler): _static_hashes = {} # type: Dict[str, Optional[str]] _lock = threading.Lock() # protects _static_hashes - def initialize( # type: ignore + def initialize( self, path: str, default_filename: str = None ) -> None: self.root = path @@ -3009,7 +3011,7 @@ class FallbackHandler(RequestHandler): ]) """ - def initialize( # type: ignore + def initialize( self, fallback: Callable[[httputil.HTTPServerRequest], None] ) -> None: self.fallback = fallback