How to tell PyCharm that a class is a mixin specifically for class X

I'm writing a mixin for a huge class X and in this mixin I am using methods from X so I would love auto-complete to help me with that. Is there a way to tell PyCharm about my intentions?

class X(..., NewMixin):
    def do_something(self):

class NewMixin(object):
    def new_mixin_special_feature(self):
        self.do_... (<-- here I want PyCharm to help me)
Have you tried turning on collection of runtime types? Preferences > Python Debugger > "Collect run-time types for code insight". Turn this on and then run the program through the debugger - you shouldn't need to breakpoint.
Thanks, enabled, ran tests with debugger, even added a breakpoint, but that didn't help in my case. But I can understand why my use case is a bit abnormal because X depends on NewMixin and I want NewMixin to know of X which makes it a circular dependency.
I have exactly same problem, the Mixin uses bits of internals of a class and Pycharm inspection gladly marks them as error "blah blah defined outside of init", ...

I've found following solution

    class SomeMixin(object):

        def some_method(self):

            self = self  # type: SomeClass

            self.auto_complete_method()  # now IDE understands "self" type



Building on Ivan Klass, another way that avoids the assignment to itself warning is:


assert isinstance(self, SomeClass)


and now the IDE will understand that self can use SomeClass's methods.  However, the IDE will also forget that self is a member of SomeMixin, in case you use SomeMixin's methods elsewhere.  You could do something like:


clone = self

assert isinstance(self, SomeClass)

assert isinstance(clone, SomeMixin)


and then call SomeMixin's methods off of clone and SomeClass's methods off of self, and the IDE is happy.

(but eventually there should be another way...)



Please vote for the following feature request and follow it for updates.


or you can use typings:



class Mixin:

  def foo(self: 'Base'): pass


class Base(Mixin):

  def bar(self): pass



Please sign in to leave a comment.