Skip to content

"Already defined error" with conditional importing #7557

@ngie-eign

Description

@ngie-eign

The following code triggers a false positive:

try:
    from unittest import mock  # python 3
except ImportError:
    import mock  # python 2

Example:

$ mypy ~/mypy_false_positive.py
mypy_false_positive.py:4: error: Name 'mock' already defined (by an import)

The false positive is tautilogically impossible, since unittest.mock failing to import would trigger the second statement on line 4.

This is a continuation of #1153.

I'm opening a new issue because #1153 has been closed for ~3 years now and I wanted to provide more up-to-date/salient details.

Additional details:

$ mypy --version
mypy 0.720
$ head -n 1 `which mypy`
#!/usr/bin/python3
$ python3 --version
Python 3.7.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions