-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
Refactored method Flask.make_response to improve readability #1676
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
Conversation
flask/app.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
dbb826f to
e672db5
Compare
I respectfully disagree. I think using Reading the function, I felt it was unnecessarily convoluted and it was difficult to follow all of the possible paths in the code. IMHO my refactoring turns the function into a simple series of |
|
the origin rv tuple after append None so the 3 items is |
|
@guyskk Yes, I understand the idea in I just think the same thing can be achieved with a little more code and far better readability. As demonstrated in the PR. IMHO this function could be implemented simply with a series of |
|
Your implement of _make_response_from_tuple is too verbose, you need not split it into some functions, it just a little bit of code and you make it complicated. What's more, you should not change code and tests in the same commit, can your refactor pass the origin tests? The 2th item can be string(status) or int(statuc_code), I wonder you refactor changed this behavior. |
Why? By doing so you have working code with passing tests in each commit. |
|
this is a refactor other than new feature,so the behavior shouldn't change,and should pass the origin tests. |
|
@guyskk I agree, I might have refactored into too many sub-functions. Other than that, IMHO the refactoring is good because it gives the function a structure of simple The 'rules' governing the behavior of the function are now visible to the eye, as opposed to the way the original is structured. However as I said, I think you are right that I refactored into too many function levels. @ThiefMaster I am updating the PR to split into less sub-functions. |
The method is now factored to a number of submethods, each corresponding to a type of object the view function may return. The docstring for the method has also been improved and contains a more detailed explanation for what can be returned from a view function. Also refactored the test for response creation.
|
Thanks for looking into this. I agree that it was hard to follow. However, this introduced a bug where I expanded on your idea by breaking out the tuple unpacking, adding more error messages, improving the documentation, and adding more tests. See #2256. |
* be explicit about how tuples are unpacked * allow bytes for status value * allow Headers for headers value * use TypeError instead of ValueError * errors are more descriptive * document that view must not return None * update documentation about return values * test more response types * test error messages closes pallets#1676
The method now looks at the type of the object returned from the view function, and returns a
self.response_classobject based on that type.The docstring for the method has also been improved and contains a more
detailed explanation for what can be returned from a view function.
Also refactored the test for response creation.