Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my IDE recognize dynamically added functions?

I'm currently developing a dynamic software system in python. It includes the use of dependency injection at many points, which is implemented with dynamic class attributes. My aim is to add a function dynamically to a class (with setattr) an this works quite fine. The only problem I have is that the IDE (in my case it's PyCharm) has no idea about those functions and marks it as "unresolved reference". Although the script runs without errors, it looks not very nice and I want the IDE to support other programmers, that don't know about those functions.

Here is an example of what I mean:

class A:
    def __init__(self, func_obj):
        setattr(self, 'custom_function', func_obj)

a = A(print) # Unresolved attribute reference 'custom_function' for class 'A' ...
a.custom_function('Hello World!')

As expected, this example prints "Hello World!". But I find it's ugly, that the IDE shows a warning for the last line, where I called "custom_function". And also there is no auto completion if you try to explore the script with duck typing.

So let me come to my question: Is there any way to tell the IDE that there are some dynamically added functions in this class? Maybe there is some way with meta classes or something like that... But I didn't find anything on Google and I have no idea where else to try.

I hope you can help me with that :)

like image 985
Lula Avatar asked Oct 21 '25 11:10

Lula


1 Answers

I had a similar request and asked directly to PyCharm bug tracker: PY-28326. I think that their answer applies to your question too:

PyCharm at the moment doesn't support methods dynamically added to classes.

like image 111
smarie Avatar answered Oct 22 '25 23:10

smarie