documenting an alternate constructor?

I have code a little like this:

class Base(object):
    @classmethod
    def alternateConstructor(clazz, x):
        self = clazz()
        return self

class Things(Base):
    def frob(self, y):
        pass

def main(x):
    thing = Things.alternateConstructor(x)

and I'd like pycharm to be able to infer that "thing" an instance of "Things", but it doesn't seem to know that.  Is there any way I can annotate the alternateConstructor to help it out?

2 comments
Comment actions Permalink

Hello Kevin,

You can use epydoc or sphinx to specify the return rype of the alternateConstructor

function (or any other function, not only a constructor-like one).

I have code a little like this:

class Base(object):
> @classmethod
> def alternateConstructor(clazz, x):
> self = clazz()
> return self
> class Things(Base):
> def frob(self, y):
> pass
> def main(x):
> thing = Things.alternateConstructor(x)
> 

and I'd like pycharm to be able to infer that "thing" an instance of

"Things", but it doesn't seem to know that.  Is there any way I can

annotate the alternateConstructor to help it out?

---

Original message URL:

http://devnet.jetbrains.net/message/5318840#5318840

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Well, it appears as if it already understands that alternateConstructor returns a Base object.  But I'm not going to document Base.alternateConstructor with ":rtype: Things" because that's not accurate.  It would probably work to override Base.alternateConstructor as Things.alternateConstructor, but that would be otherwise redundant and serve no purpose to the code itself.

0

Please sign in to leave a comment.