FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Retry constructors methods support None by daniel-sanche · Pull Request #592 · googleapis/python-api-core · GitHub

This repository was archived by the owner on Feb 23, 2026. It is now read-only.
/ python-api-core Public archive

fix: Retry constructors methods support None - #592

Merged
daniel-sanche merged 8 commits into
mainfrom
fix_replace
Feb 5, 2024
Merged

fix: Retry constructors methods support None#592
daniel-sanche merged 8 commits into
mainfrom
fix_replace

Conversation

Copy link
Copy Markdown
Contributor

Fixes #591

v2.16.0 did some refactoring of the Retry class, including adding a _replace helper method to simplify the constructors with_deadline, with_timeout, with_predicate, and with_delay. But it was noticed there is a bug in the implementation, where falsy values are ignored, with the existing value silently used instead.

Note that None is technically not supported by the type annotations, and 0 is typically not a valid value for these fields, which is likely why there was a gap in the tests around this. But the annotations were added recently, and existing code use pass None in for a timeouts, and None is supported by the internal retry code down the stack. For this reason, I changed the type annotation for the timeout field to support None as part of this fix, and added tests to ensure that it will be supported in the future

daniel-sanche requested review from a team January 31, 2024 23:54
product-auto-label Bot added the size: m Pull request size is medium. label Jan 31, 2024

vchudnov-g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some questions and test suggestions, but the core functionality LGTM.

Comment on lines +153 to +154
if not use_deadline
else retry_.with_deadline(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Under the hood, with_deadline is just calling with_timeout, so these two branches wind up testing the same code, module the trivial wrapping function. Right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, I believe so. I think the intention is to make sure they stay in-line through any potential future refactors

"""
return self._replace(predicate=predicate)
return type(self)(
predicate=predicate,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Are we allowing predicate to be None here? If so, we would need to tweak the type annotation, and to add a test for this edge case? (The test might be starting with something with a non-trivial predicate, and then verifying that calling .with_predicate(None) sets it to None?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It seems like predicate has always been a required variable

vchudnov-g Feb 5, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Well, in _replace, we had predicate: Callable[[Exception], bool] | None = None,, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry, I missed this comment

In the _replace helper, None represented a variable that isn't replaced (that is, it used the same value as the original Retry object).

But this doesn't work if users are passing in None for a with_timeout and expecting it to disable timeouts, which wasn't supported by the type annotations, but was used in practice. Which was the motivation for this change

return self._replace(initial=initial, maximum=maximum, multiplier=multiplier)
return type(self)(
predicate=self._predicate,
initial=initial if initial is not None else self._initial,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If these values are not None but still falsy, we still want to use them? If so, we should capture this in a test since this differs from the previous behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah it differs from the previous behaviour, but it is in line with the pre-2.16.0 behaviour

Good point on the test. I had added one for timeout, but not for this one. Fixed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@daniel-sanche Please could you link the specific commit for the fix in #592 (comment)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

sorry, forgot to push. The new test should be there now

parthea assigned daniel-sanche and unassigned parthea and vchudnov-g Feb 4, 2024
daniel-sanche enabled auto-merge (squash) February 5, 2024 22:07
daniel-sanche merged commit 416203c into main Feb 5, 2024
daniel-sanche deleted the fix_replace branch February 5, 2024 22:07
This was referenced May 30, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API ignores user defined timeout and applies default timeout

3 participants


Back | FazBrowse Home | New Git URL