Skip to content

Commit 01662d9

Browse files
chore(internal): updates (#93)
1 parent 4770119 commit 01662d9

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ nox = "^2023.4.22"
3434
nox-poetry = "^1.0.3"
3535

3636

37+
3738
[build-system]
3839
requires = ["poetry-core>=1.0.0"]
3940
build-backend = "poetry.core.masonry.api"

src/finch/_compat.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ def model_copy(model: _ModelT) -> _ModelT:
120120
return model.copy() # type: ignore
121121

122122

123-
def model_json(model: pydantic.BaseModel) -> str:
123+
def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str:
124124
if PYDANTIC_V2:
125-
return model.model_dump_json()
126-
return model.json() # type: ignore
125+
return model.model_dump_json(indent=indent)
126+
return model.json(indent=indent) # type: ignore
127127

128128

129129
def model_dump(model: pydantic.BaseModel) -> dict[str, Any]:
@@ -132,6 +132,12 @@ def model_dump(model: pydantic.BaseModel) -> dict[str, Any]:
132132
return cast("dict[str, Any]", model.dict()) # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
133133

134134

135+
def model_parse(model: type[_ModelT], data: Any) -> _ModelT:
136+
if PYDANTIC_V2:
137+
return model.model_validate(data)
138+
return model.parse_obj(data) # pyright: ignore[reportDeprecated]
139+
140+
135141
# generic models
136142
if TYPE_CHECKING:
137143

src/finch/_types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
_T = TypeVar("_T")
3232

3333
# Approximates httpx internal ProxiesTypes and RequestFiles types
34-
ProxiesTypes = Union[str, Proxy, Dict[str, Union[None, str, Proxy]]]
34+
ProxiesDict = Dict[str, Union[None, str, Proxy]]
35+
ProxiesTypes = Union[str, Proxy, ProxiesDict]
3536
FileContent = Union[IO[bytes], bytes]
3637
FileTypes = Union[
3738
# file (or bytes)

src/finch/_utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ._utils import flatten as flatten
22
from ._utils import is_dict as is_dict
33
from ._utils import is_list as is_list
4+
from ._utils import is_given as is_given
45
from ._utils import is_mapping as is_mapping
56
from ._utils import parse_date as parse_date
67
from ._utils import coerce_float as coerce_float

src/finch/_utils/_utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing_extensions import Required, Annotated, TypeGuard, get_args, get_origin
99

10-
from .._types import NotGiven, FileTypes
10+
from .._types import NotGiven, FileTypes, NotGivenOr
1111
from .._compat import is_union as _is_union
1212
from .._compat import parse_date as parse_date
1313
from .._compat import parse_datetime as parse_datetime
@@ -100,6 +100,10 @@ def _extract_items(
100100
return []
101101

102102

103+
def is_given(obj: NotGivenOr[_T]) -> TypeGuard[_T]:
104+
return not isinstance(obj, NotGiven)
105+
106+
103107
# Type safe methods for narrowing types with TypeVars.
104108
# The default narrowing for isinstance(obj, dict) is dict[unknown, unknown],
105109
# however this cause Pyright to rightfully report errors. As we know we don't

0 commit comments

Comments
 (0)