'False Unused Import Statement in PyCharm?

Given this scenario:

b.py:

import A
# A is unused here

c.py:

from b import A
# A is used here

PyCharm complains in b.py that import A is an unused import and Optimize imports deletes it, breaking import in c.py.

I know these chained imports are not a good practice (although you may use it to implement a facade module), but is it me or is it a PyCharm fail?



Solution 1:[1]

You can actually use the PyUnresolvedReferences marker to deactivate the inspection for your import statement:

# noinspection PyUnresolvedReferences
import A

Reference: PyCharm bug PY-2240

Solution 2:[2]

from C import A, B
_ = (A, B); del _

Works for me. I don't like

# noinspection PyUnresolvedReferences

as it would give false negatives in case A cannot be imported. And

__all__ = ['A', 'B', ...]

is cryptic and is not convenient for refactoring.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 benselme
Solution 2