Skip to content

importing ComPASS.options hijacks argparse CLI arguments #1

Description

@ma-ath

Hello! Sorry for opening an issue here. If there is a preferred way to submit issues, I’m happy to follow that.
I’m working on a project that uses ComPASS as a library, and I ran into a problem when trying to use my own argparse CLI arguments.

The issue seems to be on file options.py, (which seems to be unaltered on the current main branch) which calls compass_config.load_cli at import time. This "hijacks" the CLI arguments before they reach argparse, making it impossible to parse custom arguments in scripts that import ComPASS modules.

Minimum reproduction

# issue.py
import argparse

from ComPASS.linalg.factory import linear_solver  # noqa

parser = argparse.ArgumentParser()
parser.add_argument("--flag")
args = parser.parse_args()

print(f"{args.flag=}")
$ python issue.py
args.flag=None

$ python issue.py --flag="Hello"
Traceback (most recent call last):
  File "/workspaces/orison/src/orison_models/compass/issue.py", line 3, in <module>
    from ComPASS.linalg.factory import linear_solver  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/ComPASS/linalg/factory.py", line 9, in <module>
    from .. import options
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/ComPASS/options.py", line 62, in <module>
    compass_config.load_cli(allow_extra_args=True)
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/inept/config.py", line 188, in load_cli
    self.update(self.parse_cli(args, **extra))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/inept/config.py", line 183, in parse_cli
    ctx = self.cli.make_context(self.cli.name, args, **extra)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/core.py", line 1405, in parse_args
    opts, args, param_order = parser.parse_args(args=args)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/parser.py", line 337, in parse_args
    self._process_args_for_options(state)
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/parser.py", line 364, in _process_args_for_options
    self._process_opts(arg, state)
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/parser.py", line 514, in _process_opts
    self._match_long_opt(norm_long_opt, explicit_value, state)
  File "/opt/conda/envs/orison-2.0/lib/python3.12/site-packages/click/parser.py", line 398, in _match_long_opt
    raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx)
click.exceptions.NoSuchOption: No such option: --flag
WARNING! There are options you set that were not used!
WARNING! could be spelling mistake, etc!
There is one unused database option. It is:
Option left: name:--flag=Hello (no value) source: command line

Suggested notes:

Ideally importing options.py should not run load_cli. As a workaround to allow compatibility with already existing scripts, compass_config.load_cli could be deactivated with an environment variable, i.e

if not os.environ.get("COMPASS_DISABLE_CLI_PARSING"):
    compass_config.load_cli(allow_extra_args=True)

Setting COMPASS_DISABLE_CLI_PARSING before importing ComPASS would then prevent that behaviour.

System Settings

OS: Ubuntu 24.04 running on Docker container
Architecture: amd64
Python version: 3.12.13
ComPASS version: git+https://github.com/BRGM/ComPASS.git@v4.5.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions