Expose HTTPRedirect default status #1611
Conversation
…ct.default_status class property.
status = int(status) | ||
if status < 300 or status > 399: | ||
raise ValueError('status must be between 300 and 399.') | ||
status = int(status) if status is not None else self.default_status |
webknjaz
Jul 12, 2017
Member
Why not assign self.status
right here?
Why not assign self.status
right here?
jaraco
Jul 26, 2017
Author
Member
Yes, I think that would be preferable.
Yes, I think that would be preferable.
raise ValueError('status must be between 300 and 399.') | ||
status = int(status) if status is not None else self.default_status | ||
if not 300 <= status <= 399: | ||
raise ValueError('status must be between 300 and 399.') | ||
|
||
self.status = status | ||
CherryPyException.__init__(self, abs_urls, status) |
webknjaz
Jul 12, 2017
Member
Can we use super()
?
Can we use super()
?
jaraco
Jul 26, 2017
Author
Member
Yes, perhaps, but that's outside the scope of this PR.
Yes, perhaps, but that's outside the scope of this PR.
@@ -123,6 +123,9 @@ class Root: | |||
import six | |||
from six.moves import urllib | |||
|
|||
from jaraco.classes.properties import classproperty |
webknjaz
Jul 12, 2017
Member
I'd move it before six
preserving alphabetic order
I'd move it before six
preserving alphabetic order
jaraco
Jul 26, 2017
Author
Member
I've been using a convention of creating four sections for imports: stdlib, compatibility (stdlib functionality but from third-party modules), third-party modules, and finally intrapackage imports. Perhaps my convention diverges slightly from the more common convention of putting all third-party imports (even six) in the same group.
I've been using a convention of creating four sections for imports: stdlib, compatibility (stdlib functionality but from third-party modules), third-party modules, and finally intrapackage imports. Perhaps my convention diverges slightly from the more common convention of putting all third-party imports (even six) in the same group.
webknjaz
Jul 26, 2017
Member
No, it's okay. It's good that you explained your way of structuring imports. I think makes sense, we should probably document this approach in contribution docs to make everyone follow the same code style.
No, it's okay. It's good that you explained your way of structuring imports. I think makes sense, we should probably document this approach in contribution docs to make everyone follow the same code style.
Another dependency.. And also a non standard language concept of class property. |
|
||
self.status = status | ||
CherryPyException.__init__(self, abs_urls, status) | ||
|
||
@classproperty | ||
def default_status(cls): |
jaraco
Jul 26, 2017
Author
Member
codacy fail - this is a classmethod, so the convention is cls
.
codacy fail - this is a classmethod, so the convention is cls
.
|
||
self.status = status | ||
CherryPyException.__init__(self, abs_urls, status) | ||
|
||
@classproperty | ||
def default_status(cls): |
jaraco
Jul 26, 2017
Author
Member
codacy fail - it can't be a static method because it's a class property.
codacy fail - it can't be a static method because it's a class property.
It is a non-standard concept; i.e. it's not in the stdlib. I personally think it should be as it's a fairly basic concept in OO, even if only occasionally used. Yes, the additional dependency makes me a little sad, but not as sad as a copy/paste of the implementation (and subsequent maintenance burden). That is, if we assume a classproperty is desired. I believe a classproperty is desired. It would be trivial to make that @Safihre : Classproperty details aside, what interface do you think is best for the end user who wishes to resolve the default status for a redirect? |
What is so wrong with that? |
It's normally applied to instances, not classes. It's pythonic way to use |
df9be29
to
c92503e
Codecov Report
@@ Coverage Diff @@
## master #1611 +/- ##
==========================================
- Coverage 77.56% 77.12% -0.45%
==========================================
Files 106 106
Lines 14374 14372 -2
==========================================
- Hits 11149 11084 -65
- Misses 3225 3288 +63 |
While adapting some internal code, I had a situation where I wanted to handle the response directly rather than raising an HTTPRedirect, but I still wanted to use the same response code. After inspecting the logic for how HTTPRedirect calculates the appropriate response code for a redirect, I realized I wanted to re-use this logic rather than copy/paste it.
This change extracts that logic into the
HTTPRedirect.default_status
class property, such that it can be used by a third party application even if HTTPRedirect itself isn't used.I'm a little sad this change adds a new dependency for the classproperty, but since it's not stdlib, that's the best option.