Skip to content

importmagic implemented - #636

Closed
Vladimir Urushev (pilat) wants to merge 3 commits into
microsoft:masterfrom
pilat:importmagic
Closed

Vladimir Urushev (pilat) wants to merge 3 commits into
microsoft:masterfrom
pilat:importmagic

Conversation

@pilat

@pilat Vladimir Urushev (pilat) commented Jan 26, 2018 •

Copy link
Copy Markdown

I tried to implement python importmagic. Perhaps it will be resolved issue #21

It works like this

ky147jykqx

@msftclas

Microsoft Contribution License Agreements (msftclas) commented Jan 26, 2018 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@codecov

codecov Bot commented Jan 26, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #636 into master will increase coverage by 0.4%.
The diff coverage is 74.3%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #636     +/-   ##
=========================================
+ Coverage   60.69%   61.09%   +0.4%     
=========================================
  Files         232      236      +4     
  Lines       10502    10825    +323     
  Branches     1830     1889     +59     
=========================================
+ Hits         6374     6614    +240     
- Misses       4122     4205     +83     
  Partials        6        6
Impacted Files Coverage Δ
src/client/extension.ts 94.26% <100%> (+0.22%) ⬆️
src/client/providers/importMagicProvider.ts 58.24% <58.24%> (ø)
.../client/providers/importMagicCompletionProvider.ts 69.81% <69.81%> (ø)
src/client/providers/importMagicProxy.ts 82.78% <82.78%> (ø)
...client/languageServices/importMagicProxyFactory.ts 86.36% <86.36%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 36b3050...45e40b8. Read the comment docs.

@brettcannon

Copy link
Copy Markdown
Member

Thanks for the PR, Vladimir Urushev (@pilat) ! We will have a look at this when we have the time (should be some time in February as we just hit code freeze for our next release).

public async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position,
token: vscode.CancellationToken, context: CompletionContext): Promise<vscode.CompletionItem[]> {
if (!this.enable) {
return Promise.resolve([]);

@elliott-beach Elliott Beach (elliott-beach) Feb 5, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: why not return [] instead of Promise.resolve([]) here and in other async functions, as async functions return promises automatically? That matches the current style of project.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right! I have already fixed it.

@DonJayamanne

Copy link
Copy Markdown

Vladimir Urushev (@pilat)
I'm sorry for the delay. Current'y we're very busy with at least two major engineering items for this months release. We will be reviewing this PR as soon as possible. Hopefully sometime around the 3rd week of Feb.

@pilat

Copy link
Copy Markdown
Author

Don Jayamanne (@DonJayamanne)
Ok! Thank you!

json_message = {'error': True, 'message': 'Importmagic not installed',
'traceback': '', 'type': 'ModuleNotFoundError'}
sys.stderr.write(json.dumps(json_message))
sys.stderr.write('\n')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

from __future__ import print_function

print(json.dumps(json_message), file=sys.stderr)

method_name = request.get('method')

result = None
method = getattr(self, 'method_%s' % method_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much more readable with a simple if/elif/elif switch. Dynamically constructing method names and using getattr to access code means that you will never be able to find where a particular function is used.

self._input = io.open(sys.stdin.fileno(), encoding='utf-8')
self._index = None

def method_build_index(self, **kwargs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these named method_*?

python_source = fd.read()
scope = importmagic.Scope.from_source(python_source)

_unresolved, _unreferenced = \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leading underscore typically denotes unused variables declared only for unpacking:

foo, _dummy = function_that_returns_a_tuple()

raise ValueError('Empty module')

if not self._index:
raise Exception('First run inibuild_indext() method')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no inibuild_indext.

python_source = fd.read()

imports = importmagic.Imports(self._index, python_source)
imports.set_style(multiline='backslash', max_columns=79) # TODO: Get style from VS Code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will likely become a blocker for merging this change. You could import isort and use its configuration parser to determine proper settings for the file. This would make sure whatever this tool does is not immediately flagged by an isort checker.

result = method(**request)

if not result or type(result) != dict:
raise ValueError('Method must return dict')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something that should be covered by tests rather than a runtime check.

response = self._process_request(request)
json_message = dict(id=request_id, **response)
self._success_response(json_message)
except:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unqualified except is bad. It will capture unexpected things, like SystemExit or KeyboardInterrupt.

@balta2ar

Copy link
Copy Markdown

Gentle ping, Vladimir Urushev (@pilat). It's a really nice feature I'd like to see merged.

@brettcannon

Copy link
Copy Markdown
Member

There's currently no plans on accepting this PR as it has merge conflicts which have to get fixed first.

We are also working on some intellisense changes which should help with getting this feature solved as well at some point, even if we don't get this specific PR merged.

@mizzao

Andrew Mao (mizzao) commented Mar 19, 2018 •

Copy link
Copy Markdown

This is a really nice feature that would save many Python devs a ton of time. It's been around a while: DonJayamanne/pythonVSCode#764. I hope we'll see this in production soon!

Vladimir Urushev (@pilat): is there a way to test this feature out on a local version?

@timonweb

Copy link
Copy Markdown

Just wondering, what's the status of this issue?

@brettcannon

Copy link
Copy Markdown
Member

Timur (@timonweb) we have an experimental analysis engine that is being developed which will enable this, so we're in a weird spot where we could either merge this now and then probably toss the work later, or simply wait for the new engine to land and make that do this.

@qubitron

Copy link
Copy Markdown

Mikhail Arkhipov (@MikhailArkhipov) do you think we could re-use this approach in the Python language server? When would be the right time to start looking at it?

@jason-curtis

Copy link
Copy Markdown

Brett Cannon (@brettcannon), is there an update on this or the experimental analysis engine?

@brettcannon

Copy link
Copy Markdown
Member

thatneat it's basically at the top of our roadmap to add this to the language server (after fixing bugs of course 😉 ).

@jason-curtis

Jason Curtis (jason-curtis) commented Aug 30, 2018 •

Copy link
Copy Markdown

Looks like the thing to do is use this extension instead: https://marketplace.visualstudio.com/items?itemName=brainfit.vscode-importmagic
[Edit: had a stale page, just saw the above comment. awesome!]

@brettcannon

Copy link
Copy Markdown
Member

I'm going to close this as the plan is to put this functionality into the language server. Thanks to Vladimir Urushev (@pilat) for taking the time to create this PR!

@jason-curtis

Copy link
Copy Markdown

awesome, can't wait! Is there somewhere we can track the corresponding feature progress on the language server? Or should we just watch changelogs to find out when this comes out?

@brettcannon

Copy link
Copy Markdown
Member

thatneat microsoft/python-language-server#19

@lock lock Bot locked as resolved and limited conversation to collaborators Jul 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.