Coverage for src/incant/_compat.py: 100%

1 import sys

2

3 from functools import partial

4 from inspect import Parameter

5 from inspect import signature as sig

6 from typing import Any, Optional

7

8 from attr import frozen

9

10

11 NO_OVERRIDE = object()

12

13

14@frozen

15 class Override:

16 name: Optional[str] = None

17 annotation: Any = NO_OVERRIDE

18

19

20 if sys.version_info >= (3, 9):

21 from typing import _AnnotatedAlias # type: ignore

22

23 else:

24 from typing_extensions import _AnnotatedAlias

25

26 if sys.version_info >= (3, 10):

27 signature = partial(sig, eval_str=True)

28

29 else:

30 signature = sig

31

32

33 def get_annotated_override(p: Parameter) -> Parameter:

34 if p.annotation.__class__ is _AnnotatedAlias:

35 for arg in p.annotation.__metadata__:

36 if isinstance(arg, Override):

37 name = arg.name if arg.name is not None else p.name

38 an = (

39 arg.annotation

40 if arg.annotation is not NO_OVERRIDE

41 else p.annotation

42 )

43 return Parameter(name, p.kind, default=p.default, annotation=an)

44 return p