-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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?
src/hpack/hpack.py
Outdated
@@ -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: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one!
|
||
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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
04b0c41
to
1534dfe
Compare
|
||
|
||
# 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]]]] |
There was a problem hiding this comment.
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.
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]]] |
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.