From f91791dbf94a7066628e0b43b0e7a751c46b8de5 Mon Sep 17 00:00:00 2001 From: Henrique Joaquim Date: Wed, 22 May 2024 12:46:19 +0100 Subject: [PATCH] [BugFix] Revamp the usage of list syntax on routines (#6450) * using list syntax on routines * black * ruff * simplify * black --- .../argparse_class_processor.py | 1 - .../argparse_translator.py | 3 --- cli/openbb_cli/controllers/base_controller.py | 21 +++++++++++++----- .../controllers/base_platform_controller.py | 1 - cli/openbb_cli/controllers/cli_controller.py | 22 +++++++++++-------- .../test_controllers_settings_controller.py | 1 - ruff.toml | 2 +- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/cli/openbb_cli/argparse_translator/argparse_class_processor.py b/cli/openbb_cli/argparse_translator/argparse_class_processor.py index 6fff39aa18e3..d7ddcdafcd23 100644 --- a/cli/openbb_cli/argparse_translator/argparse_class_processor.py +++ b/cli/openbb_cli/argparse_translator/argparse_class_processor.py @@ -92,7 +92,6 @@ def _process_class( if name.startswith("__") or name.startswith("_"): continue if inspect.ismethod(member): - class_name = cls._get_class_name(target) methods[f"{class_name}_{name}"] = ArgparseTranslator( func=member, diff --git a/cli/openbb_cli/argparse_translator/argparse_translator.py b/cli/openbb_cli/argparse_translator/argparse_translator.py index 7931b22304f7..605fbe6b41f0 100644 --- a/cli/openbb_cli/argparse_translator/argparse_translator.py +++ b/cli/openbb_cli/argparse_translator/argparse_translator.py @@ -33,7 +33,6 @@ class ArgparseActionType(Enum): class CustomArgument(BaseModel): - name: str type: Optional[Any] dest: str @@ -62,7 +61,6 @@ def remove_props_on_store_true(cls, values: "CustomArgument"): # override def model_dump(self, **kwargs): - res = super().model_dump(**kwargs) # Check if choices is present and if it's an empty tuple remove it @@ -152,7 +150,6 @@ def _get_choices(self, type_: str, custom_choices: Any) -> Tuple: def build_custom_groups(self): """Build the custom groups from the reference.""" for route, v in self.reference.items(): - for provider, args in v["parameters"].items(): if provider == "standard": continue diff --git a/cli/openbb_cli/controllers/base_controller.py b/cli/openbb_cli/controllers/base_controller.py index b1952da5fcfb..206e8a735bd1 100644 --- a/cli/openbb_cli/controllers/base_controller.py +++ b/cli/openbb_cli/controllers/base_controller.py @@ -799,11 +799,22 @@ def parse_known_args_and_warn( return None try: - # If the user uses a comma separated list of arguments, split them - for index, arg in enumerate(other_args): - if "," in arg: - parts = arg.split(",") - other_args[index : index + 1] = parts + # Determine the index of the routine arguments + routine_args_index = next( + ( + i + 1 + for i, arg in enumerate(other_args) + if arg in ("-i", "--input") + and "routine_args" in [action.dest for action in parser._actions] + ), + -1, + ) + # Split comma-separated arguments, except for the argument at routine_args_index + other_args = [ + part + for index, arg in enumerate(other_args) + for part in (arg.split(",") if index != routine_args_index else [arg]) + ] (ns_parser, l_unknown_args) = parser.parse_known_args(other_args) diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index df67c708e148..ed8f0087516a 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -158,7 +158,6 @@ def method(self, other_args: List[str], translator=translator): title = f"{self.PATH}{translator.func.__name__}" if obbject: - if isinstance(obbject, OBBject): if session.max_obbjects_exceeded() and obbject.results: session.obbject_registry.remove() diff --git a/cli/openbb_cli/controllers/cli_controller.py b/cli/openbb_cli/controllers/cli_controller.py index 49eaf92da333..ccd54ae91141 100644 --- a/cli/openbb_cli/controllers/cli_controller.py +++ b/cli/openbb_cli/controllers/cli_controller.py @@ -355,7 +355,7 @@ def call_exe(self, other_args: List[str]): "--input", help="Select multiple inputs to be replaced in the routine and separated by commas. E.g. GME,AMC,BTC-USD", dest="routine_args", - type=lambda s: [str(item) for item in s.split(",")], + type=str, ) parser.add_argument( "-e", @@ -431,17 +431,22 @@ def call_exe(self, other_args: List[str]): with open(routine_path) as fp: raw_lines = list(fp) + script_inputs = [] # Capture ARGV either as list if args separated by commas or as single value - if ns_parser.routine_args: - script_inputs = ( - ns_parser.routine_args - if "," not in ns_parser.routine_args - else ns_parser.routine_args.split(",") + if routine_args := ns_parser.routine_args: + pattern = r"\[(.*?)\]" + matches = re.findall(pattern, routine_args) + + for match in matches: + routine_args = routine_args.replace(f"[{match}]", "") + script_inputs.append(match) + + script_inputs.extend( + [val for val in routine_args.split(",") if val] ) err, parsed_script = parse_openbb_script( - raw_lines=raw_lines, - script_inputs=script_inputs if ns_parser.routine_args else None, + raw_lines=raw_lines, script_inputs=script_inputs ) # If there err output is not an empty string then it means there was an @@ -546,7 +551,6 @@ def run_cli(jobs_cmds: Optional[List[str]] = None, test_mode=False): t_controller.print_help() while ret_code: - # There is a command in the queue if t_controller.queue and len(t_controller.queue) > 0: # If the command is quitting the menu we want to return in here diff --git a/cli/tests/test_controllers_settings_controller.py b/cli/tests/test_controllers_settings_controller.py index 439281392057..7540b3bea4f6 100644 --- a/cli/tests/test_controllers_settings_controller.py +++ b/cli/tests/test_controllers_settings_controller.py @@ -11,7 +11,6 @@ @pytest.fixture def mock_session(): with patch("openbb_cli.controllers.settings_controller.session") as mock: - mock.settings.USE_INTERACTIVE_DF = False mock.settings.ALLOWED_NUMBER_OF_ROWS = 20 mock.settings.TIMEZONE = "UTC" diff --git a/ruff.toml b/ruff.toml index 385bdf9232c0..cae9b8373d12 100644 --- a/ruff.toml +++ b/ruff.toml @@ -41,7 +41,7 @@ force-wrap-aliases = true [lint.pylint] max-args = 8 -max-branches = 25 +max-branches = 26 max-returns = 9 max-statements = 30