Repository navigation
feat(abstractions): enhance HeadersCollection usability and dict interoperability - #816
Hardik K (hardikkaurani) wants to merge 1 commit into
Conversation
|
Hi Vincent Biret (@baywet), submitted this PR to resolve #208 by enabling dictionary and HeadersCollection constructor initialization, mapping support in |
| headers (dict[str, str]): The headers to add. | ||
| headers: The headers to add. | ||
| """ | ||
| if not headers: |
There was a problem hiding this comment.
Addressed:
- Updated \�dd_all\ to only reject \None\ (\if headers is None:), allowing empty \HeadersCollection\ instances and empty mappings to complete safely as no-ops without raising errors.
- Updated \ est_request_information.py:29\ to an explicit initialization check (\�ssert request_info.headers is not None).
- Added regression tests for empty mapping and empty collection initialization/merges in \ est_request_header.py.
| elif isinstance(values, Iterable): | ||
| for value in values: | ||
| self.add(key, value) |
There was a problem hiding this comment.
Addressed:
- Materialized iterable values into a list when delegating to \self.add(key, [str(item) for item in map_val])\ (and for collection iterations), properly triggering single-value header handling (e.g., keeping only the first value for \SINGLE_VALUE_HEADERS).
- Added regression test \ est_init_iterable_single_value_header\ verifying \HeadersCollection({'Content-Length': ['10', '20']})\ preserves only the first value ({'10'}).
| return self.get(key) | ||
|
|
||
| def __setitem__(self, key: str, value: Union[str, list[str]]) -> None: | ||
| self.add(key, value) |
There was a problem hiding this comment.
Addressed:
- Implemented _setitem_\ to replace existing header values on assignment (case-insensitively), while validating the replacement (checking key and value not null) before mutating stored headers. Also respects single-value headers on assignment.
- Added tests:
- \ est_subscript_assignment_replaces_values\ covering repeated assignments, differing cases, and list assignments.
- \ est_subscript_assignment_validation\ verifying invalid key/values raise \ValueError\ without mutating existing state.
- \ est_subscript_assignment_single_value_header\ verifying \SINGLE_VALUE_HEADERS\ handling.
|
This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged. |
012435e to
4088ad1
Compare
|
|
Conflicts have been resolved. A maintainer will take a look shortly. |
|
I have resolved the merge conflicts with \main\ and addressed all review feedback:
All local quality gates and checks (\pytest\ 168/168, \mypy\ 0 issues in 48 files, \pylint\ 10.00/10, \yapf, \isort) pass cleanly. Ready for review! |




Description
Fixes #208
This PR addresses the usability improvements requested for
HeadersCollection:HeadersCollectionnow accepts an optional initial headers parameter (Optional[Union[HeadersCollection, Mapping[str, Union[str, Iterable[str]]]]]), allowing direct instantiation from a dictionary or copy-construction from anotherHeadersCollection.add_allDictionary Support:add_allnow accepts either aHeadersCollectionor anyMapping(dict) of header names to values (Union[str, Iterable[str]]), matching the docstring expectation and easing integration across callers.Implements standard Python container magic methods:
__contains__: supports"header" in headers__getitem__: supportsheaders["header"]__setitem__: supportsheaders["header"] = "value"__len__: supportslen(headers)__iter__: supports iterating header keys directlyRelated Issue
Fixes #208
Testing
Added unit tests in
packages/abstractions/tests/test_request_header.py:test_init_with_dicttest_init_with_headers_collectiontest_add_all_with_dicttest_container_methodscc Vincent Biret (@baywet)