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

Answered
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):
        pass


class NewMixin(object):
    def new_mixin_special_feature(self):
        self.do_... (<-- here I want PyCharm to help me)
1
8 comments
Avatar
Permanently deleted user
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.

http://stackoverflow.com/a/17821632/858289
1
Avatar
Permanently deleted user
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.
0
Avatar
Permanently deleted user
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", ...
1

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

 

1

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...)

0

Hi,

Please vote for the following feature request https://youtrack.jetbrains.com/issue/PY-7712 and follow it for updates.

0

or you can use typings:

 

```

class Mixin:

  def foo(self: 'Base'): pass

 

class Base(Mixin):

  def bar(self): pass

```

1

Please sign in to leave a comment.