Skip to content

Commit 629dd53

Browse files
committed
add tests for lazy imports
1 parent a2c9395 commit 629dd53

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lib/test/test_argparse.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,50 @@ def test_skip_invalid_stdout(self):
8080
self.assertRegex(mocked_stderr.getvalue(), r'usage:')
8181

8282

83+
class TestLazyImports(unittest.TestCase):
84+
LAZY_IMPORTS = {
85+
"_colorize",
86+
"copy",
87+
"difflib",
88+
"shutil",
89+
"textwrap",
90+
"warnings",
91+
}
92+
def test_module_import(self):
93+
import_helper.ensure_lazy_imports(
94+
"argparse",
95+
self.LAZY_IMPORTS,
96+
)
97+
98+
def test_create_parser(self):
99+
# Test imports are still unused after
100+
# creating a parser
101+
create_parser = "argparse.ArgumentParser()"
102+
imported_modules = {"shutil"}
103+
104+
import_helper.ensure_lazy_imports(
105+
"argparse",
106+
self.LAZY_IMPORTS - imported_modules,
107+
additional_code=create_parser,
108+
)
109+
110+
def test_add_subparser(self):
111+
# This fails as it currently imports colorize
112+
add_subparser = textwrap.dedent(
113+
f"""
114+
parser = argparse.ArgumentParser()
115+
parser.add_subparsers(dest='command', required=False)
116+
"""
117+
)
118+
imported_modules = {"shutil"}
119+
120+
import_helper.ensure_lazy_imports(
121+
"argparse",
122+
self.LAZY_IMPORTS - imported_modules,
123+
additional_code=add_subparser,
124+
)
125+
126+
83127
class TestArgumentParserPickleable(unittest.TestCase):
84128

85129
@force_not_colorized

0 commit comments

Comments
 (0)