Skip to content

fix(abstractions): safely handle missing values and normalize return in HeadersCollection.remove_value - #839

Merged
Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-collection-remove-value-handling
Oct 29, 2026
Merged

Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-collection-remove-value-handling

Conversation

@hardikkaurani

@hardikkaurani Hardik K (hardikkaurani) commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

In HeadersCollection.remove_value, calling remove_value with a value not present in an existing header previously caused an unhandled KeyError exception because set.remove(...) was invoked directly without verifying value membership. In contrast, calling remove_value on a non-existent header gracefully returned False.

Additionally, when removing the final remaining value of a header, remove_value previously returned set(), which evaluates to False in Python boolean contexts (bool(set()) == False), giving callers a false-negative status even though removal succeeded and the header was popped.

This PR addresses both issues:

  1. Safe Missing Value Handling: Verifies header_value in values before removing; returns False if the value is not present, avoiding unhandled KeyError exceptions.
  2. Normalized Return Semantics: Returns True when the value was removed and no values remain in the header (cleaning up the key from the internal mapping), returns the remaining set[str] when other values remain, and returns False when the header or value was not found.
  3. Documentation: Updated docstrings on remove_value and remove to accurately reflect their Union[bool, set[str]] return types.

Changes

  • packages/abstractions/kiota_abstractions/headers_collection.py:
    • Added membership check in remove_value to return False if header_value not in values.
    • Returned True when the header is fully cleared and removed after removing its last value.
    • Updated docstring return annotations for remove_value and remove.
  • packages/abstractions/tests/test_request_header.py:
    • Added test for removing a non-existent value from an existing header returning False without raising KeyError.
    • Added test verifying header name case-insensitivity on removal.
    • Added assertions for return semantics on multi-value removal, last-value removal, and header removal.

Testing

  • Verified with pytest packages/abstractions: 194/194 passed.
  • Verified with pylint kiota_abstractions: rated 10.00/10.
  • Verified with mypy kiota_abstractions: 0 issues found across 48 source files.
  • Verified code formatting with yapf and import sorting with isort.
  • Ran regression test suites across serialization and http packages.

@sonarqubecloud

Copy link
Copy Markdown

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.

🟢 Approval recommended

No active review issues block approval of the header-value removal changes.

No open issues 🎉

What changed in this PR

This PR makes header-value removal safer in Kiota’s abstractions layer and clarifies its return semantics.

Changes:

  • Returns False when a value is absent instead of raising KeyError.
  • Returns True after removing a header’s final value, while preserving the remaining-set return for partial removal.
  • Updates return-value documentation and adds regression coverage for missing values, case-insensitive header names, and removal results.
File Description
packages/​abstractions/​tests/​test_request_header.py Adds and strengthens assertions for removal behavior and return values.
packages/​abstractions/​kiota_abstractions/​headers_collection.py Safely handles absent values, fixes final-value removal results, and documents return types.

Review effort: Balanced

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Oct 29, 2026
@baywet
Vincent Biret (baywet) merged commit ec30d23 into microsoft:main Oct 29, 2026
69 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

3 participants