Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7cc7d2

Browse files
gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052)
1 parent da310d2 commit f7cc7d2

18 files changed

+51
-54
lines changed

Lib/test/test_import/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test_import_name_binding(self):
539539
import test as x
540540
import test.support
541541
self.assertIs(x, test, x.__name__)
542-
self.assertTrue(hasattr(test.support, "__file__"))
542+
self.assertHasAttr(test.support, "__file__")
543543

544544
# import x.y.z as w binds z as w
545545
import test.support as y
@@ -610,7 +610,7 @@ def test_file_to_source(self):
610610
sys.path.insert(0, os.curdir)
611611
try:
612612
mod = __import__(TESTFN)
613-
self.assertTrue(mod.__file__.endswith('.py'))
613+
self.assertEndsWith(mod.__file__, '.py')
614614
os.remove(source)
615615
del sys.modules[TESTFN]
616616
make_legacy_pyc(source)
@@ -1443,7 +1443,7 @@ def test_UNC_path(self):
14431443
self.fail("could not import 'test_unc_path' from %r: %r"
14441444
% (unc, e))
14451445
self.assertEqual(mod.testdata, 'test_unc_path')
1446-
self.assertTrue(mod.__file__.startswith(unc), mod.__file__)
1446+
self.assertStartsWith(mod.__file__, unc)
14471447
unload("test_unc_path")
14481448

14491449

@@ -1456,7 +1456,7 @@ def tearDown(self):
14561456
def test_relimport_star(self):
14571457
# This will import * from .test_import.
14581458
from .. import relimport
1459-
self.assertTrue(hasattr(relimport, "RelativeImportTests"))
1459+
self.assertHasAttr(relimport, "RelativeImportTests")
14601460

14611461
def test_issue3221(self):
14621462
# Note for mergers: the 'absolute' tests from the 2.x branch
@@ -1786,15 +1786,15 @@ def test_frozen_importlib_is_bootstrap(self):
17861786
self.assertIs(mod, _bootstrap)
17871787
self.assertEqual(mod.__name__, 'importlib._bootstrap')
17881788
self.assertEqual(mod.__package__, 'importlib')
1789-
self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
1789+
self.assertEndsWith(mod.__file__, '_bootstrap.py')
17901790

17911791
def test_frozen_importlib_external_is_bootstrap_external(self):
17921792
from importlib import _bootstrap_external
17931793
mod = sys.modules['_frozen_importlib_external']
17941794
self.assertIs(mod, _bootstrap_external)
17951795
self.assertEqual(mod.__name__, 'importlib._bootstrap_external')
17961796
self.assertEqual(mod.__package__, 'importlib')
1797-
self.assertTrue(mod.__file__.endswith('_bootstrap_external.py'), mod.__file__)
1797+
self.assertEndsWith(mod.__file__, '_bootstrap_external.py')
17981798

17991799
def test_there_can_be_only_one(self):
18001800
# Issue #15386 revealed a tricky loophole in the bootstrapping
@@ -2800,7 +2800,7 @@ def check_common(self, loaded):
28002800
self.assertEqual(mod.__file__, self.FILE)
28012801
self.assertEqual(mod.__spec__.origin, self.ORIGIN)
28022802
if not isolated:
2803-
self.assertTrue(issubclass(mod.error, Exception))
2803+
self.assertIsSubclass(mod.error, Exception)
28042804
self.assertEqual(mod.int_const, 1969)
28052805
self.assertEqual(mod.str_const, 'something different')
28062806
self.assertIsInstance(mod._module_initialized, float)

Lib/test/test_importlib/extension/test_path_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def hook(self, entry):
2121
def test_success(self):
2222
# Path hook should handle a directory where a known extension module
2323
# exists.
24-
self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_spec'))
24+
self.assertHasAttr(self.hook(util.EXTENSIONS.path), 'find_spec')
2525

2626

2727
(Frozen_PathHooksTests,

Lib/test/test_importlib/frozen/test_loader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def exec_module(self, name, origname=None):
6161
module.main()
6262

6363
self.assertTrue(module.initialized)
64-
self.assertTrue(hasattr(module, '__spec__'))
64+
self.assertHasAttr(module, '__spec__')
6565
self.assertEqual(module.__spec__.origin, 'frozen')
6666
return module, stdout.getvalue()
6767

@@ -72,7 +72,7 @@ def test_module(self):
7272
for attr, value in check.items():
7373
self.assertEqual(getattr(module, attr), value)
7474
self.assertEqual(output, 'Hello world!\n')
75-
self.assertTrue(hasattr(module, '__spec__'))
75+
self.assertHasAttr(module, '__spec__')
7676
self.assertEqual(module.__spec__.loader_state.origname, name)
7777

7878
def test_package(self):
@@ -136,7 +136,7 @@ def test_get_code(self):
136136
exec(code, mod.__dict__)
137137
with captured_stdout() as stdout:
138138
mod.main()
139-
self.assertTrue(hasattr(mod, 'initialized'))
139+
self.assertHasAttr(mod, 'initialized')
140140
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
141141

142142
def test_get_source(self):

Lib/test/test_importlib/import_/test_caching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_using_cache_for_assigning_to_attribute(self):
7878
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
7979
with util.import_state(meta_path=[importer]):
8080
module = self.__import__('pkg.module')
81-
self.assertTrue(hasattr(module, 'module'))
81+
self.assertHasAttr(module, 'module')
8282
self.assertEqual(id(module.module),
8383
id(sys.modules['pkg.module']))
8484

@@ -88,7 +88,7 @@ def test_using_cache_for_fromlist(self):
8888
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
8989
with util.import_state(meta_path=[importer]):
9090
module = self.__import__('pkg', fromlist=['module'])
91-
self.assertTrue(hasattr(module, 'module'))
91+
self.assertHasAttr(module, 'module')
9292
self.assertEqual(id(module.module),
9393
id(sys.modules['pkg.module']))
9494

Lib/test/test_importlib/import_/test_fromlist.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ def test_nonexistent_object(self):
6363
with util.import_state(meta_path=[importer]):
6464
module = self.__import__('module', fromlist=['non_existent'])
6565
self.assertEqual(module.__name__, 'module')
66-
self.assertFalse(hasattr(module, 'non_existent'))
66+
self.assertNotHasAttr(module, 'non_existent')
6767

6868
def test_module_from_package(self):
6969
# [module]
7070
with util.mock_spec('pkg.__init__', 'pkg.module') as importer:
7171
with util.import_state(meta_path=[importer]):
7272
module = self.__import__('pkg', fromlist=['module'])
7373
self.assertEqual(module.__name__, 'pkg')
74-
self.assertTrue(hasattr(module, 'module'))
74+
self.assertHasAttr(module, 'module')
7575
self.assertEqual(module.module.__name__, 'pkg.module')
7676

7777
def test_nonexistent_from_package(self):
7878
with util.mock_spec('pkg.__init__') as importer:
7979
with util.import_state(meta_path=[importer]):
8080
module = self.__import__('pkg', fromlist=['non_existent'])
8181
self.assertEqual(module.__name__, 'pkg')
82-
self.assertFalse(hasattr(module, 'non_existent'))
82+
self.assertNotHasAttr(module, 'non_existent')
8383

8484
def test_module_from_package_triggers_ModuleNotFoundError(self):
8585
# If a submodule causes an ModuleNotFoundError because it tries
@@ -107,7 +107,7 @@ def basic_star_test(self, fromlist=['*']):
107107
mock['pkg'].__all__ = ['module']
108108
module = self.__import__('pkg', fromlist=fromlist)
109109
self.assertEqual(module.__name__, 'pkg')
110-
self.assertTrue(hasattr(module, 'module'))
110+
self.assertHasAttr(module, 'module')
111111
self.assertEqual(module.module.__name__, 'pkg.module')
112112

113113
def test_using_star(self):
@@ -125,8 +125,8 @@ def test_star_with_others(self):
125125
mock['pkg'].__all__ = ['module1']
126126
module = self.__import__('pkg', fromlist=['module2', '*'])
127127
self.assertEqual(module.__name__, 'pkg')
128-
self.assertTrue(hasattr(module, 'module1'))
129-
self.assertTrue(hasattr(module, 'module2'))
128+
self.assertHasAttr(module, 'module1')
129+
self.assertHasAttr(module, 'module2')
130130
self.assertEqual(module.module1.__name__, 'pkg.module1')
131131
self.assertEqual(module.module2.__name__, 'pkg.module2')
132132

@@ -136,15 +136,15 @@ def test_nonexistent_in_all(self):
136136
importer['pkg'].__all__ = ['non_existent']
137137
module = self.__import__('pkg', fromlist=['*'])
138138
self.assertEqual(module.__name__, 'pkg')
139-
self.assertFalse(hasattr(module, 'non_existent'))
139+
self.assertNotHasAttr(module, 'non_existent')
140140

141141
def test_star_in_all(self):
142142
with util.mock_spec('pkg.__init__') as importer:
143143
with util.import_state(meta_path=[importer]):
144144
importer['pkg'].__all__ = ['*']
145145
module = self.__import__('pkg', fromlist=['*'])
146146
self.assertEqual(module.__name__, 'pkg')
147-
self.assertFalse(hasattr(module, '*'))
147+
self.assertNotHasAttr(module, '*')
148148

149149
def test_invalid_type(self):
150150
with util.mock_spec('pkg.__init__') as importer:

Lib/test/test_importlib/import_/test_meta_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_empty(self):
4343
self.assertIsNone(importlib._bootstrap._find_spec('nothing',
4444
None))
4545
self.assertEqual(len(w), 1)
46-
self.assertTrue(issubclass(w[-1].category, ImportWarning))
46+
self.assertIsSubclass(w[-1].category, ImportWarning)
4747

4848

4949
(Frozen_CallingOrder,

Lib/test/test_importlib/import_/test_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_empty_path_hooks(self):
8080
self.assertIsNone(self.find('os'))
8181
self.assertIsNone(sys.path_importer_cache[path_entry])
8282
self.assertEqual(len(w), 1)
83-
self.assertTrue(issubclass(w[-1].category, ImportWarning))
83+
self.assertIsSubclass(w[-1].category, ImportWarning)
8484

8585
def test_path_importer_cache_empty_string(self):
8686
# The empty string should create a finder using the cwd.

Lib/test/test_importlib/import_/test_relative_imports.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def callback(global_):
8181
self.__import__('pkg') # For __import__().
8282
module = self.__import__('', global_, fromlist=['mod2'], level=1)
8383
self.assertEqual(module.__name__, 'pkg')
84-
self.assertTrue(hasattr(module, 'mod2'))
84+
self.assertHasAttr(module, 'mod2')
8585
self.assertEqual(module.mod2.attr, 'pkg.mod2')
8686
self.relative_import_test(create, globals_, callback)
8787

@@ -107,7 +107,7 @@ def callback(global_):
107107
module = self.__import__('', global_, fromlist=['module'],
108108
level=1)
109109
self.assertEqual(module.__name__, 'pkg')
110-
self.assertTrue(hasattr(module, 'module'))
110+
self.assertHasAttr(module, 'module')
111111
self.assertEqual(module.module.attr, 'pkg.module')
112112
self.relative_import_test(create, globals_, callback)
113113

@@ -131,7 +131,7 @@ def callback(global_):
131131
module = self.__import__('', global_, fromlist=['subpkg2'],
132132
level=2)
133133
self.assertEqual(module.__name__, 'pkg')
134-
self.assertTrue(hasattr(module, 'subpkg2'))
134+
self.assertHasAttr(module, 'subpkg2')
135135
self.assertEqual(module.subpkg2.attr, 'pkg.subpkg2.__init__')
136136
self.relative_import_test(create, globals_, callback)
137137

Lib/test/test_importlib/resources/test_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_reading(self):
2020
target = resources.files(self.data) / 'utf-8.file'
2121
with resources.as_file(target) as path:
2222
self.assertIsInstance(path, pathlib.Path)
23-
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
23+
self.assertEndsWith(path.name, "utf-8.file")
2424
self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))
2525

2626

Lib/test/test_importlib/source/test_finder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def run_test(self, test, create=None, *, compile_=None, unlink=None):
7373
if error.errno != errno.ENOENT:
7474
raise
7575
loader = self.import_(mapping['.root'], test)
76-
self.assertTrue(hasattr(loader, 'load_module'))
76+
self.assertHasAttr(loader, 'load_module')
7777
return loader
7878

7979
def test_module(self):
@@ -100,15 +100,15 @@ def test_module_in_package(self):
100100
with util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
101101
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
102102
loader = self.import_(pkg_dir, 'pkg.sub')
103-
self.assertTrue(hasattr(loader, 'load_module'))
103+
self.assertHasAttr(loader, 'load_module')
104104

105105
# [sub package]
106106
def test_package_in_package(self):
107107
context = util.create_modules('pkg.__init__', 'pkg.sub.__init__')
108108
with context as mapping:
109109
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
110110
loader = self.import_(pkg_dir, 'pkg.sub')
111-
self.assertTrue(hasattr(loader, 'load_module'))
111+
self.assertHasAttr(loader, 'load_module')
112112

113113
# [package over modules]
114114
def test_package_over_module(self):
@@ -129,7 +129,7 @@ def test_empty_string_for_dir(self):
129129
file.write("# test file for importlib")
130130
try:
131131
loader = self._find(finder, 'mod', loader_only=True)
132-
self.assertTrue(hasattr(loader, 'load_module'))
132+
self.assertHasAttr(loader, 'load_module')
133133
finally:
134134
os.unlink('mod.py')
135135

Lib/test/test_importlib/source/test_path_hook.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def path_hook(self):
1515

1616
def test_success(self):
1717
with util.create_modules('dummy') as mapping:
18-
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
19-
'find_spec'))
18+
self.assertHasAttr(self.path_hook()(mapping['.root']),
19+
'find_spec')
2020

2121
def test_empty_string(self):
2222
# The empty string represents the cwd.
23-
self.assertTrue(hasattr(self.path_hook()(''), 'find_spec'))
23+
self.assertHasAttr(self.path_hook()(''), 'find_spec')
2424

2525

2626
(Frozen_PathHookTest,

Lib/test/test_importlib/test_abc.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ def setUp(self):
4343
def test_subclasses(self):
4444
# Test that the expected subclasses inherit.
4545
for subclass in self.subclasses:
46-
self.assertTrue(issubclass(subclass, self.__test),
47-
"{0} is not a subclass of {1}".format(subclass, self.__test))
46+
self.assertIsSubclass(subclass, self.__test)
4847

4948
def test_superclasses(self):
5049
# Test that the class inherits from the expected superclasses.
5150
for superclass in self.superclasses:
52-
self.assertTrue(issubclass(self.__test, superclass),
53-
"{0} is not a superclass of {1}".format(superclass, self.__test))
51+
self.assertIsSubclass(self.__test, superclass)
5452

5553

5654
class MetaPathFinder(InheritanceTests):
@@ -424,14 +422,14 @@ def test_source_to_code_source(self):
424422
# Since compile() can handle strings, so should source_to_code().
425423
source = 'attr = 42'
426424
module = self.source_to_module(source)
427-
self.assertTrue(hasattr(module, 'attr'))
425+
self.assertHasAttr(module, 'attr')
428426
self.assertEqual(module.attr, 42)
429427

430428
def test_source_to_code_bytes(self):
431429
# Since compile() can handle bytes, so should source_to_code().
432430
source = b'attr = 42'
433431
module = self.source_to_module(source)
434-
self.assertTrue(hasattr(module, 'attr'))
432+
self.assertHasAttr(module, 'attr')
435433
self.assertEqual(module.attr, 42)
436434

437435
def test_source_to_code_path(self):
@@ -765,7 +763,7 @@ def test_package_settings(self):
765763
warnings.simplefilter('ignore', DeprecationWarning)
766764
module = self.loader.load_module(self.name)
767765
self.verify_module(module)
768-
self.assertFalse(hasattr(module, '__path__'))
766+
self.assertNotHasAttr(module, '__path__')
769767

770768
def test_get_source_encoding(self):
771769
# Source is considered encoded in UTF-8 by default unless otherwise

Lib/test/test_importlib/test_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ def test_everyone_has___loader__(self):
430430
for name, module in sys.modules.items():
431431
if isinstance(module, types.ModuleType):
432432
with self.subTest(name=name):
433-
self.assertTrue(hasattr(module, '__loader__'),
434-
'{!r} lacks a __loader__ attribute'.format(name))
433+
self.assertHasAttr(module, '__loader__')
435434
if self.machinery.BuiltinImporter.find_spec(name):
436435
self.assertIsNot(module.__loader__, None)
437436
elif self.machinery.FrozenImporter.find_spec(name):
@@ -441,7 +440,7 @@ def test_everyone_has___spec__(self):
441440
for name, module in sys.modules.items():
442441
if isinstance(module, types.ModuleType):
443442
with self.subTest(name=name):
444-
self.assertTrue(hasattr(module, '__spec__'))
443+
self.assertHasAttr(module, '__spec__')
445444
if self.machinery.BuiltinImporter.find_spec(name):
446445
self.assertIsNot(module.__spec__, None)
447446
elif self.machinery.FrozenImporter.find_spec(name):

Lib/test/test_importlib/test_lazy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ def test_delete_eventual_attr(self):
125125
# Deleting an attribute should stay deleted.
126126
module = self.new_module()
127127
del module.attr
128-
self.assertFalse(hasattr(module, 'attr'))
128+
self.assertNotHasAttr(module, 'attr')
129129

130130
def test_delete_preexisting_attr(self):
131131
module = self.new_module()
132132
del module.__name__
133-
self.assertFalse(hasattr(module, '__name__'))
133+
self.assertNotHasAttr(module, '__name__')
134134

135135
def test_module_substitution_error(self):
136136
with test_util.uncache(TestingImporter.module_name):

Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_cant_import_other(self):
8080

8181
def test_simple_repr(self):
8282
import foo.one
83-
self.assertTrue(repr(foo).startswith("<module 'foo' (namespace) from ["))
83+
self.assertStartsWith(repr(foo), "<module 'foo' (namespace) from [")
8484

8585

8686
class DynamicPathNamespacePackage(NamespacePackageTest):
@@ -301,7 +301,7 @@ def test_missing_directory(self):
301301

302302
def test_missing_directory2(self):
303303
import foo
304-
self.assertFalse(hasattr(foo, 'one'))
304+
self.assertNotHasAttr(foo, 'one')
305305

306306
def test_present_directory(self):
307307
import bar.two

Lib/test/test_importlib/test_pkg_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_package_import__semantics(self):
5555
except SyntaxError: pass
5656
else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
5757
self.assertNotIn(self.module_name, sys.modules)
58-
self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))
58+
self.assertNotHasAttr(sys.modules[self.package_name], 'foo')
5959

6060
# ...make up a variable name that isn't bound in __builtins__
6161
var = 'a'

0 commit comments

Comments
 (0)