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)
Please sign in to leave a comment.
http://stackoverflow.com/a/17821632/858289
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...)
Hi,
Please vote for the following feature request https://youtrack.jetbrains.com/issue/PY-7712 and follow it for updates.
or you can use typings:
```
class Mixin:
def foo(self: 'Base'): pass
class Base(Mixin):
def bar(self): pass
```