Skip to content

feat(abstractions): enhance HeadersCollection usability and dict interoperability - #816

Open
Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:feat/headers-collection-usability
Open

Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:feat/headers-collection-usability

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

Fixes #208

This PR addresses the usability improvements requested for HeadersCollection:

  1. Dictionary and HeadersCollection Constructor Initialization:
    HeadersCollection now 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 another HeadersCollection.
  2. add_all Dictionary Support:
    add_all now accepts either a HeadersCollection or any Mapping (dict) of header names to values (Union[str, Iterable[str]]), matching the docstring expectation and easing integration across callers.
  3. Python Container / Mapping Protocol:
    Implements standard Python container magic methods:
    • __contains__: supports "header" in headers
    • __getitem__: supports headers["header"]
    • __setitem__: supports headers["header"] = "value"
    • __len__: supports len(headers)
    • __iter__: supports iterating header keys directly

Related Issue

Fixes #208

Testing

Added unit tests in packages/abstractions/tests/test_request_header.py:

  • test_init_with_dict
  • test_init_with_headers_collection
  • test_add_all_with_dict
  • test_container_methods

cc Vincent Biret (@baywet)

Copilot AI balanced review requested due to automatic review settings October 19, 2026 20:17
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 19, 2026 20:17

This comment was marked as off-topic.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet), submitted this PR to resolve #208 by enabling dictionary and HeadersCollection constructor initialization, mapping support in add_all, and Python container protocol magic methods (in, [], len(), iter()). Comprehensive unit tests added in test_request_header.py. Ready for your review, thank you!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Empty-input handling, replacement assignment, and single-value header imports have unresolved correctness issues.

3 open issues

Review effort: Balanced

headers (dict[str, str]): The headers to add.
headers: The headers to add.
"""
if not headers:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +100 to +102
elif isinstance(values, Iterable):
for value in values:
self.add(key, value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 18:26
@hardikkaurani
Hardik K (hardikkaurani) force-pushed the feat/headers-collection-usability branch from 012435e to 4088ad1 Compare October 20, 2026 18:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Conflicts have been resolved. A maintainer will take a look shortly.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet)!

I have resolved the merge conflicts with \main\ and addressed all review feedback:

  1. Empty-Input Handling:

    • Updated \�dd_all\ to only reject \None\ (\if headers is None:), allowing empty \HeadersCollection\ instances and empty mappings to complete cleanly as no-ops.
    • Updated \ est_request_information.py:29\ to an explicit initialization check (\�ssert request_info.headers is not None).
    • Added unit tests in \ est_request_header.py\ verifying initialization and \�dd_all\ with empty mappings/collections.
  2. Iterable Initialization & Single-Value Headers:

    • Materialized iterable values into a list when delegating to \self.add(key, list_values)\ so that single-value headers (e.g., \Content-Length) properly retain only the first value.
    • Added regression test \ est_init_iterable_single_value_header.
  3. Subscript Assignment Replacement:

    • Implemented _setitem_\ to replace existing header values on assignment (case-insensitively) rather than appending, and validates the replacement before mutating stored headers. Also added _delitem_.
    • Added tests covering repeated assignments, case-insensitivity, validation, and single-value header assignment.

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!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

HeadersCollection usability

2 participants