From 7bb6c60399f608d947c2895837f01158f443b562 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Thu, 26 Oct 2023 16:05:09 +0000 Subject: [PATCH] chore(internal): require explicit overrides --- pyproject.toml | 3 +++ src/anthropic/_base_client.py | 4 +++- src/anthropic/_client.py | 11 +++++++++++ src/anthropic/_models.py | 6 ++++++ src/anthropic/_streaming.py | 2 ++ src/anthropic/_types.py | 10 +++++++++- src/anthropic/_utils/_proxy.py | 6 +++++- src/anthropic/_utils/_transform.py | 3 ++- 8 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0c9a0244..ec10c5ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,9 @@ exclude = [ ".venv", ".nox", ] + +reportImplicitOverride = true + reportImportCycles = false reportPrivateUsage = false diff --git a/src/anthropic/_base_client.py b/src/anthropic/_base_client.py index 536881ae..3ce3bbd9 100644 --- a/src/anthropic/_base_client.py +++ b/src/anthropic/_base_client.py @@ -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 @@ -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}" @@ -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}" diff --git a/src/anthropic/_client.py b/src/anthropic/_client.py index 07b32a97..2c014dc6 100644 --- a/src/anthropic/_client.py +++ b/src/anthropic/_client.py @@ -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] @@ -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 @@ -149,6 +152,7 @@ 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, @@ -156,6 +160,7 @@ def default_headers(self) -> dict[str, str | Omit]: **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 @@ -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, @@ -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 @@ -401,6 +409,7 @@ 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, @@ -408,6 +417,7 @@ def default_headers(self) -> dict[str, str | Omit]: **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 @@ -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, diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index 4e4107f5..7b0772e4 100644 --- a/src/anthropic/_models.py +++ b/src/anthropic/_models.py @@ -11,6 +11,7 @@ Required, TypedDict, final, + override, runtime_checkable, ) @@ -59,6 +60,7 @@ 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 @@ -66,6 +68,7 @@ def model_fields_set(self) -> set[str]: 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] @@ -73,6 +76,7 @@ def __str__(self) -> str: # 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, @@ -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, *, @@ -187,6 +192,7 @@ def model_dump( exclude_none=exclude_none, ) + @override def model_dump_json( self, *, diff --git a/src/anthropic/_streaming.py b/src/anthropic/_streaming.py index 510e290b..e399257e 100644 --- a/src/anthropic/_streaming.py +++ b/src/anthropic/_streaming.py @@ -3,6 +3,7 @@ import json from typing import TYPE_CHECKING, Any, Generic, Iterator, AsyncIterator +from typing_extensions import override import httpx @@ -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})" diff --git a/src/anthropic/_types.py b/src/anthropic/_types.py index 4ef60c49..f2da5ace 100644 --- a/src/anthropic/_types.py +++ b/src/anthropic/_types.py @@ -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 @@ -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" diff --git a/src/anthropic/_utils/_proxy.py b/src/anthropic/_utils/_proxy.py index fd85ebd5..aa934a3f 100644 --- a/src/anthropic/_utils/_proxy.py +++ b/src/anthropic/_utils/_proxy.py @@ -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") @@ -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__ diff --git a/src/anthropic/_utils/_transform.py b/src/anthropic/_utils/_transform.py index c007d8b0..d524b329 100644 --- a/src/anthropic/_utils/_transform.py +++ b/src/anthropic/_utils/_transform.py @@ -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, @@ -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}')"