mirror of
https://github.com/python/cpython
synced 2026-11-13 13:07:28 +03:00
Resolve lazy imports before using them as a mock spec
unittest.mock.patch() reads the attribute it replaces out of
target.__dict__, which for an unresolved lazy import holds a
types.LazyImportType placeholder. spec=True, spec_set=True and
autospec=True then specced the placeholder, so patching a function
produced a NonCallableMagicMock.
Reimplementation of python/cpython#153977:
- Resolve in _patch.__enter__(), and only when the spec is taken from
the original, rather than unconditionally in get_original().
Resolving on every patch() breaks patch("mod.dep", new=...) for a
lazy import of a module that is not installed.
- Test spec and spec_set as well as autospec, as asked for in review.
- Build the lazy binding in the test instead of importing json lazily
at module level: "lazy from" binds eagerly when the module is already
imported, so that test passed without the fix too.
(cherry picked from commit
|
||
|---|---|---|
| .. | ||
| __init__.py | ||
| __main__.py | ||
| _log.py | ||
| async_case.py | ||
| case.py | ||
| loader.py | ||
| main.py | ||
| mock.py | ||
| result.py | ||
| runner.py | ||
| signals.py | ||
| suite.py | ||
| util.py | ||