Skip to content

fix: more consistent naming of HTTP status code related attributes #185

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

Merged
merged 2 commits into from
Jan 22, 2024

Conversation

pyrooka
Copy link
Member

@pyrooka pyrooka commented Jan 18, 2024

This commit fixes a small inconsistency in the naming
of the HTTP status code attributes between the successful
and the error responses.

@pyrooka pyrooka force-pushed the nb/fix-retry-and-naming branch from 2d0f9dd to ccc5a9e Compare January 18, 2024 17:11
@pyrooka pyrooka changed the title fix: replace deprecated Retry attribute and more consistent naming fix: more consistent naming of HTTP status code related attributes Jan 18, 2024
@@ -39,15 +40,28 @@ def __init__(self, code: int, *, message: Optional[str] = None, http_response: O
# Call the base class constructor with the parameters it needs
super().__init__(message)
self.message = message
self.code = code
self.status_code = code
Copy link
Member Author

Choose a reason for hiding this comment

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

We use the same name in the DetailedResponse and I think it's a bit more clear than the simple code.

Comment on lines +50 to +62
# pylint: disable=fixme
# TODO: delete this by the end of 2024.
@property
def code(self):
"""The old `code` property with a deprecation warning."""

warnings.warn(
'Using the `code` attribute on the `ApiException` is deprecated and'
'will be removed in the future. Use `status_code` instead.',
DeprecationWarning,
)
return self.status_code

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought it would be good to not remove the old property completely, but mark it deprecated and give some time to the the users to update their code. Also, the date in the comment is arbitrary, there is no real reason behind it. :)

Copy link
Member

Choose a reason for hiding this comment

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

By renaming the field from code to status_code above, I think you've introduced a breaking change despite the addition of this new getter function.
What I mean is... previously a user would need to access the code field like this:

    print('The status code is: ', exception.code)

That will no longer work as the user would need to do one of the following instead now:

    print('The status code is: ', exception.status_code)

or

    print('The status code is: ', exception.code())

IOW, I don't think the user can continue to just use exception.code, can they???

Copy link
Member Author

@pyrooka pyrooka Jan 19, 2024

Choose a reason for hiding this comment

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

Fortunately that's not the case. In short, the @property decorator creates a valid attribute (well only with a getter by default), so it can be used as before. See the example below;

>>> ex = ApiException(404, message="Not found...")
>>> ex.code
404
>>> ex.status_code
404

The only difference is, when calling the code property, a deprecation message will be printed to the stderr at warning level.

Copy link
Member

Choose a reason for hiding this comment

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

Aha! I didn't notice the @property decorator, and I probably wouldn't have known what that was for even if I did 😂.

@pyrooka pyrooka requested a review from padamstx January 18, 2024 17:26
Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

I left a couple of comments re: whether this is a breaking change or not. Let's resolve this before merging.

Comment on lines +50 to +62
# pylint: disable=fixme
# TODO: delete this by the end of 2024.
@property
def code(self):
"""The old `code` property with a deprecation warning."""

warnings.warn(
'Using the `code` attribute on the `ApiException` is deprecated and'
'will be removed in the future. Use `status_code` instead.',
DeprecationWarning,
)
return self.status_code

Copy link
Member

Choose a reason for hiding this comment

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

By renaming the field from code to status_code above, I think you've introduced a breaking change despite the addition of this new getter function.
What I mean is... previously a user would need to access the code field like this:

    print('The status code is: ', exception.code)

That will no longer work as the user would need to do one of the following instead now:

    print('The status code is: ', exception.status_code)

or

    print('The status code is: ', exception.code())

IOW, I don't think the user can continue to just use exception.code, can they???

@@ -398,7 +398,7 @@ def test_request_success_invalid_json():
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
prepped = service.prepare_request('GET', url='')
service.send(prepped)
assert err.value.code == 200
assert err.value.status_code == 200
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be good to have a testcase that still uses the old way err.value.code to make sure that still works (see my previous comment about this above)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, I'll add some!

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

Since the @Property decorator allows one to use the getter just like a field, it looks like there's no breaking change.
Assuming you add a testcase to exercise that ("trust, but verify" 😂), I'll approve now.

@pyrooka pyrooka force-pushed the nb/fix-retry-and-naming branch from f249880 to 09be05d Compare January 22, 2024 13:03
@pyrooka pyrooka merged commit cf74671 into main Jan 22, 2024
@pyrooka pyrooka deleted the nb/fix-retry-and-naming branch January 22, 2024 14:08
ibm-devx-sdk pushed a commit that referenced this pull request Jan 22, 2024
# [3.19.0](v3.18.2...v3.19.0) (2024-01-22)

### Features

* rename `ApiException.code` to `ApiException.status_code` ([#185](#185)) ([cf74671](cf74671))
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 3.19.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants