Skip to content

Commit edeabd6

Browse files
committed
tests: Fix test_module for Python 3.14
Python 3.14 changed argparse to lazily import _colorize during ArgumentParser.__init__, triggering a chain of imports (_colorize -> dataclasses -> inspect -> tokenize) that ends with `from builtins import open`. The class-level mock.patch.dict on sys.modules with clear=True removed builtins from sys.modules, causing this import to fail with ImportError. Narrow the mock scope to wrap only the take_action call rather than the entire test class, so get_parser/argparse initialization runs with an unpatched sys.modules while take_action still iterates the controlled set of fake modules. Assisted-By: claude-code Change-Id: I0e1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a Signed-off-by: Jay Faulkner <jay@jvf.cc>
1 parent 437914a commit edeabd6

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

openstackclient/tests/unit/common/test_module.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ def test_command_list_with_group(self):
128128
self.assertEqual(datalist, tuple(data))
129129

130130

131-
@mock.patch.dict(
132-
'openstackclient.common.module.sys.modules',
133-
values=MODULES,
134-
clear=True,
135-
)
136131
class TestModuleList(utils.TestCommand):
137132
def setUp(self):
138133
super().setUp()
@@ -150,7 +145,12 @@ def test_module_list_no_options(self):
150145
# In base command class Lister in cliff, abstract method take_action()
151146
# returns a tuple containing the column names and an iterable
152147
# containing the data to be listed.
153-
columns, data = self.cmd.take_action(parsed_args)
148+
with mock.patch.dict(
149+
'openstackclient.common.module.sys.modules',
150+
values=MODULES,
151+
clear=True,
152+
):
153+
columns, data = self.cmd.take_action(parsed_args)
154154

155155
# Output xxxclient and openstacksdk, but not regular module, like: zlib
156156
self.assertIn(module_name_1, columns)
@@ -177,7 +177,12 @@ def test_module_list_all(self):
177177
# In base command class Lister in cliff, abstract method take_action()
178178
# returns a tuple containing the column names and an iterable
179179
# containing the data to be listed.
180-
columns, data = self.cmd.take_action(parsed_args)
180+
with mock.patch.dict(
181+
'openstackclient.common.module.sys.modules',
182+
values=MODULES,
183+
clear=True,
184+
):
185+
columns, data = self.cmd.take_action(parsed_args)
181186

182187
# Output xxxclient, openstacksdk and regular module, like: zlib
183188
self.assertIn(module_name_1, columns)

0 commit comments

Comments
 (0)