Skip to content

Commit

Permalink
Strict optional type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mnot committed Dec 13, 2023
1 parent e542a8b commit 26fa470
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 90 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true
strict_optional = false
strict_optional = true
show_error_codes = true

[tool.pylint.basic]
Expand Down
3 changes: 2 additions & 1 deletion redbot/cache_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from os import path
import time
from typing import Optional
import zlib


Expand All @@ -14,7 +15,7 @@ class CacheFile:
def __init__(self, my_path: str) -> None:
self.path = my_path

def read(self) -> bytes:
def read(self) -> Optional[bytes]:
"""
Read the file, returning its contents. If it does not exist or
cannot be read, returns None.
Expand Down
5 changes: 4 additions & 1 deletion redbot/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def __init__(
self.uri = b""
self.req_hdrs: RawHeaderListType = []
self.req_body = b""
if not exchange.http_conn.tcp_conn:
return
self.client_ip = exchange.http_conn.tcp_conn.socket.getpeername()[0]
exchange.on("request_start", self.request_start)
exchange.on("request_body", self.request_body)
exchange.on("request_done", self.request_done)
Expand All @@ -148,7 +151,7 @@ def request_body(self, chunk: bytes) -> None:
def request_done(self, trailers: RawHeaderListType) -> None:
p_uri = urlsplit(self.uri)
if p_uri.path == b"/":
client_ip = self.exchange.http_conn.tcp_conn.socket.getpeername()[0]
client_ip = self.client_ip
try:
RedWebUi(
self.server.config,
Expand Down
10 changes: 4 additions & 6 deletions redbot/formatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import locale
import sys
import time
from typing import Any, Callable, List, Dict, Type, TYPE_CHECKING
from typing import Optional, Any, Callable, List, Dict, Type, TYPE_CHECKING
import unittest

from markdown import Markdown
Expand Down Expand Up @@ -75,8 +75,8 @@ class Formatter(EventEmitter):
Is available to UIs based upon the 'name' attribute.
"""

media_type: str = None # the media type of the format.
name: str = None # the name of the format.
media_type: str # the media type of the format.
name: str = "base class" # the name of the format.
can_multiple = False # formatter can represent multiple responses.

def __init__(
Expand Down Expand Up @@ -184,7 +184,7 @@ def f_num(i: int, by1024: bool = False) -> str:
return locale.format_string("%d", i, grouping=True)


def relative_time(utime: float, now: float = None, show_sign: int = 1) -> str:
def relative_time(utime: float, now: Optional[float] = None, show_sign: int = 1) -> str:
"""
Given two times, return a string that explains how far apart they are.
show_sign can be:
Expand All @@ -199,8 +199,6 @@ def relative_time(utime: float, now: float = None, show_sign: int = 1) -> str:
2: ("none", "behind", "ahead"),
}

if utime is None:
return None
if now is None:
now = time.time()
age = round(now - utime)
Expand Down
10 changes: 8 additions & 2 deletions redbot/formatter/har.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import datetime
import json
from typing import Any, Dict, List
from typing import Optional, Any, Dict, List
from typing_extensions import TypedDict

from redbot import __version__
Expand Down Expand Up @@ -71,7 +71,12 @@ def finish_output(self) -> None:
def error_output(self, message: str) -> None:
self.output(message)

def add_entry(self, resource: HttpResource, page_ref: int = None) -> None:
def add_entry(self, resource: HttpResource, page_ref: Optional[int] = None) -> None:
assert resource.request.start_time, "request.start_time not set in add_entry"
assert resource.response.start_time, "response.start_time not set in add_entry"
assert (
resource.response.finish_time
), "response.finish_time not set in add_entry"
entry = {
"startedDateTime": isoformat(resource.request.start_time),
"time": int(
Expand Down Expand Up @@ -135,6 +140,7 @@ def add_entry(self, resource: HttpResource, page_ref: int = None) -> None:
self.har["log"]["entries"].append(entry)

def add_page(self, resource: HttpResource) -> int:
assert resource.request.start_time, "request.start_time not set in add_page"
page_id = self.last_id + 1
page = {
"startedDateTime": isoformat(resource.request.start_time),
Expand Down
4 changes: 3 additions & 1 deletion redbot/formatter/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ def format_body_sample(self, resource: HttpResource) -> Markup:
"""show the stored body sample"""
sample = b"".join(resource.response_decoded_sample)
try:
uni_sample = sample.decode(resource.response.character_encoding, "ignore")
uni_sample = sample.decode(
resource.response.character_encoding or "utf-8", "ignore"
)
except (TypeError, LookupError):
uni_sample = sample.decode("utf-8", "replace")
safe_sample = escape(uni_sample)
Expand Down
17 changes: 7 additions & 10 deletions redbot/formatter/html_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os
import time
from typing import Any, List, Tuple
from typing import Optional, Any, List, Tuple
from urllib.parse import urljoin, urlencode, quote as urlquote

import httplint
Expand Down Expand Up @@ -94,12 +94,8 @@ def feed(self, sample: bytes) -> None:
pass

def start_output(self) -> None:
if self.resource is None:
uri = ""
req_headers = []
else:
uri = self.resource.request.uri or ""
req_headers = self.resource.request.headers.text
uri = self.resource.request.uri or ""
req_headers = self.resource.request.headers.text
extra_title = " <span class='save'>"
if self.kw.get("is_saved", None):
extra_title += " saved "
Expand Down Expand Up @@ -229,9 +225,9 @@ def format_extra(self, etype: str = ".html") -> Markup:
def redbot_link(
self,
link_value: str,
link: str = None,
check_name: str = None,
res_format: str = None,
link: Optional[str] = None,
check_name: Optional[str] = None,
res_format: Optional[str] = None,
use_stored: bool = True,
descend: bool = False,
referer: bool = False,
Expand Down Expand Up @@ -261,6 +257,7 @@ def redbot_link(
Request headers are copied over from the current context.
"""
assert self.resource.request.uri, "resource.request.uri not set in redbot_link"
uri = self.resource.request.uri
args: List[Tuple[str, str]] = []
if check_name:
Expand Down
13 changes: 5 additions & 8 deletions redbot/formatter/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import json
from typing import Any, List, Dict, Union
from typing import Optional, Any, List, Dict, Union

from httplint import HttpResponseLinter
from httplint.note import categories, levels
Expand Down Expand Up @@ -59,12 +59,7 @@ def finish_output(self) -> None:
+ self.link_saved()
)
else:
if self.resource.fetch_error is None:
notification = "No response error."
else:
notification = (
f"Sorry, I can't do that; {self.resource.fetch_error.desc}"
)
notification = f"Sorry, I can't do that; {self.resource.fetch_error.desc}"
blocks = [self.markdown_block(f"_{notification}_")]
self.send_slack_message(blocks, notification)

Expand All @@ -74,7 +69,9 @@ def error_output(self, message: str) -> None:
def timeout(self) -> None:
self.send_slack_message([self.markdown_block("_Timed out._")], "Timed out.")

def send_slack_message(self, blocks: List[Dict], notification: str = None) -> None:
def send_slack_message(
self, blocks: List[Dict], notification: Optional[str] = None
) -> None:
data: Dict[str, Any] = {"blocks": blocks}
if notification:
data["text"] = notification
Expand Down
6 changes: 3 additions & 3 deletions redbot/formatter/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import operator
import re
import textwrap
from typing import Any, List
from typing import Any, List, Optional

from httplint import HttpResponseLinter
from httplint.note import Note, levels, categories
Expand Down Expand Up @@ -117,7 +117,7 @@ def format_recommendation(
def format_text(note: Note) -> List[str]:
return textwrap.wrap(strip_tags(re.sub(r"(?m)\s\s+", " ", note.detail)))

def colorize(self, level: levels, instr: str) -> str:
def colorize(self, level: Optional[levels], instr: str) -> str:
if self.kw.get("tty_out", False):
# info
color_start = "\033[0;32m"
Expand Down Expand Up @@ -188,7 +188,7 @@ def finish_output(self) -> None:
self.output(self.format_recommendations(subresource) + NL + NL)

def format_uri(self, resource: HttpResource) -> str:
return self.colorize(None, resource.request.uri)
return self.colorize(None, resource.request.uri or "")


class VerboseTextListFormatter(TextListFormatter):
Expand Down
12 changes: 6 additions & 6 deletions redbot/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from configparser import SectionProxy
import sys
from typing import List, Dict, Set, Tuple, Union
from typing import Optional, List, Dict, Set, Tuple, Union
from urllib.parse import urljoin

import thor
Expand Down Expand Up @@ -43,10 +43,10 @@ def __init__(self, config: SectionProxy, descend: bool = False) -> None:
RedFetcher.__init__(self, config)
self.descend: bool = descend
self.check_done: bool = False
self.partial_support: bool = None
self.inm_support: bool = None
self.ims_support: bool = None
self.gzip_support: bool = None
self.partial_support: bool = False
self.inm_support: bool = False
self.ims_support: bool = False
self.gzip_support: bool = False
self.gzip_savings: int = 0
self._task_map: Set[RedFetcher] = set([])
self.subreqs = {ac.check_name: ac(config, self) for ac in active_checks}
Expand Down Expand Up @@ -91,7 +91,7 @@ def check_done() -> None:

# pylint: enable=cell-var-from-loop

def finish_check(self, resource: RedFetcher = None) -> None:
def finish_check(self, resource: Optional[RedFetcher] = None) -> None:
"A check is done. Was that the last one?"
if resource:
try:
Expand Down
4 changes: 2 additions & 2 deletions redbot/resource/active_check/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def check(self) -> None:
)
RedFetcher.set_request(
self,
self.base.request.uri,
self.base.request.method,
self.base.request.uri or "",
self.base.request.method or "",
modified_headers,
self.base.request_content,
)
Expand Down
4 changes: 2 additions & 2 deletions redbot/resource/active_check/conneg.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def done(self) -> None:
self.add_base_note(
"status",
VARY_STATUS_MISMATCH,
neg_status=negotiated.status_code,
noneg_status=bare.status_code,
neg_status=negotiated.status_code or 0,
noneg_status=bare.status_code or 0,
)
return # Can't be sure what's going on...

Expand Down
2 changes: 1 addition & 1 deletion redbot/resource/active_check/etag_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def done(self) -> None:
self.add_base_note(
"header-etag",
INM_STATUS,
inm_status=self.response.status_code,
inm_status=self.response.status_code or 0,
enc_inm_status=self.response.status_code or "(unknown)",
)

Expand Down
2 changes: 1 addition & 1 deletion redbot/resource/active_check/lm_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def done(self) -> None:
self.add_base_note(
"header-last-modified",
IMS_STATUS,
ims_status=self.response.status_code,
ims_status=self.response.status_code or 0,
enc_ims_status=self.response.status_code or "(unknown)",
)

Expand Down
8 changes: 4 additions & 4 deletions redbot/resource/active_check/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class RangeRequest(SubRequest):
response_phrase = "The partial response"

def __init__(self, config: SectionProxy, resource: "HttpResource") -> None:
self.range_start: int = None
self.range_end: int = None
self.range_target: bytes = None
self.range_start: int
self.range_end: int
self.range_target: bytes
self.max_sample_size = 0 # unlimited
SubRequest.__init__(self, config, resource)

Expand Down Expand Up @@ -119,7 +119,7 @@ def done(self) -> None:
self.add_base_note(
"header-accept-ranges",
RANGE_STATUS,
range_status=self.response.status_code,
range_status=self.response.status_code or 0,
enc_range_status=self.response.status_code or "(unknown)",
)

Expand Down
Loading

0 comments on commit 26fa470

Please sign in to comment.