Skip to content

Commit 6172918

Browse files
committed
fix(commands): need to look in globals
of course! locals() inside the context of the function scope only contains the function params and local variables!
1 parent c2b8f04 commit 6172918

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from argparse import Namespace
22

3-
from compiler_admin.commands.time import convert # noqa: F401
3+
from compiler_admin.commands.time.convert import convert # noqa: F401
44

55

66
def time(args: Namespace, *extra):
7-
# try to call the subcommand function directly from local symbols
8-
# if the subcommand function was imported above, it should exist in locals()
9-
if args.subcommand in locals():
10-
locals()[args.subcommand](args, *extra)
7+
# try to call the subcommand function directly from global (module) symbols
8+
# if the subcommand function was imported above, it should exist in globals()
9+
global_env = globals()
10+
11+
if args.subcommand in global_env:
12+
cmd_func = global_env[args.subcommand]
13+
cmd_func(args, *extra)
1114
else:
1215
raise NotImplementedError(f"Unknown time subcommand: {args.subcommand}")

compiler_admin/commands/user/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111

1212
def user(args: Namespace, *extra):
13-
# try to call the subcommand function directly from local symbols
14-
# if the subcommand function was imported above, it should exist in locals()
15-
if args.subcommand in locals():
16-
locals()[args.subcommand](args, *extra)
13+
# try to call the subcommand function directly from global (module) symbols
14+
# if the subcommand function was imported above, it should exist in globals()
15+
global_env = globals()
16+
17+
if args.subcommand in global_env:
18+
cmd_func = global_env[args.subcommand]
19+
cmd_func(args, *extra)
1720
else:
1821
raise NotImplementedError(f"Unknown user subcommand: {args.subcommand}")

tests/commands/time/test__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
def test_time_subcommand_exists(mocker):
99
args = Namespace(subcommand="subcmd")
10-
subcmd = mocker.patch("compiler_admin.commands.time.locals", return_value={"subcmd": mocker.Mock()})
10+
subcmd = mocker.patch("compiler_admin.commands.time.globals", return_value={"subcmd": mocker.Mock()})
1111

1212
time(args, 1, 2, 3)
1313

14-
subcmd.assert_called()
14+
subcmd.assert_called_once()
1515
subcmd.return_value["subcmd"].assert_called_once_with(args, 1, 2, 3)
1616

1717

1818
def test_time_subcommand_doesnt_exists(mocker):
1919
args = Namespace(subcommand="subcmd")
20-
subcmd = mocker.patch("compiler_admin.commands.time.locals", return_value={})
20+
subcmd = mocker.patch("compiler_admin.commands.time.globals", return_value={})
2121

2222
with pytest.raises(NotImplementedError, match="Unknown time subcommand: subcmd"):
2323
time(args, 1, 2, 3)

tests/commands/user/test__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def test_user_subcommand_exists(mocker):
99
args = Namespace(subcommand="subcmd")
10-
subcmd = mocker.patch("compiler_admin.commands.user.locals", return_value={"subcmd": mocker.Mock()})
10+
subcmd = mocker.patch("compiler_admin.commands.user.globals", return_value={"subcmd": mocker.Mock()})
1111

1212
user(args, 1, 2, 3)
1313

@@ -17,7 +17,7 @@ def test_user_subcommand_exists(mocker):
1717

1818
def test_time_subcommand_doesnt_exists(mocker):
1919
args = Namespace(subcommand="subcmd")
20-
subcmd = mocker.patch("compiler_admin.commands.user.locals", return_value={})
20+
subcmd = mocker.patch("compiler_admin.commands.user.globals", return_value={})
2121

2222
with pytest.raises(NotImplementedError, match="Unknown user subcommand: subcmd"):
2323
user(args, 1, 2, 3)

0 commit comments

Comments
 (0)