Make Python Tools to aware of variable type

Jul 26, 2011 at 7:17 AM
Edited Jul 26, 2011 at 7:21 AM

Python is a dynamic language thus in many cases it is hard for Intellisense to determine the type of a variable. Even simple things might be unpredictable for code analyzers. But the developer is aware what type to await most of such cases. 

If one knows exactly what the type of 'this' variable is, is it possible somehow to make VS2010 Python Tools to know what the type is it too, to have full Intellisense support fur this variable?

Some editors, for example, accept isinstance(variable, Type) to determine type in such cases. Some other editors use comments. 

Is it implemented somehow now?

Is it in plans of the future development?

Editor
Jul 26, 2011 at 6:52 PM

Hi,

To answer your specific question, we don't use the isinstance call to infer types, but we have an open feature request for this; please vote it up as it will help us prioritize our work in the next release: http://pytools.codeplex.com/workitem/49

We also have an open feature to use type hints in Python 3.x (this can be complicated because they can essentially be any expression, so to do it right we have to do sophisticated parsing), please vote it up as well: http://pytools.codeplex.com/workitem/394

That said, PTVS does some type inference for you already:

  • if you assign a value to a variable, we will do type inference: foo = 1 -> we know it's an int; foo = Class() -> we know it's a Class.
  • We can reason about ad hoc types. If you do for i in [(1,2.1), (3,4.2), (5,6.3)] we will infer that i is type (int, float)
  • When you call a function with values, foo(1, Class()) we will infer the types of the function parameters from the function call.
  • if you assign i = 1 in one place and i = 3.5 in another we will give intellisense on the intersection of the members of float and int.  Similarly if you assign i=1 somewhere and i="foo" somewhere we'll give you the intersection of float and str. (This can be irritating because we don't make an attempt to sort out if i is just an iteration variable, so for i in range(5) in one place and for i in "hello world" in another means we always infer i to be the intersection of int and str.)
Jul 29, 2011 at 5:23 PM
Edited Jul 29, 2011 at 5:25 PM

Thank you very much for your response! I really appreciate what You are doing to develop python tools. 

I have an example that we run on CPython 2.7 :

in my library in directory mylibrary/cmd we have a class

 

class ConsoleContext:   
       """ manage console commands"""
       .... #long long class contents with many members

 

 

in the same directory we have a class:

 

from mylibrary.cmd import ConsoleContext

class ConsolePluginBase:
       """base class for console plugins"""
       
      context = ConsoleContext() #We tried to put ConsoleContext() for intellisence to know what the type is

 

Then we create a derived class somewhere else (all python paths are correct):

 

from mylibrary.cmd import ConsolePluginBase

class SomePlugin(ConsoleUtilBase):
 
     def process(self, args):
        assert self.context != None
        provider = self.context. #<- that is the point where intellisence don't know what to do

 

So at this point Intellisence doesn't know what self.context is.

Maybe we did some mistake (like it is hard for Intellisence to process from ... import...) and it is possible to have completion at this point?


Coordinator
Jul 30, 2011 at 7:14 PM

Does goto definition work for ConsoleUtilBase where SomePlugin is inheriting from it?  Does goto definition work for ConsoleContext where it's being created in ConolePluginBase?  Just trying to figure out where it's not working, I'll see if I can setup a repro as well.

Jul 31, 2011 at 1:16 AM

"Does goto definition work for ConsoleUtilBase where SomePlugin is inheriting from it?" - yes it works

" Does goto definition work for ConsoleContext where it's being created in ConolePluginBase?" - this one works too. 

Looks like it is not working in ConolePluginBase. It goes to ConsoleContext by 'goto definition' properly. But at the same time, in ConolePluginBase

self. - one gets the context among the list

self.context. - do nothing. 

 

Coordinator
Jul 31, 2011 at 2:45 AM

Ok, one more question - Does ConsoleUtilBase derive from ConsolePluginBase and where is it defined vs the other modules?

Coordinator
Aug 1, 2011 at 6:10 PM

I think a simple repro of this is:

 class NoDescriptor:   
       def f(self): pass

 class SomeClass:
    foo = NoDescriptor()

    def f(self):
        self.foo
        pass

 

No comletions on self.foo - we are looking for __get__ on NoDescriptor and if it doesn't have one we lose the fact that we should just return NoDescriptor it's self.

One workaround would be to define __get__ on the ConsoleContext class and return self although that's kind of ugly.

Coordinator
Aug 1, 2011 at 6:12 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Aug 1, 2011 at 7:46 PM

The fix for this is now checked in.  We'll be releasing RC2 soon so please try it out and let us know if it's still broken.  Thanks!

Aug 3, 2011 at 12:21 AM

Thank you very much for your response. 

The issue is still there. But I didn't define __get__ for the class.  It is not clear for me, should I define descriptor or it was workaround only until bug is fixed. 

P.S. Should I continue to answer here or to the work item?

P.P.S. ConsoleUtilBase  is the same ConsolePluginBase. I've just missed up the name. 

Thank you once more!

Coordinator
Aug 3, 2011 at 3:48 AM
hi Tiphon-

yes, would you kindly report on codeplex so that dino can see it too?

thanks!



From: [email removed]
To: [email removed]
Date: Tue, 2 Aug 2011 16:22:02 -0700
Subject: Re: Make Python Tools to aware of variable type [pytools:266554]

From: Tiphon
Thank you very much for your response.
The issue is still there. But I didn't define __get__ for the class. It is not clear for me, should I define descriptor or it was workaround only until bug is fixed.
P.S. Should I continue to answer here or to the work item?
P.P.S. ConsoleUtilBase is the same ConsolePluginBase. I've just missed up the name.
Thank you once more!
Read the full discussion online.
To add a post to this discussion, reply to this email (pytools@discussions.codeplex.com)
To start a new discussion for this project, email pytools@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com