Skip to content

Commit 9a54234

Browse files
committed
typing++
1 parent 2602e6c commit 9a54234

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/hpack/hpack.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Implements the HPACK header compression algorithm as detailed by the IETF.
66
"""
77
import logging
8-
from typing import Any, Generator, Union
8+
from typing import Any, Generator, Iterable, Optional, Union
99

1010
from .table import HeaderTable, table_entry_size
1111
from .exceptions import (
@@ -16,7 +16,8 @@
1616
REQUEST_CODES, REQUEST_CODES_LENGTH
1717
)
1818
from .huffman_table import decode_huffman
19-
from .struct import HeaderTuple, NeverIndexedHeaderTuple, Headers
19+
from .struct import HeaderTuple, NeverIndexedHeaderTuple, HeaderWeaklyTyped
20+
from .table import HeaderTable, table_entry_size
2021

2122
log = logging.getLogger(__name__)
2223

@@ -34,17 +35,17 @@
3435
DEFAULT_MAX_HEADER_LIST_SIZE = 2 ** 16
3536

3637

37-
def _unicode_if_needed(header: HeaderTuple, raw: bool) -> HeaderTuple:
38+
def _unicode_if_needed(header: HeaderWeaklyTyped, raw: bool) -> HeaderTuple:
3839
"""
3940
Provides a header as a unicode string if raw is False, otherwise returns
4041
it as a bytestring.
4142
"""
4243
name = bytes(header[0]) # type: ignore
4344
value = bytes(header[1]) # type: ignore
45+
4446
if not raw:
45-
return header.__class__(name.decode('utf-8'), value.decode('utf-8'))
46-
else:
47-
return header.__class__(name, value)
47+
return header.__class__(name.decode("utf-8"), value.decode("utf-8")) # type: ignore
48+
return header.__class__(name, value) # type: ignore
4849

4950

5051
def encode_integer(integer: int, prefix_bits: int) -> bytearray:
@@ -123,7 +124,7 @@ def decode_integer(data: bytes, prefix_bits: int) -> tuple[int, int]:
123124

124125

125126
def _dict_to_iterable(header_dict: Union[dict[bytes, bytes], dict[str, str]]) \
126-
-> Generator[Union[tuple[bytes, bytes], tuple[str, str]], None, None]:
127+
-> Generator[Union[tuple[bytes, bytes, Optional[bool]], tuple[str, str, Optional[bool]]], None, None]:
127128
"""
128129
This converts a dictionary to an iterable of two-tuples. This is a
129130
HPACK-specific function because it pulls "special-headers" out first and
@@ -177,7 +178,7 @@ def header_table_size(self, value: int) -> None:
177178
self.table_size_changes.append(value)
178179

179180
def encode(self,
180-
headers: Headers,
181+
headers: Union[Iterable[tuple[Union[bytes, str], Union[bytes, str], Optional[bool]]], dict[Union[bytes, str], Union[bytes, str, tuple]]],
181182
huffman: bool = True) -> bytes:
182183
"""
183184
Takes a set of headers and encodes them into a HPACK-encoded header
@@ -433,7 +434,7 @@ def header_table_size(self) -> int:
433434
def header_table_size(self, value: int) -> None:
434435
self.header_table.maxsize = value
435436

436-
def decode(self, data: bytes, raw: bool = False) -> Headers:
437+
def decode(self, data: bytes, raw: bool = False) -> Iterable[HeaderTuple]:
437438
"""
438439
Takes an HPACK-encoded header block and decodes it into a header set.
439440

src/hpack/huffman.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"""
88

99

10+
from typing import Optional
11+
12+
1013
class HuffmanEncoder:
1114
"""
1215
Encodes a string according to the Huffman encoding table defined in the
@@ -16,7 +19,7 @@ def __init__(self, huffman_code_list: list[int], huffman_code_list_lengths: list
1619
self.huffman_code_list = huffman_code_list
1720
self.huffman_code_list_lengths = huffman_code_list_lengths
1821

19-
def encode(self, bytes_to_encode: bytes) -> bytes:
22+
def encode(self, bytes_to_encode: Optional[bytes]) -> bytes:
2023
"""
2124
Given a string of bytes, encodes them according to the HPACK Huffman
2225
specification.

src/hpack/struct.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
Contains structures for representing header fields with associated metadata.
66
"""
77

8+
from typing import Any
89

9-
from typing import Any, Iterable, Union
10+
from typing_extensions import Self, TypeAlias
1011

1112

12-
class HeaderTuple(tuple[Union[bytes, str], Union[bytes, str]]):
13+
class HeaderTuple(tuple[bytes, bytes]):
1314
"""
1415
A data structure that stores a single header field.
1516
@@ -27,7 +28,7 @@ class HeaderTuple(tuple[Union[bytes, str], Union[bytes, str]]):
2728

2829
indexable = True
2930

30-
def __new__(cls, *args: Any) -> "HeaderTuple":
31+
def __new__(cls, *args: Any) -> Self:
3132
return tuple.__new__(cls, args)
3233

3334

@@ -40,10 +41,9 @@ class NeverIndexedHeaderTuple(HeaderTuple):
4041

4142
indexable = False
4243

43-
def __new__(cls, *args: Any) -> "NeverIndexedHeaderTuple":
44+
def __new__(cls, *args: Any) -> Self:
4445
return tuple.__new__(cls, args)
4546

4647

47-
# explicitly mentioning all valid header types , even if some superseed each other
48-
Header = Union[HeaderTuple, NeverIndexedHeaderTuple, tuple[bytes, bytes], tuple[str, str]]
49-
Headers = Iterable[Header]
48+
Header: TypeAlias = "HeaderTuple | NeverIndexedHeaderTuple | tuple[bytes, bytes]"
49+
HeaderWeaklyTyped: TypeAlias = "HeaderTuple | NeverIndexedHeaderTuple | tuple[bytes | str, bytes | str]"

src/hpack/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# flake8: noqa
22
from collections import deque
33
import logging
4-
from typing import Any, Optional, Union
4+
from typing import Optional
55

66
from .exceptions import InvalidTableIndex
77

88
log = logging.getLogger(__name__)
99

1010

11-
def table_entry_size(name: Union[bytes, str], value: Union[bytes, str]) -> int:
11+
def table_entry_size(name: bytes, value: bytes) -> int:
1212
"""
1313
Calculates the size of a single entry
1414

0 commit comments

Comments
 (0)