-
Notifications
You must be signed in to change notification settings - Fork 0
feat: make Client.headers case-insensitive #54
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
base: main
Are you sure you want to change the base?
Conversation
Code Review: feat: make Client.headers case-insensitiveSummaryThis PR adds case-insensitive header access to Positive Aspects ✅
Issues Found 🐛1. Missing dict methods (Medium Priority)
Impact: Users might expect to SET headers case-insensitively too, not just read them. However, since the getter creates a new dict each time (line 227), modifications don't affect the underlying client anyway. This is actually correct behavior (prevents confusion), but could be documented. Recommendation:
2. Property getter creates new dict on every access (Performance)Line 227: This creates a new wrapper dict on EVERY access. While unlikely to be a bottleneck, repeated access in loops could be inefficient: for _ in range(1000):
val = client.headers["Content-Type"] # Creates 1000 dict wrappersRecommendation: Document this behavior or consider caching if performance becomes an issue. 3. Missing type annotations (Low Priority)
def __getitem__(self, key): # Should be: (self, key: str) -> strRecommendation: Add type annotations for IDE support and type checking consistency. 4. No test for items(), keys(), values() (Low Priority)While these methods work (they inherit from dict), the test doesn't verify that iterating over headers works correctly. Recommendation: Add test cases: assert list(client.headers.keys()) == ["x-custom", "content-type"]
assert list(client.headers.items()) == [("x-custom", "value"), ...]Security Considerations 🔒No security concerns identified. The case-insensitive lookup matches HTTP/2 spec requirements. Performance Considerations ⚡
Suggestions for Improvement 💡
@property
def headers(self) -> dict[str, str]:
"""
Headers configured for this client (case-insensitive access).
Note: Returns a read-only view. To modify headers, assign a new dict:
client.headers = {"X-Custom": "value"}
Case-insensitive access:
client.headers["content-type"] # Works
client.headers["Content-Type"] # Also works
"""
def __getitem__(self, key: str) -> str:
return super().__getitem__(key.lower())
def __contains__(self, key: str) -> bool: # type: ignore[override]
return super().__contains__(key.lower())
def get(self, key: str, default: str | None = None) -> str | None:
return super().get(key.lower(), default)
def __setitem__(self, key: str, value: str) -> None:
raise TypeError(
"Cannot modify headers directly. Use 'client.headers = {...}' instead."
)Test Coverage ✅
VerdictApproved with minor suggestions ✅ The implementation is solid and solves the stated problem. The issues identified are mostly about edge cases and documentation rather than core functionality. The PR is ready to merge after considering the suggestions above (particularly adding type hints and improving documentation). Great work maintaining backward compatibility while improving the API ergonomics! Review generated by Claude Code |
|
but @claude as CaseInsensitiveDict subclasses |
|
Claude finished @thomasht86's task in 54s —— View job Clarifying the
|
Add __setitem__, __delitem__, pop, and setdefault methods to ensure full case-insensitive behavior for all dict operations. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
PR Review: Make Client.headers Case-InsensitiveSummaryThis PR adds a ✅ Strengths
🔍 Issues & Concerns1. Inconsistency with existing architectureThe codebase already has
Recommendation: Consider either:
2.
|
Summary
CaseInsensitiveDictwrapper class that allows case-insensitive key accessClient.headersproperty to return headers wrapped inCaseInsensitiveDictclient.headers["X-Custom"],client.headers["x-custom"], andclient.headers["X-CUSTOM"]all work nowTest plan
test_client_headers_case_insensitivetest covering__getitem__,__contains__, andget()methods🤖 Generated with Claude Code