fix: handle missing FastAPI request client#116
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesFastAPI client None guard
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR Summary by QodoFix FastAPI extractor when request.client is missing Description
Diagram
High-Level Assessment
Files changed (2)
|
There was a problem hiding this comment.
Code Review
This pull request updates the FastAPI request info extractor to safely handle missing request client information by returning a default empty value, and adds a corresponding unit test. The review feedback suggests explicitly checking if request.client is not None: instead of relying on truthiness (if request.client:), which aligns with PEP 8 recommendations for checking variables that default to None and prevents potential issues if the client object evaluates to False in a boolean context.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
|
||
| def get_remote_ip(self, request: starlette.requests.Request): | ||
| return request.client.host | ||
| if request.client: |
There was a problem hiding this comment.
According to PEP 8, when testing whether a variable that defaults to None was set to some other value, you should explicitly compare it using is not None rather than relying on truthiness. This is especially important for objects that are containers (like NamedTuple / tuple which request.client is), as they could potentially evaluate to False in a boolean context if empty.
| if request.client: | |
| if request.client is not None: |
References
- PEP 8: Programming Recommendations - Comparisons to singletons like None should always be done with is or is not, never the equality operators. Also, beware of writing if x when you really mean if x is not None. (link)
|
|
||
| def get_remote_port(self, request: starlette.requests.Request): | ||
| return request.client.port | ||
| if request.client: |
There was a problem hiding this comment.
According to PEP 8, when testing whether a variable that defaults to None was set to some other value, you should explicitly compare it using is not None rather than relying on truthiness. This is especially important for objects that are containers (like NamedTuple / tuple which request.client is), as they could potentially evaluate to False in a boolean context if empty.
| if request.client: | |
| if request.client is not None: |
References
- PEP 8: Programming Recommendations - Comparisons to singletons like None should always be done with is or is not, never the equality operators. Also, beware of writing if x when you really mean if x is not None. (link)
Summary
request.clientisNone.AttributeError.Fixes #90
Verification
python -m pytest tests/test_fastapi.pypython -m pytest --ignore tests/smoketestspython -m flake8 json_logging/framework/fastapi/implementation.py tests/test_fastapi.py --count --select=E9,F63,F7,F82 --show-source --statisticsNotes
Summary by CodeRabbit
Bug Fixes
Tests