Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions json_logging/framework/fastapi/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ def get_method(self, request: starlette.requests.Request):
return request.method

def get_remote_ip(self, request: starlette.requests.Request):
return request.client.host
if request.client:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
if request.client:
if request.client is not None:
References
  1. 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)

return request.client.host
return json_logging.EMPTY_VALUE

def get_remote_port(self, request: starlette.requests.Request):
return request.client.port
if request.client:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
if request.client:
if request.client is not None:
References
  1. 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)

return request.client.port
return json_logging.EMPTY_VALUE


class FastAPIResponseInfoExtractor(BaseResponseInfoExtractor):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,22 @@ def test_excluded_from_request_instrumentation(client_and_log_handler):

assert response.status_code == 200
assert len(handler.messages) == 0


def test_request_info_extractor_handles_missing_client():
"""Test if missing request client information falls back to empty values"""
import json_logging
from json_logging.framework.fastapi.implementation import FastAPIRequestInfoExtractor

request = fastapi.Request({
"type": "http",
"method": "GET",
"path": "/",
"headers": [],
"query_string": b"",
"client": None,
})
extractor = FastAPIRequestInfoExtractor()

assert extractor.get_remote_ip(request) == json_logging.EMPTY_VALUE
assert extractor.get_remote_port(request) == json_logging.EMPTY_VALUE