FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44686 replace unittest.mock._importer with pkgutil.resolve_name by graingert · Pull Request #18544 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 4 additions & 23 deletions Lib/unittest/mock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import pprint
import sys
import builtins
import pkgutil
from asyncio import iscoroutinefunction
from types import CodeType, ModuleType, MethodType
from unittest.util import safe_repr
Expand Down Expand Up @@ -1239,25 +1240,6 @@ class or instance) that acts as the specification for the mock object. If
"""


def _dot_lookup(thing, comp, import_path):
try:
return getattr(thing, comp)
except AttributeError:
__import__(import_path)
return getattr(thing, comp)


def _importer(target):
components = target.split('.')
import_path = components.pop(0)
thing = __import__(import_path)

for comp in components:
import_path += ".%s" % comp
thing = _dot_lookup(thing, comp, import_path)
return thing


# _check_spec_arg_typos takes kwargs from commands like patch and checks that
# they don't contain common misspellings of arguments related to autospeccing.
def _check_spec_arg_typos(kwargs_to_check):
Expand Down Expand Up @@ -1611,8 +1593,7 @@ def _get_target(target):
except (TypeError, ValueError):
raise TypeError("Need a valid target to patch. You supplied: %r" %
(target,))
getter = lambda: _importer(target)
return getter, attribute
return partial(pkgutil.resolve_name, target), attribute

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is there any reason here to be using functools.partial over lambda?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

for consistency. lambda: is used twice in this file whereas partial( is used six times



def _patch_object(
Expand Down Expand Up @@ -1667,7 +1648,7 @@ def _patch_multiple(target, spec=None, create=False, spec_set=None,
for choosing which methods to wrap.
"""
if type(target) is str:
getter = lambda: _importer(target)
getter = partial(pkgutil.resolve_name, target)
else:
getter = lambda: target

Expand Down Expand Up @@ -1847,7 +1828,7 @@ def __enter__(self):
def _patch_dict(self):
values = self.values
if isinstance(self.in_dict, str):
self.in_dict = _importer(self.in_dict)
self.in_dict = pkgutil.resolve_name(self.in_dict)
in_dict = self.in_dict
clear = self.clear

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace ``unittest.mock._importer`` with ``pkgutil.resolve_name``.

Back | FazBrowse Home | New Git URL