Skip to content

gh-111881: Use lazy import in unittest #111887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
import functools
import difflib
import pprint
import re
import warnings
Expand Down Expand Up @@ -1065,6 +1064,8 @@ def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
except (TypeError, IndexError, NotImplementedError):
differing += ('Unable to index element %d '
'of second %s\n' % (len1, seq_type_name))

import difflib
standardMsg = differing
diffMsg = '\n' + '\n'.join(
difflib.ndiff(pprint.pformat(seq1).splitlines(),
Expand Down Expand Up @@ -1178,6 +1179,7 @@ def assertDictEqual(self, d1, d2, msg=None):
self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')

if d1 != d2:
import difflib
standardMsg = '%s != %s' % _common_shorten_repr(d1, d2)
diff = ('\n' + '\n'.join(difflib.ndiff(
pprint.pformat(d1).splitlines(),
Expand Down Expand Up @@ -1247,6 +1249,7 @@ def assertMultiLineEqual(self, first, second, msg=None):
secondlines = second_presplit.splitlines(keepends=True)

# Generate the message and diff, then raise the exception
import difflib
standardMsg = '%s != %s' % _common_shorten_repr(first, second)
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)
Expand Down
11 changes: 7 additions & 4 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import types
import functools

from fnmatch import fnmatch, fnmatchcase

from . import case, suite, util

__unittest = True
Expand Down Expand Up @@ -219,8 +217,12 @@ def shouldIncludeMethod(attrname):
fullName = f'%s.%s.%s' % (
testCaseClass.__module__, testCaseClass.__qualname__, attrname
)
return self.testNamePatterns is None or \
any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
if self.testNamePatterns is None:
return True
else:
from fnmatch import fnmatchcase
return any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)

testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
if self.sortTestMethodsUsing:
testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
Expand Down Expand Up @@ -339,6 +341,7 @@ def _get_module_from_name(self, name):

def _match_path(self, path, full_path, pattern):
# override this method to use alternative matching strategy
from fnmatch import fnmatch
return fnmatch(path, pattern)

def _find_tests(self, start_dir, pattern):
Expand Down
6 changes: 4 additions & 2 deletions Lib/unittest/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Unittest main program"""

import sys
import argparse
import os
import sys

from . import loader, runner
from .signals import installHandler
Expand Down Expand Up @@ -159,6 +158,7 @@ def _initArgParsers(self):
self._discovery_parser = self._getDiscoveryArgParser(parent_parser)

def _getParentArgParser(self):
import argparse
parser = argparse.ArgumentParser(add_help=False)

parser.add_argument('-v', '--verbose', dest='verbosity',
Expand Down Expand Up @@ -197,6 +197,7 @@ def _getParentArgParser(self):
return parser

def _getMainArgParser(self, parent):
import argparse
parser = argparse.ArgumentParser(parents=[parent])
parser.prog = self.progName
parser.print_help = self._print_help
Expand All @@ -208,6 +209,7 @@ def _getMainArgParser(self, parent):
return parser

def _getDiscoveryArgParser(self, parent):
import argparse
parser = argparse.ArgumentParser(parents=[parent])
parser.prog = '%s discover' % self.progName
parser.epilog = ('For test discovery all test modules must be '
Expand Down