Search code examples
pythonautocompletepycharm

Is there a way to have Python code completion work when inheriting from a variable array of classes?


I am working on a templatized and extensible python library and I want to do something like this:

registered_features = [
    feature1.feature.Support,
    feature2.feature.Support,
    feature3.feature.Support,
]

where each of those references a class in a directory structure like this:

feature1
--feature.py

and inside of feature.py is

class Support:
    def someExtendedFunctionality(self):
        ....

Then I inherit everything like this:

class App(*registered_features):
    def __init__(self):
        ....

Everything works at runtime, but the issue I hit is for code completion, I get no suggestions for the "registered_features"

Is there any way I can make this work? I'm guessing code completion is a static analysis and expanding that list is a runtime operation. Is there a different way to do something like this?


Solution

  • I'm assuming the classes in registered_features don't change and you are using it as a static object to hold a list of classes. If not and it changes during running time, the static analysis wouldn't be able to help with code completion.

    So, assuming it's a static class, you can replace it with this instead:

    class RegisteredFeatures(SupportA, SupportB, SupportC):
        pass
    
    
    class App(RegisteredFeatures):
        pass
    

    Instead of using a list object to hold all the classes, you use a class that is doing just about the same thing. I tested this and the code completion works with Pycharm 2021.2.3