From e36205175a639bc403f6c3b471576a575da480fe Mon Sep 17 00:00:00 2001 From: Mark Nottingham Date: Sun, 15 Oct 2023 20:18:46 +1100 Subject: [PATCH] Make lint and typecheck happier --- pyproject.toml | 2 +- redbot/formatter/har.py | 4 ++-- redbot/formatter/html_base.py | 2 +- redbot/resource/active_check/base.py | 2 +- redbot/resource/active_check/conneg.py | 6 +++--- redbot/resource/active_check/etag_validate.py | 4 ++-- redbot/resource/active_check/lm_validate.py | 4 ++-- redbot/resource/active_check/range.py | 9 +++++---- redbot/resource/fetch.py | 13 +++++++------ 9 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 34e21805..51bbb913 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,7 +84,7 @@ method-rgx = "[a-z_][a-z0-9_]{1,30}$" class-rgx = "[a-zA-Z0-9_]+$" [tool.pylint.messages_control] -disable = "C0114,C0115,C0116,W0613" +disable = "C0114,C0115,C0116,W0613,R0903" [tool.pylint.reports] reports = false diff --git a/redbot/formatter/har.py b/redbot/formatter/har.py index 04064e10..bb7493ed 100755 --- a/redbot/formatter/har.py +++ b/redbot/formatter/har.py @@ -75,7 +75,7 @@ def add_entry(self, resource: HttpResource, page_ref: int = None) -> None: entry = { "startedDateTime": isoformat(resource.request.start_time), "time": int( - (resource.response.complete_time - resource.request.start_time) * 1000 + (resource.response.finish_time - resource.request.start_time) * 1000 ), "_red_messages": self.format_notes(resource), } @@ -120,7 +120,7 @@ def add_entry(self, resource: HttpResource, page_ref: int = None) -> None: (resource.response.start_time - resource.request.start_time) * 1000 ), "receive": int( - (resource.response.complete_time - resource.response.start_time) * 1000 + (resource.response.finish_time - resource.response.start_time) * 1000 ), } diff --git a/redbot/formatter/html_base.py b/redbot/formatter/html_base.py index d198236b..272b82b1 100644 --- a/redbot/formatter/html_base.py +++ b/redbot/formatter/html_base.py @@ -94,7 +94,7 @@ def feed(self, sample: bytes) -> None: def start_output(self) -> None: if self.resource: uri = self.resource.request.uri or "" - req_headers = self.resource.request.headers + req_headers = self.resource.request.headers.text else: uri = "" req_headers = [] diff --git a/redbot/resource/active_check/base.py b/redbot/resource/active_check/base.py index b1eebf7c..2cb60595 100755 --- a/redbot/resource/active_check/base.py +++ b/redbot/resource/active_check/base.py @@ -56,7 +56,7 @@ def check(self) -> None: self.base.request.uri, self.base.request.method, modified_headers, - self.base.request.payload, + b"".join([chunk[1] for chunk in self.base.request.content_sample]), ) RedFetcher.check(self) diff --git a/redbot/resource/active_check/conneg.py b/redbot/resource/active_check/conneg.py index d0ba2214..c366d25a 100755 --- a/redbot/resource/active_check/conneg.py +++ b/redbot/resource/active_check/conneg.py @@ -29,7 +29,7 @@ def preflight(self) -> bool: k.lower() for (k, v) in self.base.request.headers.text ]: return False - if self.base.response.status_code == "206": + if self.base.response.status_code == 206: return False return True @@ -38,8 +38,8 @@ def done(self) -> None: bare = self.base.response if not negotiated.complete: - if self.http_error: - problem = self.http_error.desc + if self.fetch_error: + problem = self.fetch_error.desc else: problem = "" self.add_base_note("", CONNEG_SUBREQ_PROBLEM, problem=problem) diff --git a/redbot/resource/active_check/etag_validate.py b/redbot/resource/active_check/etag_validate.py index 0035b245..cba719e8 100755 --- a/redbot/resource/active_check/etag_validate.py +++ b/redbot/resource/active_check/etag_validate.py @@ -29,7 +29,7 @@ def modify_request_headers( return base_headers def preflight(self) -> bool: - if self.base.response.status_code[0] == "3": + if 300 <= self.base.response.status_code <= 399: return False etag = self.base.response.headers.parsed.get("etag", None) if etag: @@ -46,7 +46,7 @@ def done(self) -> None: self.add_base_note("", ETAG_SUBREQ_PROBLEM, problem=problem) return - if self.response.status_code == "304": + if self.response.status_code == 304: self.base.inm_support = True self.add_base_note("header-etag", INM_304) self.check_missing_hdrs( diff --git a/redbot/resource/active_check/lm_validate.py b/redbot/resource/active_check/lm_validate.py index 1b312a73..7c63ba60 100755 --- a/redbot/resource/active_check/lm_validate.py +++ b/redbot/resource/active_check/lm_validate.py @@ -58,7 +58,7 @@ def modify_request_headers( return base_headers def preflight(self) -> bool: - if self.base.response.status_code[0] == "3": + if 300 <= self.base.response.status_code <= 399: return False if self.base.response.headers.parsed.get("last-modified", None): return True @@ -74,7 +74,7 @@ def done(self) -> None: self.add_base_note("", LM_SUBREQ_PROBLEM, problem=problem) return - if self.response.status_code == "304": + if self.response.status_code == 304: self.base.ims_support = True self.add_base_note("header-last-modified", IMS_304) self.check_missing_hdrs( diff --git a/redbot/resource/active_check/range.py b/redbot/resource/active_check/range.py index a23502db..763c1e40 100755 --- a/redbot/resource/active_check/range.py +++ b/redbot/resource/active_check/range.py @@ -6,7 +6,8 @@ import random from typing import TYPE_CHECKING -from httplint.note import Note, categories, levels, display_bytes +from httplint.note import Note, categories, levels +from httplint.util import display_bytes from redbot.resource.active_check.base import SubRequest from redbot.formatter import f_num @@ -42,9 +43,9 @@ def modify_request_headers( return base_headers def preflight(self) -> bool: - if self.base.response.status_code[0] == "3": + if 300 <= self.base.response.status_code <= 399: return False - if self.base.response.status_code == "206": + if self.base.response.status_code == 206: return False if "bytes" in self.base.response.headers.parsed.get("accept-ranges", []): if not self.base.response.content_sample: @@ -65,7 +66,7 @@ def done(self) -> None: self.add_base_note("", RANGE_SUBREQ_PROBLEM, problem=problem) return - if self.response.status_code == "206": + if self.response.status_code == 206: c_e = "content-encoding" if ("gzip" in self.base.response.headers.parsed.get(c_e, [])) == ( "gzip" not in self.response.headers.parsed.get(c_e, []) diff --git a/redbot/resource/fetch.py b/redbot/resource/fetch.py index 3c1caf54..9ec37210 100755 --- a/redbot/resource/fetch.py +++ b/redbot/resource/fetch.py @@ -8,7 +8,7 @@ from configparser import SectionProxy import time -from typing import Any, Dict, List, Tuple, Type, Union +from typing import Any, Dict, List, Tuple from httplint import HttpRequestLinter, HttpResponseLinter from httplint.note import Notes, Note, categories, levels @@ -61,9 +61,9 @@ def __init__(self, config: SectionProxy) -> None: self.request = HttpRequestLinter(max_sample_size=0) self.nonfinal_responses: List[HttpResponseLinter] = [] self.response = HttpResponseLinter() - self.exchange: HttpClientExchange = None + self.exchange: HttpClientExchange self.fetch_started = False - self.http_error: httperr.HttpError = None + self.fetch_error: httperr.HttpError self.fetch_done = False self.setup_check_ip() @@ -159,12 +159,12 @@ def check(self) -> None: (k.encode("ascii", "replace"), v.encode("ascii", "replace")) for (k, v) in self.request.headers.text ] + self.request.start_time = time.time() self.exchange.request_start( self.request.method.encode("ascii"), self.request.uri.encode("ascii"), req_hdrs, ) - self.request.start_time = int(time.time()) if not self.fetch_done: # the request could have immediately failed. if self.request.content_sample: for chunk in self.request.content_sample: @@ -187,7 +187,7 @@ def _response_start( self, status: bytes, phrase: bytes, res_headers: RawHeaderListType ) -> None: "Process the response start-line and headers." - self.response.start_time = time.time() + self.response.start_time = int(time.time()) self.response.process_response_topline( self.exchange.res_version, status, phrase ) @@ -214,7 +214,7 @@ def _response_error(self, error: httperr.HttpError) -> None: ) err_sample = error.detail[:40] or "" if isinstance(error, httperr.ExtraDataError): - if self.response.status_code == "304": + if self.response.status_code == 304: self.notes.add("body", BODY_NOT_ALLOWED, sample=err_sample) else: self.notes.add("body", EXTRA_DATA, sample=err_sample) @@ -230,6 +230,7 @@ def _response_error(self, error: httperr.HttpError) -> None: self._fetch_done() def _fetch_done(self) -> None: + self.response.finish_time = time.time() if not self.fetch_done: self.fetch_done = True self.exchange = None