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

Set response attribute length_remaining in BaseHTTPResponse #3317

Merged
merged 2 commits into from Jan 29, 2024

Conversation

programmer-ke
Copy link
Contributor

@programmer-ke programmer-ke commented Jan 25, 2024

Closes #3315

Since both HTTPResponse and HTTP2Response use the length_remaining, make it an attribute of the base class.

@programmer-ke
Copy link
Contributor Author

Changelog test is failing. I don't think a changelog is needed for this since it is invisible to users?

@pquentin pquentin added the Skip Changelog Pull requests that don't require a changelog entry label Jan 25, 2024
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this!

length_remaining needs to be passed in the __init__ method without any default value as is done for status or version. Then in HTTP2 response we can call super().__init__(status=status, ..., length_remaining=0). Does that make sense?

@programmer-ke
Copy link
Contributor Author

Thanks for tackling this!

length_remaining needs to be passed in the __init__ method without any default value as is done for status or version. Then in HTTP2 response we can call super().__init__(status=status, ..., length_remaining=0). Does that make sense?

That does make sense. Now, in the case of HTTPResponse, length_remaining is initialized by the _init_length. We'd call this before calling super().__init__ passing in the calculated length. However _init_length in turn relies on a few attributes set in the super's __init__.

self.length_remaining = self._init_length(request_method)

How can we handle of this? The easy way would be to simply call super with an initial length_remaining of 0 then updating it with the calculated value after super().__init__.

@programmer-ke
Copy link
Contributor Author

length_remaining needs to be passed in the __init__ method without any default value as is done for status or version. Then in HTTP2 response we can call super().__init__(status=status, ..., length_remaining=0). Does that make sense?

I've now made it a parameter of BaseHTTPRequest's __init__, what do you think?

Also I've run the CI twice the tests seem to be failing on random network issues.

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks for the proof of concept! I'm not sure what to think. Setting to 0 and then to the real value seems pretty bad, but passing all required values to init_length seems pretty bad too. I'm sorry that I failed to see that, and for now this changed looks a like net negative to me. :/

I'll wait to see if another reviewer thinks differently.

Copy link
Member

@illia-v illia-v left a comment

Choose a reason for hiding this comment

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

What do you think about adding an annotation length_remaining: int | None without any default value to BaseHTTPResponse?

@programmer-ke
Copy link
Contributor Author

What do you think about adding an annotation length_remaining: int | None without any default value to BaseHTTPResponse?

Possible. So do we still need to pass its value as an argument to __init__? If so we'll always have to provide some value there i.e. super().__init__(..., length_remaining=0) , or otherwise specify a default value for the parameter.

@sethmlarson
Copy link
Member

sethmlarson commented Jan 27, 2024

@programmer-ke I agree with @illia-v's approach, let's type annotate BaseHTTPResponse and keep the default value of 0 only on the new HTTP2Response.

@programmer-ke
Copy link
Contributor Author

Made an update, now setting the length_remaining in the base class without a default value.

Also, there's some flakiness in the tests I think

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

@illia-v illia-v merged commit 6d2f0f6 into urllib3:main Jan 29, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip Changelog Pull requests that don't require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move length_remaining into BaseHTTPResponse
4 participants