Coverage for src/pydantic_typer/utils.py: 92%

1 from __future__ import annotations

2

3 import inspect

4 import sys

5 from typing import Any, Callable, TypeVar

6

7 KeyType = TypeVar("KeyType")

8

9

10 def deep_update(mapping: dict[KeyType, Any], *updating_mappings: dict[KeyType, Any]) -> dict[KeyType, Any]:

11 # Copied from pydantic because they don't expose it publicly:

12 # https://github.com/pydantic/pydantic/blob/26129479a06960af9d02d3a948e51985fe59ed4b/pydantic/_internal/_utils.py#L103

13 updated_mapping = mapping.copy()

14 for updating_mapping in updating_mappings:

15 for k, v in updating_mapping.items():

16 if k in updated_mapping and isinstance(updated_mapping[k], dict) and isinstance(v, dict):

17 updated_mapping[k] = deep_update(updated_mapping[k], v)

18 else:

19 updated_mapping[k] = v

20 return updated_mapping

21

22

23 def inspect_signature(func: Callable[..., Any]) -> inspect.Signature:

24 # pragma: no cover

25 if sys.version_info >= (3, 10): 25 ↛ 28line 25 didn't jump to line 28 because the condition on line 25 was always true

26 signature = inspect.signature(func, eval_str=True)

27 else:

28 signature = inspect.signature(func)

29 return signature