Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydev Data Type hint for Class members

According to pydev manual, one can provide type hints for variables using comments http://pydev.org/manual_adv_type_hints.html

I am trying to inform pydev about type of class members (so that code completion and intellisense can work properly).

Here is what I have tried:

class a:
    def __init__(self):
        self.a=None
        self.b=None

    def setVariable(self,a,b)
        self.a=a
        self.b=b

    def doSomething(self):
        ': :type self.a: packageX.moduleY.ClassZ'
        # use self.a from here on

But it doesn't seem to work. Can anyone shed some light?

like image 573
Suryavanshi Avatar asked Mar 18 '14 01:03

Suryavanshi


People also ask

How do you write a class hint in Python?

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ).

What are type hints?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here's an example of adding type information to a function.

Does Python enforce type hints?

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code. Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types.

How do you define a class variable in Python?

A class variable is declared inside of class, but outside of any instance method or __init__() method. By convention, typically it is placed right below the class header and before the constructor method and other methods.


1 Answers

It seems that Pydev (3.4.1) doesn't support docstring-type-hinting for class attributes, as well as propagation of type-hinted variables into class attributes.

Here's an example I'd expect to work: (Given this class, I expect for completion suggestions in the last two lines of the example code, for the reasons mentioned in the comments on these lines.)

class SampleClass(object):
    """Summary of class here.

    Attributes:
        member: Member
        @type member: MemberClass
        other_member: Another member
    """

    def __init__(self, other=None):
        """Inits SampleClass with blah.

        Args:
            @param other: Instance of OtherMemberClass
            @type other: OtherMemberClass
        """
        self.other_member = other
        self.member = None

    def public_method(self):
        """Performs operation blah."""
        self.member.#should get completions from MemberClass here because hinted in class docstring
        self.other_member.#should get completions from OtherMemberClass here because `other` was assigned to attribute in __init__, and `other` type was hinted in __init__ docstring.

I opened a ticket on the Pydev tracker regrading this.

like image 170
Itamar Avatar answered Sep 30 '22 17:09

Itamar