-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplicitly_annotated_option.py
60 lines (44 loc) · 1.6 KB
/
explicitly_annotated_option.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
This example shows an explicitly annotated option type, which allows users to specify
an override or specific annotation via the `type_annotation` argument.
The benefit of this approach is that it lets users specify an annotation explicitly for
cases where the deduced annotation would be incorrect.
"""
from __future__ import annotations
import typing as t
import click
class _SENTINEL:
"""Internal sentinel class."""
class ExplicitlyAnnotatedOption(click.Option):
def __init__(
self,
*args: t.Any,
type_annotation: type = _SENTINEL,
**kwargs: t.Any,
) -> None:
super().__init__(*args, **kwargs)
self._type_annotation = type_annotation
def has_explicit_annotation(self) -> bool:
if self._type_annotation == _SENTINEL:
return False
return True
@property
def type_annotation(self) -> type:
if self._type_annotation == _SENTINEL:
raise ValueError("cannot get annotation from option when it is not set")
return t.cast(type, self._type_annotation)
@click.command()
@click.option(
"--bar",
type=int,
callback=lambda ctx, param, value: str(value) if value is not None else None,
cls=ExplicitlyAnnotatedOption,
# although 'int | None' is expected, the callback returns 'str | None'
# but Optional might be used because some python versions need a runtime type
type_annotation=t.Optional[str],
)
def foo(*, bar: str | None) -> None:
click.echo(f"bar: {bar}")
if __name__ == "__main__":
import click_type_test
click_type_test.check_param_annotations(foo)