Skip to content
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

Implements Response.from_response and support for cf opts in Py fetch. #3229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dom96
Copy link
Collaborator

@dom96 dom96 commented Dec 10, 2024

While going through our examples I noticed that these were missing.

@dom96 dom96 requested review from a team as code owners December 10, 2024 17:57
):
options = {
"status": status.value if isinstance(status, HTTPStatus) else status,
}
if len(statusText) > 0:
options["statusText"] = statusText
if len(status_text) > 0:
Copy link
Member

Choose a reason for hiding this comment

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

This does the same thing afaict:

Suggested change
if len(status_text) > 0:
if status_text:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Explicit is always better than implicit

Copy link
Contributor

Choose a reason for hiding this comment

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

Well it's most common in python code to check if a container is non empty with if container:. I think you'll find this suggested in most style guides as the preferred way.

@@ -117,15 +139,25 @@ def __init__(
)
super().__init__(js_resp.url, js_resp)

@classmethod
def from_response(cls, body: Body, response: "Response") -> "Response":
Copy link
Contributor

Choose a reason for hiding this comment

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

from_response is a bit of an odd name. Is there an existing api like this? I would think to make it a method called replace_body. e.g,

def replace_body(self, new_body: Body) -> "Response":
    """
    Returns a new Response object with the same headers but an updated body
    """"

@@ -99,7 +121,7 @@ def __init__(
self,
body: Body,
status: HTTPStatus | int = HTTPStatus.OK,
statusText="",
status_text="",
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting is inconsistent here, does our lint check not check formatting?

Comment on lines +145 to +147
result = cls.__new__(cls)
js_resp = js.Response.new(b, response.js_object)
super(Response, result).__init__(js_resp.url, js_resp)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain the reasoning for this? Ideally we should switch to:

Suggested change
result = cls.__new__(cls)
js_resp = js.Response.new(b, response.js_object)
super(Response, result).__init__(js_resp.url, js_resp)
js_resp = js.Response.new(b, response.js_object)
return cls(js_resp.url, js_resp)

or type(self)(js_resp.url, js_resp) assuming we make it into an instance method. If this is different in some important way we'll need a comment. Particularly, I would expect super(cls) instead of super(Response) which skips the __init__ functions of any subclasses.

Comment on lines +54 to +58
# * that other options can be passed into `fetch` (so that we can support
# new options without updating this code)
resp = await fetch(
"https://example.com/redirect", redirect="manual", foobarbaz=42
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we could test that we receive foobarbaz on the other side by mocking the JS fetch. In this case, we just test that we don't crash with this option but we could discard it without failing the test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants