Skip to content
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

add typing information #277

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

add typing information #277

wants to merge 2 commits into from

Conversation

Kriechi
Copy link
Member

@Kriechi Kriechi commented Nov 16, 2024

mypy is reasonably happy already - but I expect some misaligned bugs to be in there. This can be addressed iteratively going forward as there should be no functional or behavioral change due to incorrect type hints.

Copy link
Contributor

@BYK BYK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add black too in a follow up?

@@ -143,11 +138,11 @@ def _dict_to_iterable(header_dict):
yield key, header_dict[key]


def _to_bytes(string):
def _to_bytes(string: Union[bytes, str, Any]) -> bytes:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just Any probably covers all but I like you calling out the "expected" types. Maybe add a comment about this to avoid confusion in future readers?

@@ -575,7 +572,7 @@ def _decode_literal(self, data, should_index):
high_byte = data[0]
indexed_name = high_byte & 0x0F
name_len = 4
not_indexable = high_byte & 0x10
not_indexable = bool(high_byte & 0x10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one!

src/hpack/huffman.py Show resolved Hide resolved

from .exceptions import InvalidTableIndex

log = logging.getLogger(__name__)


def table_entry_size(name, value):
def table_entry_size(name: Union[bytes, str], value: Union[bytes, str]) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can abstract this Union[bytes, str] and tuple[bytes, bytes] into type definitions as they are used in a bunch of places. Something like RawHeaderPiece and UntypedHeaderTuple respectively?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! It was on my ToDo list, but didn't make it for this first PR - happy to be refactored in a future PR!
I'm working on a similar PR for the h2 library as well, so we might want to hold off on type aliases until we have a full picture.

src/hpack/table.py Outdated Show resolved Hide resolved
@Kriechi Kriechi force-pushed the typing branch 2 times, most recently from 04b0c41 to 1534dfe Compare November 16, 2024 18:44


# explicitly mentioning all valid header types , even if some superseed each other
type Headers = Iterable[Union[HeaderTuple, NeverIndexedHeaderTuple, tuple[Union[bytes, str], Union[bytes, str]]]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better not to allow mixed bytes and str headers as we don't expect them.

Suggested change
type Headers = Iterable[Union[HeaderTuple, NeverIndexedHeaderTuple, tuple[Union[bytes, str], Union[bytes, str]]]]
type Headers = Iterable[Union[HeaderTuple, NeverIndexedHeaderTuple, tuple[bytes, bytes], tuple[str, str]]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants