Roman Susi
Roman Susi

Reputation: 4199

How to PyCharm import inside a class is actually used?

While using the following pattern:

class A(object):
    from my_methods import a_method as a
    from my_methods import b_method as b

I encountered a problem: PyCharm considers those methods as unused and Optimize imports remove them.

One way to mark methods as used is to mention them in a class, for example:

class A(object):
    from my_methods import a_method as a
    from my_methods import b_method as b
    a, b

But then PyCharm complains about this line is having no effect.

Is there the right way to solve the problem? Maybe some hints or pragmas?

The methods are used indirectly, so PyCharm can't deduce their usage (there are no direct A().a() calls in the code).

Upvotes: 0

Views: 75

Answers (1)

user3657941
user3657941

Reputation:

Do you ever call the methods?

my_methods.py

def a_method(self):
    pass

def b_method(self):
    pass

optimize.py

class A(object):
    from my_methods import a_method as a
    from my_methods import b_method as b

def _unused():
    x = A()
    x.a()
    x.b()

When I ran [Code]->[Optimize Imports] on optimize.py, the imports were not removed.

The _unused can be also a method of the class A, and may serve for tests and documentation.

Upvotes: 1

Related Questions