Skip to content

Commit

Permalink
chore(internal): updates (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Sep 8, 2023
1 parent bd499a1 commit 5c940e0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ nox = "^2023.4.22"
nox-poetry = "^1.0.3"



[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Expand Down
12 changes: 9 additions & 3 deletions src/modern_treasury/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ def model_copy(model: _ModelT) -> _ModelT:
return model.copy() # type: ignore


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


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


def model_parse(model: type[_ModelT], data: Any) -> _ModelT:
if PYDANTIC_V2:
return model.model_validate(data)
return model.parse_obj(data) # pyright: ignore[reportDeprecated]


# generic models
if TYPE_CHECKING:

Expand Down
3 changes: 2 additions & 1 deletion src/modern_treasury/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
_T = TypeVar("_T")

# Approximates httpx internal ProxiesTypes and RequestFiles types
ProxiesTypes = Union[str, Proxy, Dict[str, Union[None, str, Proxy]]]
ProxiesDict = Dict[str, Union[None, str, Proxy]]
ProxiesTypes = Union[str, Proxy, ProxiesDict]
FileContent = Union[IO[bytes], bytes]
FileTypes = Union[
# file (or bytes)
Expand Down
1 change: 1 addition & 0 deletions src/modern_treasury/_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ._utils import flatten as flatten
from ._utils import is_dict as is_dict
from ._utils import is_list as is_list
from ._utils import is_given as is_given
from ._utils import is_mapping as is_mapping
from ._utils import parse_date as parse_date
from ._utils import coerce_float as coerce_float
Expand Down
6 changes: 5 additions & 1 deletion src/modern_treasury/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing_extensions import Required, Annotated, TypeGuard, get_args, get_origin

from .._types import NotGiven, FileTypes
from .._types import NotGiven, FileTypes, NotGivenOr
from .._compat import is_union as _is_union
from .._compat import parse_date as parse_date
from .._compat import parse_datetime as parse_datetime
Expand Down Expand Up @@ -100,6 +100,10 @@ def _extract_items(
return []


def is_given(obj: NotGivenOr[_T]) -> TypeGuard[_T]:
return not isinstance(obj, NotGiven)


# Type safe methods for narrowing types with TypeVars.
# The default narrowing for isinstance(obj, dict) is dict[unknown, unknown],
# however this cause Pyright to rightfully report errors. As we know we don't
Expand Down

0 comments on commit 5c940e0

Please sign in to comment.