Skip to content

add typing information #1289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add typing information
  • Loading branch information
Kriechi committed Jan 22, 2025
commit cf9f2e332b48791966a780ff92ccdf02d799a26b
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ maintainers = [

requires-python = ">=3.9"
dependencies = [
"hyperframe>=6.0,<7",
"hpack>=4.0,<5",
"hyperframe>=6.1,<7",
"hpack>=4.1,<5",
]
dynamic = ["version"]

Expand Down Expand Up @@ -66,6 +66,7 @@ testing = [
linting = [
"ruff>=0.8.0,<1",
"mypy>=1.13.0,<2",
"typing_extensions>=4.12.2",
]

packaging = [
Expand Down Expand Up @@ -93,6 +94,10 @@ version = { attr = "h2.__version__" }
line-length = 140
target-version = "py39"

[tool.mypy]
show_error_codes = true
strict = true

[tool.pytest.ini_options]
testpaths = [ "tests" ]

Expand Down
1 change: 0 additions & 1 deletion src/h2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
h2
~~
Expand Down
42 changes: 21 additions & 21 deletions src/h2/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
h2/config
~~~~~~~~~
Expand All @@ -7,21 +6,22 @@
"""

import sys
from typing import Any, Optional, Union


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):
def __init__(self, name: str) -> None:
self.name = name
self.attr_name = '_%s' % self.name

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

def __set__(self, instance, value):
def __set__(self, instance: Any, value: bool) -> None:
if not isinstance(value, bool):
raise ValueError("%s must be a bool" % self.name)
setattr(instance, self.attr_name, value)
Expand All @@ -35,16 +35,16 @@ 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):
def __init__(self, *vargs) -> None: # type: ignore
pass

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

def trace(self, *vargs, **kwargs):
def trace(self, *vargs, **kwargs) -> None: # type: ignore
"""
No-op logging. Only level needed for now.
"""
Expand All @@ -61,15 +61,15 @@ class OutputLogger:
Defaults to ``sys.stderr``.
:param trace: Enables trace-level output. Defaults to ``False``.
"""
def __init__(self, file=None, trace_level=False):
def __init__(self, file=None, trace_level=False): # type: ignore
super().__init__()
self.file = file or sys.stderr
self.trace_level = trace_level

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

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

Expand Down Expand Up @@ -165,14 +165,14 @@ class H2Configuration:
)

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

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

@header_encoding.setter
def header_encoding(self, value):
def header_encoding(self, value: Optional[Union[bool, str]]) -> None:
"""
Enforces constraints on the value of header encoding.
"""
Expand Down
Loading