Type hinting in Eclipse with PyDev

前端 未结 5 946
醉话见心
醉话见心 2021-02-18 22:02

I\'m studying Python, after a lot of PHP experience, and it would be handy to have type-hinting in Python. Looks like Eclipse with PyDev doesn\'t support this. Any sug

5条回答
  •  太阳男子
    2021-02-18 22:45

    Present Python 2/3

    For local scope variables and function parameters PyDev has this:

    assert isinstance(obj, MyClass)
    obj. # here hint will work
    

    Though I guess it's a not documented feature. Here's PyDev's official page for type hints and couple of excerpts which illustrate Sphinx syntax.

    class Example:
    
      def param(self, a):
        ''':type a: MyClass'''
    
      def var(self, iterable):
        for a in iterable: #: :type a: AnotherClass
            pass
    

    Unfortunately, neither works for class members.

    Since, PyDev 4 there's also something that can resemble PEP-484 (see below).

    class LatestExample:
    
      def listcase(self, param):
        ''':type param: list[str]'''
    
      def dictcase(self, param):
        ':type param: dict[str, MyClass]'
    

    Future Python 3

    Take a look at answer by @slushy. No doubt this is the future. But for the time being PyDev supports neither function annotations, PEP-3107, nor new PEP-484 stuff @slushy demonstrates. PEP-484 is coming in Python 3.5 in some limited form, and in 3.6 in final. Here's BDFL's PyCon 2015 presentation for type hints and PEP-484.

提交回复
热议问题