From d89f8ba82d64bc3e4d5c8a60599585bcd02372a9 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Thu, 13 Jun 2024 22:23:14 +0200 Subject: [PATCH] fix compatibility with pydantic <2.7 --- b2/_internal/_cli/obj_loads.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/b2/_internal/_cli/obj_loads.py b/b2/_internal/_cli/obj_loads.py index de4a71ae..d7dd3b67 100644 --- a/b2/_internal/_cli/obj_loads.py +++ b/b2/_internal/_cli/obj_loads.py @@ -10,6 +10,7 @@ from __future__ import annotations import argparse +import copy import io import json import logging @@ -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: