-
Notifications
You must be signed in to change notification settings - Fork 560
fix: handle math.inf as max_retries without TypeError crash #1282
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -368,7 +368,7 @@ class BaseClient(Generic[_HttpxClientT, _DefaultStreamT]): | |
| _client: _HttpxClientT | ||
| _version: str | ||
| _base_url: URL | ||
| max_retries: int | ||
| max_retries: int | float | ||
| timeout: Union[float, Timeout, None] | ||
| _strict_response_validation: bool | ||
| _idempotency_header: str | None | ||
|
|
@@ -380,7 +380,7 @@ def __init__( | |
| version: str, | ||
| base_url: str | URL, | ||
| _strict_response_validation: bool, | ||
| max_retries: int = DEFAULT_MAX_RETRIES, | ||
| max_retries: int | float = DEFAULT_MAX_RETRIES, | ||
| timeout: float | Timeout | None = DEFAULT_TIMEOUT, | ||
| custom_headers: Mapping[str, str] | None = None, | ||
| custom_query: Mapping[str, object] | None = None, | ||
|
|
@@ -913,7 +913,7 @@ def __init__( | |
| *, | ||
| version: str, | ||
| base_url: str | URL, | ||
| max_retries: int = DEFAULT_MAX_RETRIES, | ||
| max_retries: int | float = DEFAULT_MAX_RETRIES, | ||
| timeout: float | Timeout | None | NotGiven = not_given, | ||
| http_client: httpx.Client | None = None, | ||
| custom_headers: Mapping[str, str] | None = None, | ||
|
|
@@ -1047,7 +1047,15 @@ def request( | |
| max_retries = input_options.get_max_retries(self.max_retries) | ||
|
|
||
| retries_taken = 0 | ||
| for retries_taken in range(max_retries + 1): | ||
| # Cap max_retries for range() — math.inf is a float and cannot be passed | ||
| # to range(). The error message in __init__ advertises math.inf as valid, | ||
| # so we must handle it here. Use sys.maxsize as the effective upper bound. | ||
| # Also wrap with int() to handle finite floats like 2.0 (range(3.0) raises TypeError). | ||
| if max_retries == math.inf or max_retries >= sys.maxsize: | ||
| range_limit = sys.maxsize | ||
| else: | ||
| range_limit = int(max_retries) + 1 | ||
| for retries_taken in range(range_limit): | ||
|
Comment on lines
+1050
to
+1058
|
||
| options = model_copy(input_options) | ||
| options = self._prepare_options(options) | ||
|
|
||
|
|
@@ -1552,7 +1560,7 @@ def __init__( | |
| version: str, | ||
| base_url: str | URL, | ||
| _strict_response_validation: bool, | ||
| max_retries: int = DEFAULT_MAX_RETRIES, | ||
| max_retries: int | float = DEFAULT_MAX_RETRIES, | ||
| timeout: float | Timeout | None | NotGiven = not_given, | ||
| http_client: httpx.AsyncClient | None = None, | ||
| custom_headers: Mapping[str, str] | None = None, | ||
|
|
@@ -1687,7 +1695,13 @@ async def request( | |
| max_retries = input_options.get_max_retries(self.max_retries) | ||
|
|
||
| retries_taken = 0 | ||
| for retries_taken in range(max_retries + 1): | ||
| # Cap max_retries for range() — math.inf is a float and cannot be passed | ||
| # to range(). Also wrap with int() to handle finite floats like 2.0 (range(3.0) raises TypeError). | ||
| if max_retries == math.inf or max_retries >= sys.maxsize: | ||
| range_limit = sys.maxsize | ||
| else: | ||
| range_limit = int(max_retries) + 1 | ||
| for retries_taken in range(range_limit): | ||
| options = model_copy(input_options) | ||
| options = await self._prepare_options(options) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the attribute annotation to
max_retries: int | floathelps communicate support formath.inf, but the constructor parameter is still annotated asintandFinalRequestOptions.get_max_retries()is typed to take/returnint(seesrc/anthropic/_models.py). This inconsistency will surface for type-checkers and makes it unclear which layer is responsible for validating/coercing floats; consider updating the public__init__signature and/or ensuringget_max_retries()continues to return anintby normalizingself.max_retriesat assignment time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed — updated
__init__parameter types inBaseClient,SyncAPIClient, andAsyncAPIClienttoint | float. Also updatedFinalRequestOptions.get_max_retries()return type.