Ask Your Question
-1

Fail to execute `Goto Definition ' for aiida-quantumespresso's band_structure Workchain invoked like this: PwBandStructureWorkChain = WorkflowFactory('quantumespresso.pw.band_structure')

asked 2020-05-02 08:28:45 -0500

hongyi-zhao gravatar image

updated 2020-05-04 15:51:42 -0500

Hi,

aiida-quantumespresso is a aiida-core's plugin located here: https://github.com/aiidateam/aiida-qu... which including the Workchain for computing a band structure. The source code is located here: https://github.com/aiidateam/aiida-qu....

When I using the following method to import it:

from aiida_quantumespresso.workflows.pw import band_structure

When I put the mouse on band_structure, I can find the Goto Defination option.

While this Workchain can also be used as class object passing method like this:

PwBandStructureWorkChain = WorkflowFactory('quantumespresso.pw.band_structure')

The WorkflowFactoryfunction is defined here: https://github.com/aiidateam/aiida-co...

But in this case, if I put mouse on the quantumespresso.pw.band_structure, the wing won't display the Goto Defination option at all.

Any hints?

Regards

edit retag flag offensive close merge delete

Comments

Is there an easy way to install this in a Python 3 environment? The pip package is for Python 2.7. Or are you using Python 2.7?

Wingware Support gravatar imageWingware Support ( 2020-05-04 10:21:22 -0500 )edit

It seems there is no up-to-date version of that package on pip|conda. But it can be installed using git repo directly or clone the repo and then install locally with pip. You should first install the aiida-core package. I demonstrate the steps using conda as follows:

$ conda create -n aiida-core aiida-core python=3.8
$ conda activate aiida-core
$ reentry scan
$ git clone https://github.com/aiidateam/aiida-quantumespresso.git aiida-quantumespresso.git 
$ cd aiida-quantumespresso.git 
$ pip install  -e --isolated .

I use python 3.8.2.

Regards

hongyi-zhao gravatar imagehongyi-zhao ( 2020-05-04 19:17:29 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-05-06 16:51:52 -0500

Wing's static analyzer doesn't understand dynamic functions such as WorkflowFactory, which seems to be a specialized way to import modules. In cases like this, you can add type hints to help the static analyzer. An example could be:

from aiida_quantumespresso.workflows.pw import band_structure
from aiida.plugins.factories import WorkflowFactory
PwBandStructureWorkChain = WorkflowFactory('quantumespresso.pw.band_structure')
inst = PwBandStructureWorkChain()
inst: band_structure.PwBandStructureWorkChain

The type hint specifies that inst is an instance of the band_structure.PwBandStructureWorkChain class and with it, autocomplete lists attributes & methods and goto definition works on the methods.

edit flag offensive delete link more

Comments

Though this method can do the trick, it's cumbersome. As you can see, I must manually add the type hints for each one similar to this case. Could you please add this feature into wing so that it can automatically do this job for the user?

If so, the runtime auto-completions for dynamically loaded types can also be seen by the static analyzer without bothering the debug mode.

Regards

hongyi-zhao gravatar imagehongyi-zhao ( 2020-05-06 19:46:11 -0500 )edit

You may want to write a convenience function such as

from aiida_quantumespresso.workflows.pw import band_structure
from aiida.plugins.factories import WorkflowFactory
def CreateBandStructureObject():
  PwBandStructureWorkChain = WorkflowFactory('quantumespresso.pw.band_structure')
  inst = PwBandStructureWorkChain()
  inst: band_structure.PwBandStructureWorkChain
  return inst

Then you can use CreateBandStructureObject() in multiple places in your code. Note that I know very little about aiida so I don't know if the above code makes sense or not.

In general, tools that do static analysis like Wing prefer straightforward Python code than dynamic code that relies on interpreting strings to decide which module to load. So inst = band_structure.PwBandStructureWorkChain() would be understood while using WorkflowFactory is not (again, I know very little about aiida so I don't know if inst = band_structure.PwBandStructureWorkChain() would work).

You may want to work with a debug process active; I often use unittest test cases for things like this so I can quickly run ...(more)

Wingware Support gravatar imageWingware Support ( 2020-05-07 10:23:14 -0500 )edit

I really want to revise the source code to implement this method so that I can use it directly, say for this case, add some codes in the band_structure.py module. I tried to add the above defined function by you to band_structure.py but failed to do the trick. Any hints for my problem?

Regards

hongyi-zhao gravatar imagehongyi-zhao ( 2020-05-07 19:20:36 -0500 )edit

What specific problems are you having? The names would need to be adjusted for use in that file. It's also generally not a good idea to modify pip installed files unless they are temporary and you plan to contribute them to the package maintainer.

Wingware Support gravatar imageWingware Support ( 2020-05-08 09:02:24 -0500 )edit

As far as I know, the aiida and its plugins use many such methods for relative modules. So, if I want to use this method for all the interfaces/functions/classes supplied by this method, I will need so many code lines to define the type hint function. So I want to have a method which can fix this problem in the souce code.

Regards

hongyi-zhao gravatar imagehongyi-zhao ( 2020-05-08 17:04:44 -0500 )edit
0

answered 2020-05-06 19:56:33 -0500

hongyi-zhao gravatar image

updated 2020-05-07 00:17:22 -0500

Another issue:

If I rewrite your code as following:

from aiida_quantumespresso.workflows.pw import band_structure
from aiida.plugins.factories import WorkflowFactory
inst = WorkflowFactory('quantumespresso.pw.band_structure')
PwBandStructureWorkChain = inst()
PwBandStructureWorkChain: band_structure.inst

Then PwBandStructureWorkChain.x<tab> won't give the completion.

Any hints?

Regards

edit flag offensive delete link more

Comments

band_structure.inst doesn't exist in band_structure.py as far as I can see. In general, you want to use classes defined with class statements in type hints. A type hint tells Wing and other tools that the name to the left of : is an instance of the class to the right (to simplify a bit).

Wingware Support gravatar imageWingware Support ( 2020-05-07 10:08:29 -0500 )edit

Thanks for your explanation. I really want to revise the band_structure.py itself with the code snippet given by you. But I tried directly put it into somewhere at the beginning of that file, it failed to work.

Any hints for this?

Regards

hongyi-zhao gravatar imagehongyi-zhao ( 2020-05-07 18:57:53 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-05-02 08:28:45 -0500

Seen: 33 times

Last updated: May 07