microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.25k stars 1.15k forks source link

Python longstring tag vs block comment tag in Vscode #23703

Open Apamobil opened 3 weeks ago

Apamobil commented 3 weeks ago

Does this issue occur when all extensions are disabled?: Yes

DESCRIPTION

Vscode Language Configuration includes either line comment tag and or block comment tags. The extension we have been developing uses by default (based on implementation decision) block comments if they exist for the programming/script language of the current file.

The problem is with Python block comments and Vscode. Vscode misleadingly returns longstring tag ''' as a block comment tag that must be used in the beginning and end of a multi-line comment. However, according to the Python language specification the longstring tag is NOT for comments, but for long strings (as its name states).

From Python language reference:

2.1.3. Comments 2.4.1. String and Bytes literals

It seems this is a bug in Vscode implementation. It should be corrected in a way that so that language configuration does not contain longer longstring tag (''') as a block comment tag for Python comment.

VSCodeTriageBot commented 3 weeks ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.90.0. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

Apamobil commented 3 weeks ago

It's not yet available due to the company policy.

luabud commented 2 weeks ago

Hi @Apamobil, I am struggling to reproduce any wrong behaviour in VS Code with longstrings defined by ''' or with comment blocks, with the Pylance extension disabled or enabled. Would you mind explaining a bit better what is the behaviour you are experiencing?

Apamobil commented 2 weeks ago

Hi! In a nutshell:

Vscode misleadingly returns longstring tag ''' as a block comment tag for Python.

According to the Python language specification longstring tag is not intended for comments, but for long strings as the name states. This is often used as an alternative method for commenting out multiple lines (using triple-quoted string literals (''' ''' or """ """). However, they are not officially block comments for Python.

The problem is that automated formatters and linters don't like the longstring tags and are sometimes formatting the comments marked with longstring tags.

See also this article, it gives some background info about commenting in Python: https://www.datacamp.com/tutorial/python-block-comment

luabud commented 1 week ago

@Apamobil would you mind explaining what you mean by "VS Code returns a longstring tag as a block comment tag"?

The reason why I'm not following what's happening is that the rendering of comments blocks with # and ''' or """ are treated differently in VS Code. In the screenshot below, the comment blocks are grayed out, while the string blocks (whether in a string assignment or not) are displayed with a different color:

Image

But perhaps the problem isn't with VS Code itself but with a linter or formatter extension you're using?

Apamobil commented 1 week ago

I'm also a bit on a thin ice here myself since the colleague who reported this behavior is on a leave and I wrote this ticket on behalf of him.

I think the problem is that Vscode LanguageConfiguration includes either line comment tag and or block comment tags. The extension in question uses by default block comments. The block comments for python are implemented as ''' in Vscode and it is against the python language reference. And this causes problems with linters/formatters.

Does this make it any more clear? :)