RobertOstermann / vscode-inline-parameters

An Visual Studio Code extension that adds inline parameter annotations.
https://marketplace.visualstudio.com/items?itemName=RobertOstermann.inline-parameters-extended
MIT License
29 stars 6 forks source link

Python inline parameters won't work for Python<3.6 #4

Closed l1997i closed 2 years ago

l1997i commented 2 years ago

Thanks so much for the awesome extension.

I found a bug for Python inline parameters.

In src/python/pythonHelper.ts#L12, f-strings are used to format strings. https://github.com/RobertOstermann/vscode-inline-parameters/blob/44fae04770b30327e9d950f01a352135d8533eed/src/python/pythonHelper.ts#L12

However, this feature was introduced in Python 3.6. If the default Python configured in the environment is Python2 or <3.6, the Python features of this extension will not work properly. Specifically, it will have the following error,

[2022-04-11 20:41:00.818] [exthost] [error] Error: Command failed: python /home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.1/out/src/python/helpers/main.py /home/luis/test/test.py
  File "/home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.1/out/src/python/helpers/main.py", line 17
    expression_line = f"expression line: {node.func.lineno}"

As a solution for this, I think you should choose the appropriate Python version in the program (because most computers have both Python3 and Python2 installed).

One of the simplest solutions is to specify the use of Python3 directly in src/python/pythonHelper.ts#L12, but this still cannot ensure that Python>=3.6.

const command = `python ${context.extensionPath}/out/src/python/helpers/main.py ${fsPath}`; // Before
const command = `python3 ${context.extensionPath}/out/src/python/helpers/main.py ${fsPath}`; // After
RobertOstermann commented 2 years ago

@l1997i I do not work in Python much and did not realize that using the f-strings would only work with versions of Python >= 3.6. Thanks for making me aware of that! I have added a fix in commit ffbc5a0, which hopefully solves the problem by using string concatenation with the + operator instead of using f-strings.

I have also added a inline-parameters.python.executablePath setting that allows you to change that command to use python3 instead of the default python.

I have published version 1.1.2. please try that version and let me know if the program now works with Python2 or Python3 versions earlier than 3.6

l1997i commented 2 years ago

Still doesn't work. I've set inline-parameters.python.executablePath to python3, but the output says it still runs on Python2.7, which will cause 'Num' object has no attribute 'end_lineno' error.

Traceback (most recent call last):
  File "/home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.2/out/src/python/helpers/main.py", line 39, in <module>
    main()
  File "/home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.2/out/src/python/helpers/main.py", line 10, in main
    analyzer.visit(tree)
  File "/usr/lib/python2.7/ast.py", line 241, in visit
    return visitor(node)
  File "/usr/lib/python2.7/ast.py", line 249, in generic_visit
    self.visit(item)
  File "/usr/lib/python2.7/ast.py", line 241, in visit
    return visitor(node)
  File "/usr/lib/python2.7/ast.py", line 251, in generic_visit
    self.visit(value)
  File "/usr/lib/python2.7/ast.py", line 241, in visit
    return visitor(node)
  File "/home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.2/out/src/python/helpers/main.py", line 21, in visit_Call
    argument_end_line = "argument end line: " + str(argument.end_lineno)
AttributeError: 'Num' object has no attribute 'end_lineno'

By the way, I also manually run the command using Python3,

python3 /home/luis/.vscode-server/extensions/robertostermann.inline-parameters-extended-1.1.2/out/src/python/helpers/main.py /home/luis/test/test.py

it works,

expression line: 4 | expression character: 0 | argument start line: 4 | argument start character: 4 | argument end line: 4 | argument end character: 5
expression line: 4 | expression character: 0 | argument start line: 4 | argument start character: 7 | argument end line: 4 | argument end character: 8
NEW EXPRESSION
l1997i commented 2 years ago

I've fix this and make a pull request https://github.com/RobertOstermann/vscode-inline-parameters/pull/5.

RobertOstermann commented 2 years ago

@l1997i Did you reload your VSCode window after changing the Python path environment variable? I think it should work after reload and I can add a popup to let users know that.

l1997i commented 2 years ago

@l1997i Did you reload your VSCode window after changing the Python path environment variable? I think it should work after reload and I can add a popup to let users know that.

@RobertOstermann Yes, I have. I even reopen the VS Code.

RobertOstermann commented 2 years ago

Ok, I have published version 1.1.3 with changes from 4daccfc that should allow Python 2.7 to work with this extension. I also added the Python command that is being run in the inline-parameters-extended output tab. That might show that python3 is being run correctly? If not I am not sure why it is not respecting the setting, but I looked at your PR #5 and like the idea of using the ms-python extension setting for the python path. I will get that merged and push out a new version with those changes.

RobertOstermann commented 2 years ago

@l1997i I have merged your changes and slightly altered them in 078dbe7 to allow for the configured path to take priority when it is set. I published version 1.1.4 with those changes. Let me know if that fixes all the bugs you were seeing.

l1997i commented 2 years ago

All bug fixed! Thanks so much!