Posted 9 years ago by tal
Version: 14.2.0610
Avatar

Hey,

I’m looking at the new Python Language Add-on for SyntaxEditor sample.
First of all, looks really good!

Second, I want to implement a small test program for this evaluation and to do that I have 3 things I wish to try:

1. I would like to add a standard capability of static python code analysis (like in other IDEs):
Assuming a parameter “count” is received in a function, I would like to enable the auto-complete on it, and usually it is done by making an assert statement before using that parameter (obviously this example is not the best one but just replace the type and it becomes more meaningful...):

def __init__(self, count):
    assert isinstance(count, int)

or even:
    assert isinstance(count, (int, long))

Is it possible to add that capability to the parser and allow him to understand these kind of hints?
If so can you provide me with some pointers or starters on how to do that?

2. I would also like to provide some more variables which are not present in the document, and will be at run-time, to the auto-completion mechanism. Is it possible?
Reading the documentation, I think the best way to go is to make a decoration around the PythonCompletionProvider class. Am I in the right way?

3. I wish also try and split the entire document to several active modules (ModuleDefinition), is it possible to do that (as the returned Context for the entire document)?

In thanks,
Tal

Comments (11)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tal,

Thanks for evaluating our products.

1) We don't have that capability at this time but that is an interesting suggestion.  We'll write down the idea.  Which IDEs do you know of that use the assert to derive type information?

2) Yes, you could make a class that inherits PythonCompletionProvider and override the OnSessionOpening method.  You can add/remove items there before the completion session opens.  Then register your custom class instance as a PythonCompletionProvider service type on your language to replace the existing service.

3) I'm not quite sure what you are trying to do here.  Can you provide some more detail?


Actipro Software Support

Posted 9 years ago by tal
Avatar

Hey,

Thanks for the fast response!

1) I believe this is one of the most important feature for python static analysis. A list of IDE's that supports this feature:
    Visual Studio (+ Python Tools for VS)
    PyDev (http://sebthom.de/140-leveraging-pydevs-auto-completion-indirectly-created-objects/)
    Wing (http://www.wingware.com/doc/edit/helping-wing-analyze-code)
    I haven't checked but I suppose Komodo and PyCharm also support this…

I find it most useful to place these asserts in the start of every function that receives parameters, especially in the constructor in order to allow the auto-completion to understand all the members I set in “self” (the type is set and remembered throughout the class usage).
Using this method can also result in more than one interpretation of a single variable (and then the auto-complete is provided as a union for all of them…):

assert isinstance(obj, (Class1, Class2))
   or even
assert obj is None or isinstance(obj, (Class1, Class2))

If you are going to implement this, do you have an estimate on when would it be released?

2) I’ll try doing that, thanks!

3) I want to be able to display (and understand the code AST + collapse code + syntax error display + IntelliPrompt…) for more than one module in the same view but to allow each one to be analyzed and understood independently.

Thanks again,
Tal

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tal,

1) Ok thanks for the suggetion and examples. We have some more immediate work for the add-on on our plate but will consider looking into this after.

3) Sorry but each document being edited by a SyntaxEditor gets associated with a single "module".  There isn't a direct way to split the document up into multiple registered modules.  You could possibly watch document text change events, then split and copy various text ranges to other documents where each of those documents represents one of your modules.  That might get working what you are trying to do.  But please note that if you have large documents, continuously slicing up long text runs (at least if you do it in the UI thread) might affect editing performance.


Actipro Software Support

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tal,

As an update for the parameter assert type hint suggestion, we definitely plan on adding this as part of the major updates that are coming to the Python add-on.  I'm not yet sure if they will be in the upcoming 2014.2 maintenance release or whether they will be part of the first 2015.1 release, but it will be in one of those.


Actipro Software Support

Posted 9 years ago by tal
Avatar

Sounds amazing!

I would like to also point out that there are other kinds of asserts that can be of a service here;

if you would like another example:

assert issubclass(cls, MyClass) -> cls is now at least a MyClass class

cls.my_static_method(...) # should be auto completed...

a = cls() -> a is a MyClass instance (or one if the subclasses instance)

 

It is also possible to extend those and also infer the type of a function's parameters if those are the first lines the are executed inside the function (but I think that is a bit more complex) :)

Thanks again,

Tal

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tal,

This sort of thing should already be working where 'cls' will recognize it's the owner type:

class Test:
    def foo(self, x):
        pass
    
    @classmethod
    def bar(cls):
        return cls.|

That being said, the 'cls()' code isn't yet recognized by our resolver.  We will try and add that for our major updates to the add-on (likely coming in the 2015.1 version).


Actipro Software Support

Posted 9 years ago by tal
Avatar

Sorry to mislead you, a full example's code could be something like:

def my_any_func(p1, p2):
    assert isinstance(p1, MyClass)
    assert p2 is None or issubclass(p2, MyBaseClass)
    p1.$auto-complete$() # p1 is now an instance of MyClass and can be auto-completed
    if p2 is not None:
        p2.$auto-complete$() # p1 is now at least a MyBaseClass class
        a = p2("string init") # a is a MyBaseClass instance
        a.$auto-complete$()

 :)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tal,

Thanks for the explanation.  That makes sense and we'll try and get that added as well.


Actipro Software Support

Posted 9 years ago by tal
Avatar

Hey,
I downloaded the new evaluation version (15.1.0620).
First I would like to thank you for this new addition! It came out really well.
I ran a few stress testing on this new feature and found out three missing expected behaviors that will complete this new feature that you might want to consider adding in a future release.
The first one is when using the assert with “or x is None”:

assert x is None or isinstance(x, int)

after that, currently, the editor doesn’t assume x can be int.
The second one is when using assert issubclass and saving that value in ‘self’ the autocomplete on it outside that function is empty:

class A(object):
  def __init__(self, x):
    assert issubclass(x, int)
    self.x = x
a = A(int)
a.x.<here>

The last one is when using this assert inside more than one function (or setting it to different type) and saving the value in self:

class A(object):
  def __init__(self):
    self.z = None
  def set_z(self, value):
    assert isinstance(value, str)
    self.z = value
a = A()
# a.set_z("z")
a.z.<here>

By the way I noticed the new “public/all” option in the auto-complete session. Is it possible to change it automatically according to the start of the completed text (toggle show all if text begins with ‘_’, and hide again when it doesn't - unless the user selects an option by himself)?
Thanks again, amazing update,
Tal

Answer - Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

We're glad to hear you love the update.

1/2) I believe we can improve these scenarios for the next maintenance release.

3) This one isn't likely to be improved because you'd need an interpreter (which we don't have) running in the background to know more about advanced scenarios like that.

For the public/all switch, that's an interesting idea that we can mark down for possibly doing in the future.


Actipro Software Support

Posted 9 years ago by tal
Avatar

Great, thanks!

By the way, regarding 3 – I was just expecting the auto-complete to include all options from all possible assignments (and not just the first one as in this case, in __init__) - not to interpret the state of the value.
Similary to using:

assert z is None or isinstance(z, str)

Thanks again!

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.