OXIESEC PANEL
- Current Dir:
/
/
opt
/
alt
/
python311
/
lib
/
python3.11
/
site-packages
/
typer
Server IP: 2a02:4780:11:1594:0:ef5:22d7:a
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
01/09/2025 02:18:04 AM
rwxr-xr-x
📄
__init__.py
1.57 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
__main__.py
30 bytes
05/14/2024 03:18:15 PM
rw-r--r--
📁
__pycache__
-
05/14/2024 03:18:15 PM
rwxr-xr-x
📄
_completion_classes.py
6.53 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
_completion_shared.py
8.3 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
_typing.py
19.26 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
cli.py
9.18 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
colors.py
430 bytes
05/14/2024 03:18:15 PM
rw-r--r--
📄
completion.py
4.65 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
core.py
23.71 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
main.py
38.68 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
models.py
15.54 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
params.py
13.46 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
py.typed
0 bytes
05/14/2024 03:18:15 PM
rw-r--r--
📄
rich_utils.py
23.64 KB
05/14/2024 03:18:15 PM
rw-r--r--
📄
testing.py
874 bytes
05/14/2024 03:18:15 PM
rw-r--r--
📄
utils.py
7.24 KB
05/14/2024 03:18:15 PM
rw-r--r--
Editing: utils.py
Close
import inspect import sys from copy import copy from typing import Any, Callable, Dict, List, Tuple, Type, cast, get_type_hints from typing_extensions import Annotated from ._typing import get_args, get_origin from .models import ArgumentInfo, OptionInfo, ParameterInfo, ParamMeta def _param_type_to_user_string(param_type: Type[ParameterInfo]) -> str: # Render a `ParameterInfo` subclass for use in error messages. # User code doesn't call `*Info` directly, so errors should present the classes how # they were (probably) defined in the user code. if param_type is OptionInfo: return "`Option`" elif param_type is ArgumentInfo: return "`Argument`" # This line shouldn't be reachable during normal use. return f"`{param_type.__name__}`" # pragma: no cover class AnnotatedParamWithDefaultValueError(Exception): argument_name: str param_type: Type[ParameterInfo] def __init__(self, argument_name: str, param_type: Type[ParameterInfo]): self.argument_name = argument_name self.param_type = param_type def __str__(self) -> str: param_type_str = _param_type_to_user_string(self.param_type) return ( f"{param_type_str} default value cannot be set in `Annotated`" f" for {self.argument_name!r}. Set the default value with `=` instead." ) class MixedAnnotatedAndDefaultStyleError(Exception): argument_name: str annotated_param_type: Type[ParameterInfo] default_param_type: Type[ParameterInfo] def __init__( self, argument_name: str, annotated_param_type: Type[ParameterInfo], default_param_type: Type[ParameterInfo], ): self.argument_name = argument_name self.annotated_param_type = annotated_param_type self.default_param_type = default_param_type def __str__(self) -> str: annotated_param_type_str = _param_type_to_user_string(self.annotated_param_type) default_param_type_str = _param_type_to_user_string(self.default_param_type) msg = f"Cannot specify {annotated_param_type_str} in `Annotated` and" if self.annotated_param_type is self.default_param_type: msg += " default value" else: msg += f" {default_param_type_str} as a default value" msg += f" together for {self.argument_name!r}" return msg class MultipleTyperAnnotationsError(Exception): argument_name: str def __init__(self, argument_name: str): self.argument_name = argument_name def __str__(self) -> str: return ( "Cannot specify multiple `Annotated` Typer arguments" f" for {self.argument_name!r}" ) class DefaultFactoryAndDefaultValueError(Exception): argument_name: str param_type: Type[ParameterInfo] def __init__(self, argument_name: str, param_type: Type[ParameterInfo]): self.argument_name = argument_name self.param_type = param_type def __str__(self) -> str: param_type_str = _param_type_to_user_string(self.param_type) return ( "Cannot specify `default_factory` and a default value together" f" for {param_type_str}" ) def _split_annotation_from_typer_annotations( base_annotation: Type[Any], ) -> Tuple[Type[Any], List[ParameterInfo]]: if get_origin(base_annotation) is not Annotated: # type: ignore return base_annotation, [] base_annotation, *maybe_typer_annotations = get_args(base_annotation) return base_annotation, [ annotation for annotation in maybe_typer_annotations if isinstance(annotation, ParameterInfo) ] def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]: if sys.version_info >= (3, 10): signature = inspect.signature(func, eval_str=True) else: signature = inspect.signature(func) type_hints = get_type_hints(func) params = {} for param in signature.parameters.values(): annotation, typer_annotations = _split_annotation_from_typer_annotations( param.annotation, ) if len(typer_annotations) > 1: raise MultipleTyperAnnotationsError(param.name) default = param.default if typer_annotations: # It's something like `my_param: Annotated[str, Argument()]` [parameter_info] = typer_annotations # Forbid `my_param: Annotated[str, Argument()] = Argument("...")` if isinstance(param.default, ParameterInfo): raise MixedAnnotatedAndDefaultStyleError( argument_name=param.name, annotated_param_type=type(parameter_info), default_param_type=type(param.default), ) parameter_info = copy(parameter_info) # When used as a default, `Option` takes a default value and option names # as positional arguments: # `Option(some_value, "--some-argument", "-s")` # When used in `Annotated` (ie, what this is handling), `Option` just takes # option names as positional arguments: # `Option("--some-argument", "-s")` # In this case, the `default` attribute of `parameter_info` is actually # meant to be the first item of `param_decls`. if ( isinstance(parameter_info, OptionInfo) and parameter_info.default is not ... ): parameter_info.param_decls = ( cast(str, parameter_info.default), *(parameter_info.param_decls or ()), ) parameter_info.default = ... # Forbid `my_param: Annotated[str, Argument('some-default')]` if parameter_info.default is not ...: raise AnnotatedParamWithDefaultValueError( param_type=type(parameter_info), argument_name=param.name, ) if param.default is not param.empty: # Put the parameter's default (set by `=`) into `parameter_info`, where # typer can find it. parameter_info.default = param.default default = parameter_info elif param.name in type_hints: # Resolve forward references. annotation = type_hints[param.name] if isinstance(default, ParameterInfo): parameter_info = copy(default) # Click supports `default` as either # - an actual value; or # - a factory function (returning a default value.) # The two are not interchangeable for static typing, so typer allows # specifying `default_factory`. Move the `default_factory` into `default` # so click can find it. if parameter_info.default is ... and parameter_info.default_factory: parameter_info.default = parameter_info.default_factory elif parameter_info.default_factory: raise DefaultFactoryAndDefaultValueError( argument_name=param.name, param_type=type(parameter_info) ) default = parameter_info params[param.name] = ParamMeta( name=param.name, default=default, annotation=annotation ) return params