Skip to content

Commit

Permalink
Allow sys.argv and args in LightningCLI (#16808)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0ec44f1)
  • Loading branch information
Erotemic authored and Borda committed Mar 30, 2023
1 parent 60a04f8 commit b9a34d0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/pytorch_lightning/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,9 @@ def link_optimizers_and_lr_schedulers(parser: LightningArgumentParser) -> None:
def parse_arguments(self, parser: LightningArgumentParser, args: ArgsType) -> None:
"""Parses command line arguments and stores it in ``self.config``."""
if args is not None and len(sys.argv) > 1:
raise ValueError(
rank_zero_warn(
"LightningCLI's args parameter is intended to run from within Python like if it were from the command "
"line. To prevent mistakes it is not allowed to provide both args and command line arguments, got: "
"line. To prevent mistakes it is not recommended to provide both args and command line arguments, got: "
f"sys.argv[1:]={sys.argv[1:]}, args={args}."
)
if isinstance(args, (dict, Namespace)):
Expand Down
6 changes: 2 additions & 4 deletions tests/tests_pytorch/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,6 @@ def test_lightning_cli_with_args_given(args):
assert cli.model.foo == 456


def test_lightning_cli_args_and_sys_argv_exception():
with mock.patch("sys.argv", ["", "--model.foo=456"]), pytest.raises(
ValueError, match="LightningCLI's args parameter "
):
def test_lightning_cli_args_and_sys_argv_warning():
with mock.patch("sys.argv", ["", "--model.foo=456"]), pytest.warns(Warning, match="LightningCLI's args parameter "):
LightningCLI(TestModel, run=False, args=["--model.foo=789"])

0 comments on commit b9a34d0

Please sign in to comment.