Skip to content

Commit

Permalink
fix compatibility with pydantic <2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Jun 13, 2024
1 parent 6f5d880 commit d89f8ba
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion b2/_internal/_cli/obj_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import argparse
import copy
import io
import json
import logging
Expand Down Expand Up @@ -53,10 +54,20 @@ def describe_type(type_) -> str:
_UNDEF = object()


def type_with_config(type_: type[T], config: pydantic.ConfigDict) -> type[T]:
type_ = copy.copy(type_)
if not hasattr(type_, '__config__'):
type_.__pydantic_config__ = config
else:
type_.__config__ = type_.__config__.copy()
type_.__config__.update(config)
return type_


def validated_loads(data: str, expected_type: type[T] | None = None) -> T:
val = _UNDEF
if expected_type is not None and pydantic is not None:
expected_type = pydantic.with_config(pydantic.ConfigDict(extra="allow"))(expected_type)
expected_type = type_with_config(expected_type, pydantic.ConfigDict(extra="allow"))
try:
ta = TypeAdapter(expected_type)
except TypeError:
Expand Down

0 comments on commit d89f8ba

Please sign in to comment.