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

Improve RegisteredResponse init doc & signature #1126

Merged
merged 4 commits into from
Jan 14, 2025

Conversation

sirosen
Copy link
Member

@sirosen sirosen commented Jan 10, 2025

RegisteredResponse.__init__ allowed any keyword arguments via **kwargs usage, but would later pass these to responses.Response via the add() or replace() methods, at which point there could be a TypeError.

This can make it hard to understand what features are being exposed during development. e.g., "Was the parameter name status or status_code?"

To resolve and provide an improved experience for development with RegisteredResponse, the init parameters are better documented and the signature is improved to explicitly enumerate supported args.

  1. Add :param ...: docstring items
  2. Explicitly enumerate parameters, removing **kwargs: Any
  3. Document which parameters come from responses.Response, which come from responses.BaseResponse, and which ones are omitted (and why)
  4. Update service and method to use Literals to show the values

In terms of lost functionality, technically the following three parameters are removed in this change:

- auto_calculate_content_length
- passthrough
- match_querystring

However, given that _testing has a weaker backwards compatibility contract than the rest of the SDK and there are no known usages for these, this seems like a good change rather than a bad one. We can always add auto_calculate_content_length in the future. The other two would require stronger justification, since

- match_querystring is deprecated in `responses`
- passthrough doesn't seem applicable to any normal uses of `globus_sdk._testing`
- we don't forbid anyone from using `responses` directly

I was motivated to tackle this while playing with some editor support for code completion and docstring inspection, and I found that RegisteredResponse didn't have the information I wanted.
In particular, I wanted to make sure I was using correct parameter names, and the loaded class defintion and docstring didn't provide this information.


📚 Documentation preview 📚: https://globus-sdk-python--1126.org.readthedocs.build/en/1126/

`RegisteredResponse.__init__` allowed any keyword arguments via
`**kwargs` usage, but would later pass these to `responses.Response`
via the `add()` or `replace()` methods, at which point there would be
a `TypeError`.

This can make it hard to understand what features are being exposed
during development. e.g., "Was the parameter name `status` or
`status_code`?"

To resolve and provide an improved experience for development with
`RegisteredResponse`, the init parameters are better documented and
the signature is improved to explicitly enumerate supported args.

1. Add `:param ...:` docstring items
2. Explicitly enumerate parameters, removing `**kwargs: Any`
3. Document which parameters come from `responses.Response`, which
   come from `responses.BaseResponse`, and which ones are omitted (and
   why)
4. Update `service` and `method` to use Literals to show the values

In terms of lost functionality, technically the following three
parameters are removed in this change:

    - auto_calculate_content_length
    - passthrough
    - match_querystring

However, given that `_testing` has a weaker backwards compatibility
contract than the rest of the SDK and there are no known usages for
these, this seems like a good change rather than a bad one. We can
always add `auto_calculate_content_length` in the future. The other
two would require stronger justification, since
- match_querystring is deprecated in `responses`
- passthrough doesn't seem applicable to any normal uses of
  `globus_sdk._testing`
- we don't forbid anyone from using `responses` directly
@sirosen sirosen added the no-news-is-good-news This change does not require a news file label Jan 10, 2025
service: str | None = None,
method: str = responses.GET,
service: (
Literal[
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we have a pyupgrade configuration issue: above, Literal is imported conditionally for Python 3.7 and below, which we no longer support.

This should become t.Literal in a subsequent PR.

src/globus_sdk/_testing/models.py Outdated Show resolved Hide resolved
src/globus_sdk/_testing/models.py Outdated Show resolved Hide resolved
src/globus_sdk/_testing/models.py Outdated Show resolved Hide resolved
sirosen and others added 3 commits January 13, 2025 15:14
Compare in a test against the options supported by `http.HTTPMethod`,
and thereby compare against the strings supported by `responses`.

Add `OPTIONS`, `CONNECT`, and `TRACE` for completeness.
In order to ensure that we aren't overriding a potentially new default
in `responses` by passing `()` explicitly to it, simply default to
`None` and only pass a value if it is non-null.

Also refine docstrings.
@sirosen sirosen merged commit 12497e4 into globus:main Jan 14, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-news-is-good-news This change does not require a news file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants