MCPcopy
hub / github.com/pydantic/pydantic / load_str_bytes

Function load_str_bytes

pydantic/deprecated/parse.py:26–56  ·  view source on GitHub ↗
(
    b: str | bytes,
    *,
    content_type: str | None = None,
    encoding: str = 'utf8',
    proto: Protocol | None = None,
    allow_pickle: bool = False,
    json_loads: Callable[[str], Any] = json.loads,
)

Source from the content-addressed store, hash-verified

24
25@deprecated('`load_str_bytes` is deprecated.', category=None)
26def load_str_bytes(
27 b: str | bytes,
28 *,
29 content_type: str | None = None,
30 encoding: str = 'utf8',
31 proto: Protocol | None = None,
32 allow_pickle: bool = False,
33 json_loads: Callable[[str], Any] = json.loads,
34) -> Any:
35 warnings.warn('`load_str_bytes` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)
36 if proto is None and content_type:
37 if content_type.endswith(('json', 'javascript')):
38 pass
39 elif allow_pickle and content_type.endswith('pickle'):
40 proto = Protocol.pickle
41 else:
42 raise TypeError(f'Unknown content-type: {content_type}')
43
44 proto = proto or Protocol.json
45
46 if proto == Protocol.json:
47 if isinstance(b, bytes):
48 b = b.decode(encoding)
49 return json_loads(b) # type: ignore
50 elif proto == Protocol.pickle:
51 if not allow_pickle:
52 raise RuntimeError('Trying to decode with pickle with allow_pickle=False')
53 bb = b if isinstance(b, bytes) else b.encode() # type: ignore
54 return pickle.loads(bb)
55 else:
56 raise TypeError(f'Unknown protocol: {proto}')
57
58
59@deprecated('`load_file` is deprecated.', category=None)

Callers 1

load_fileFunction · 0.70

Calls 2

decodeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected