Skip to content

Commit

Permalink
chore(internal): require explicit overrides (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Oct 26, 2023
1 parent 0ed884b commit 72f4339
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ exclude = [
".venv",
".nox",
]

reportImplicitOverride = true

reportImportCycles = false
reportPrivateUsage = false

Expand Down
4 changes: 3 additions & 1 deletion src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
overload,
)
from functools import lru_cache
from typing_extensions import Literal, get_args, get_origin
from typing_extensions import Literal, get_args, override, get_origin

import anyio
import httpx
Expand Down Expand Up @@ -1588,6 +1588,7 @@ class OtherPlatform:
def __init__(self, name: str) -> None:
self.name = name

@override
def __str__(self) -> str:
return f"Other:{self.name}"

Expand Down Expand Up @@ -1649,6 +1650,7 @@ class OtherArch:
def __init__(self, name: str) -> None:
self.name = name

@override
def __str__(self) -> str:
return f"other:{self.name}"

Expand Down
11 changes: 11 additions & 0 deletions src/anthropic/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import asyncio
from typing import Union, Mapping
from typing_extensions import override

import httpx
from tokenizers import Tokenizer # type: ignore[import]
Expand Down Expand Up @@ -123,10 +124,12 @@ def __init__(
self.completions = resources.Completions(self)

@property
@override
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
if self._api_key_auth:
return self._api_key_auth
Expand All @@ -149,13 +152,15 @@ def _bearer_auth(self) -> dict[str, str]:
return {"Authorization": f"Bearer {auth_token}"}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
return {
**super().default_headers,
"anthropic-version": "2023-06-01",
**self._custom_headers,
}

@override
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if self.api_key and headers.get("X-Api-Key"):
return
Expand Down Expand Up @@ -267,6 +272,7 @@ def count_tokens(
def get_tokenizer(self) -> Tokenizer:
return sync_get_tokenizer()

@override
def _make_status_error(
self,
err_msg: str,
Expand Down Expand Up @@ -375,10 +381,12 @@ def __init__(
self.completions = resources.AsyncCompletions(self)

@property
@override
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
if self._api_key_auth:
return self._api_key_auth
Expand All @@ -401,13 +409,15 @@ def _bearer_auth(self) -> dict[str, str]:
return {"Authorization": f"Bearer {auth_token}"}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
return {
**super().default_headers,
"anthropic-version": "2023-06-01",
**self._custom_headers,
}

@override
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if self.api_key and headers.get("X-Api-Key"):
return
Expand Down Expand Up @@ -522,6 +532,7 @@ async def count_tokens(
async def get_tokenizer(self) -> Tokenizer:
return await async_get_tokenizer()

@override
def _make_status_error(
self,
err_msg: str,
Expand Down
6 changes: 6 additions & 0 deletions src/anthropic/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Required,
TypedDict,
final,
override,
runtime_checkable,
)

Expand Down Expand Up @@ -59,20 +60,23 @@ class BaseModel(pydantic.BaseModel):
else:

@property
@override
def model_fields_set(self) -> set[str]:
# a forwards-compat shim for pydantic v2
return self.__fields_set__ # type: ignore

class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
extra: Any = pydantic.Extra.allow # type: ignore

@override
def __str__(self) -> str:
# mypy complains about an invalid self arg
return f'{self.__repr_name__()}({self.__repr_str__(", ")})' # type: ignore[misc]

# Override the 'construct' method in a way that supports recursive parsing without validation.
# Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836.
@classmethod
@override
def construct(
cls: Type[ModelT],
_fields_set: set[str] | None = None,
Expand Down Expand Up @@ -139,6 +143,7 @@ def construct(
# a specifc pydantic version as some users may not know which
# pydantic version they are currently using

@override
def model_dump(
self,
*,
Expand Down Expand Up @@ -187,6 +192,7 @@ def model_dump(
exclude_none=exclude_none,
)

@override
def model_dump_json(
self,
*,
Expand Down
2 changes: 2 additions & 0 deletions src/anthropic/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
from typing import TYPE_CHECKING, Any, Generic, Iterator, AsyncIterator
from typing_extensions import override

import httpx

Expand Down Expand Up @@ -161,6 +162,7 @@ def data(self) -> str:
def json(self) -> Any:
return json.loads(self.data)

@override
def __repr__(self) -> str:
return f"ServerSentEvent(event={self.event}, data={self.data}, id={self.id}, retry={self.retry})"

Expand Down
10 changes: 9 additions & 1 deletion src/anthropic/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
Optional,
Sequence,
)
from typing_extensions import Literal, Protocol, TypeAlias, TypedDict, runtime_checkable
from typing_extensions import (
Literal,
Protocol,
TypeAlias,
TypedDict,
override,
runtime_checkable,
)

import httpx
import pydantic
Expand Down Expand Up @@ -119,6 +126,7 @@ def get(timeout: Union[int, NotGiven, None] = NotGiven()) -> Response: ...
def __bool__(self) -> Literal[False]:
return False

@override
def __repr__(self) -> str:
return "NOT_GIVEN"

Expand Down
6 changes: 5 additions & 1 deletion src/anthropic/_utils/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from abc import ABC, abstractmethod
from typing import Generic, TypeVar, Iterable, cast
from typing_extensions import ClassVar
from typing_extensions import ClassVar, override

T = TypeVar("T")

Expand All @@ -21,16 +21,20 @@ def __init__(self) -> None:
def __getattr__(self, attr: str) -> object:
return getattr(self.__get_proxied__(), attr)

@override
def __repr__(self) -> str:
return repr(self.__get_proxied__())

@override
def __str__(self) -> str:
return str(self.__get_proxied__())

@override
def __dir__(self) -> Iterable[str]:
return self.__get_proxied__().__dir__()

@property # type: ignore
@override
def __class__(self) -> type:
return self.__get_proxied__().__class__

Expand Down
3 changes: 2 additions & 1 deletion src/anthropic/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Any, List, Mapping, TypeVar, cast
from datetime import date, datetime
from typing_extensions import Literal, get_args, get_type_hints
from typing_extensions import Literal, get_args, override, get_type_hints

from ._utils import (
is_list,
Expand Down Expand Up @@ -52,6 +52,7 @@ def __init__(
self.format = format
self.format_template = format_template

@override
def __repr__(self) -> str:
return f"{self.__class__.__name__}(alias='{self.alias}', format={self.format}, format_template='{self.format_template}')"

Expand Down

0 comments on commit 72f4339

Please sign in to comment.