Skip to content

Commit

Permalink
linting++
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Dec 21, 2024
1 parent 6d39644 commit e69a7d5
Show file tree
Hide file tree
Showing 12 changed files with 777 additions and 717 deletions.
48 changes: 47 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,54 @@ h2 = [ "py.typed" ]
version = { attr = "h2.__version__" }

[tool.ruff]
line-length = 140
line-length = 150
target-version = "py39"
format.preview = true
format.docstring-code-line-length = 100
format.docstring-code-format = true
lint.select = [
"ALL",
]
lint.ignore = [
"PYI034", # PEP 673 not yet available in Python 3.9 - only in 3.11+
"ANN001", # args with typing.Any
"ANN002", # args with typing.Any
"ANN003", # kwargs with typing.Any
"ANN401", # kwargs with typing.Any
"SLF001", # implementation detail
"CPY", # not required
"D101", # docs readability
"D102", # docs readability
"D105", # docs readability
"D107", # docs readability
"D200", # docs readability
"D205", # docs readability
"D205", # docs readability
"D203", # docs readability
"D212", # docs readability
"D400", # docs readability
"D401", # docs readability
"D415", # docs readability
"PLR2004", # readability
"SIM108", # readability
"RUF012", # readability
"FBT001", # readability
"FBT002", # readability
"PGH003", # readability
"PGH004", # readability
"FIX001", # readability
"FIX002", # readability
"TD001", # readability
"TD002", # readability
"TD003", # readability
"S101", # readability
"PD901", # readability
"ERA001", # readability
"ARG001", # readability
"ARG002", # readability
"PLR0913", # readability
]
lint.isort.required-imports = [ "from __future__ import annotations" ]

[tool.mypy]
show_error_codes = true
Expand Down
4 changes: 3 additions & 1 deletion src/h2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
A HTTP/2 implementation.
"""
__version__ = '4.1.0'
from __future__ import annotations

__version__ = "4.1.0"
46 changes: 26 additions & 20 deletions src/h2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
Objects for controlling the configuration of the HTTP/2 stack.
"""
from __future__ import annotations

import sys
from typing import Any, Optional, Union
from typing import Any


class _BooleanConfigOption:
"""
Descriptor for handling a boolean config option. This will block
attempts to set boolean config options to non-bools.
"""

def __init__(self, name: str) -> None:
self.name = name
self.attr_name = '_%s' % self.name
self.attr_name = f"_{self.name}"

def __get__(self, instance: Any, owner: Any) -> bool:
return getattr(instance, self.attr_name) # type: ignore

def __set__(self, instance: Any, value: bool) -> None:
if not isinstance(value, bool):
raise ValueError("%s must be a bool" % self.name)
msg = f"{self.name} must be a bool"
raise ValueError(msg) # noqa: TRY004
setattr(instance, self.attr_name, value)


Expand All @@ -35,20 +38,19 @@ class DummyLogger:
conditionals being sprinkled throughout the h2 code for calls to
logging functions when no logger is passed into the corresponding object.
"""

def __init__(self, *vargs) -> None: # type: ignore
pass

def debug(self, *vargs, **kwargs) -> None: # type: ignore
"""
No-op logging. Only level needed for now.
"""
pass

def trace(self, *vargs, **kwargs) -> None: # type: ignore
"""
No-op logging. Only level needed for now.
"""
pass


class OutputLogger:
Expand All @@ -61,15 +63,16 @@ class OutputLogger:
Defaults to ``sys.stderr``.
:param trace: Enables trace-level output. Defaults to ``False``.
"""
def __init__(self, file=None, trace_level=False): # type: ignore

def __init__(self, file=None, trace_level=False) -> None: # type: ignore
super().__init__()
self.file = file or sys.stderr
self.trace_level = trace_level

def debug(self, fmtstr, *args): # type: ignore
def debug(self, fmtstr, *args) -> None: # type: ignore
print(f"h2 (debug): {fmtstr % args}", file=self.file)

def trace(self, fmtstr, *args): # type: ignore
def trace(self, fmtstr, *args) -> None: # type: ignore
if self.trace_level:
print(f"h2 (trace): {fmtstr % args}", file=self.file)

Expand Down Expand Up @@ -147,32 +150,33 @@ class H2Configuration:
:type logger: ``logging.Logger``
"""
client_side = _BooleanConfigOption('client_side')

client_side = _BooleanConfigOption("client_side")
validate_outbound_headers = _BooleanConfigOption(
'validate_outbound_headers'
"validate_outbound_headers",
)
normalize_outbound_headers = _BooleanConfigOption(
'normalize_outbound_headers'
"normalize_outbound_headers",
)
split_outbound_cookies = _BooleanConfigOption(
'split_outbound_cookies'
"split_outbound_cookies",
)
validate_inbound_headers = _BooleanConfigOption(
'validate_inbound_headers'
"validate_inbound_headers",
)
normalize_inbound_headers = _BooleanConfigOption(
'normalize_inbound_headers'
"normalize_inbound_headers",
)

def __init__(self,
client_side: bool = True,
header_encoding: Optional[Union[bool, str]] = None,
header_encoding: bool | str | None = None,
validate_outbound_headers: bool = True,
normalize_outbound_headers: bool = True,
split_outbound_cookies: bool = False,
validate_inbound_headers: bool = True,
normalize_inbound_headers: bool = True,
logger: Optional[Union[DummyLogger, OutputLogger]] = None) -> None:
logger: DummyLogger | OutputLogger | None = None) -> None:
self.client_side = client_side
self.header_encoding = header_encoding
self.validate_outbound_headers = validate_outbound_headers
Expand All @@ -183,7 +187,7 @@ def __init__(self,
self.logger = logger or DummyLogger(__name__)

@property
def header_encoding(self) -> Optional[Union[bool, str]]:
def header_encoding(self) -> bool | str | None:
"""
Controls whether the headers emitted by this object in events are
transparently decoded to ``unicode`` strings, and what encoding is used
Expand All @@ -195,12 +199,14 @@ def header_encoding(self) -> Optional[Union[bool, str]]:
return self._header_encoding

@header_encoding.setter
def header_encoding(self, value: Optional[Union[bool, str]]) -> None:
def header_encoding(self, value: bool | str | None) -> None:
"""
Enforces constraints on the value of header encoding.
"""
if not isinstance(value, (bool, str, type(None))):
raise ValueError("header_encoding must be bool, string, or None")
msg = "header_encoding must be bool, string, or None"
raise ValueError(msg) # noqa: TRY004
if value is True:
raise ValueError("header_encoding cannot be True")
msg = "header_encoding cannot be True"
raise ValueError(msg)
self._header_encoding = value
Loading

0 comments on commit e69a7d5

Please sign in to comment.