Skip to content

Do not fallback when importing "unittest.mock" #68

Closed
@nicoddemus

Description

@nicoddemus
try:
    import mock as mock_module
except ImportError:
    import unittest.mock as mock_module

Use this instead:

if sys.version_info.major == 2:
    import mock as mock_module
else:
    import unittest.mock as mock_module

This way we don't "swallow" the first exception in Python 2 in case something goes wrong when importing mock.

EDIT: Just for the record, the first import mock as mock_module was raising an ImportError but not because mock was missing, one of its dependencies was not installed (it was a frozen application). This made this problem much harder to debug.

cc @gqmelo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions