Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all color themes: treat comment docstrings as comments too #182162

Conversation

ElectricRCAircraftGuy
Copy link
Contributor

@ElectricRCAircraftGuy ElectricRCAircraftGuy commented May 11, 2023

This treats Python comment block docstrings as comments too, so they are subdued, like in Sublime Text, so we can focus on the code.

Before this change (bad, because the Python block comment strings are an obnoxious yellow):
image

After (good, because now they are treated like comments, and subdued):
image

Notice that regular multi-line block strings (str in the code above) still show up in yellow, as they should.

I wrote about this here: How to subdue Python block comment strings in VSCode in order to look like Sublime Text's Monokai

Fixes #182163

@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented May 11, 2023

@aeschli
Copy link
Contributor

aeschli commented May 11, 2023

It makes sense to render docstrings as comments. Would you mind adding that rule to the other built-in themes as well?

@ElectricRCAircraftGuy ElectricRCAircraftGuy force-pushed the gstaples_fix_python_comment_strings branch from 776545b to 44c2f52 Compare May 11, 2023 16:18
@ElectricRCAircraftGuy
Copy link
Contributor Author

@microsoft-github-policy-service agree

@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented May 11, 2023

It makes sense to render docstrings as comments. Would you mind adding that rule to the other built-in themes as well?

@aeschli , done. I'm grateful you received this PR positively.

@aeschli
Copy link
Contributor

aeschli commented May 12, 2023

Thanks!
I would suggest to include all doc strings, including the single line and raw doc strings.
The rule would be string.quoted.docstring, not limited to python, assuming that other languages using that scope use it for documentation as well (I didn't find other grammars using it)

ElectricRCAircraftGuy added a commit to ElectricRCAircraftGuy/GS_vs_code_custom_theme that referenced this pull request May 12, 2023
...`"string.quoted.docstring"`, instead of the more-narrow, Python-only
   `"string.quoted.docstring.multi.python"`, per the recommendation in
   this comment here:

   microsoft/vscode#182162 (comment)
@ElectricRCAircraftGuy ElectricRCAircraftGuy changed the title monokai-color-theme.json: treat Python comment string blocks as comments too all color themes: treat comment docstrings as comments too May 12, 2023
ElectricRCAircraftGuy added a commit to ElectricRCAircraftGuy/vscode that referenced this pull request May 12, 2023
...instead of "string.quoted.docstring.multi.python", per
@aeschli's request here:
microsoft#182162 (comment)

This way the comment formatting applies to *all* language quoted
docstrings, instead of just to Python.
	modified:   extensions/theme-abyss/themes/abyss-color-theme.json
	modified:   extensions/theme-defaults/themes/dark_vs.json
	modified:   extensions/theme-defaults/themes/hc_black.json
	modified:   extensions/theme-defaults/themes/hc_light.json
	modified:   extensions/theme-defaults/themes/light_vs.json
	modified:   extensions/theme-kimbie-dark/themes/kimbie-dark-color-theme.json
	modified:   extensions/theme-monokai-dimmed/themes/dimmed-monokai-color-theme.json
	modified:   extensions/theme-monokai/themes/monokai-color-theme.json
	modified:   extensions/theme-quietlight/themes/quietlight-color-theme.json
	modified:   extensions/theme-red/themes/Red-color-theme.json
	modified:   extensions/theme-solarized-dark/themes/solarized-dark-color-theme.json
	modified:   extensions/theme-solarized-light/themes/solarized-light-color-theme.json
	modified:   extensions/theme-tomorrow-night-blue/themes/tomorrow-night-blue-color-theme.json
...instead of "string.quoted.docstring.multi.python", per
@aeschli's request here:
microsoft#182162 (comment)

This way the comment formatting applies to *all* language quoted
docstrings, instead of just to Python.
@ElectricRCAircraftGuy ElectricRCAircraftGuy force-pushed the gstaples_fix_python_comment_strings branch from 1ab46a0 to f43e8a6 Compare May 12, 2023 17:08
@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented May 12, 2023

Thanks! I would suggest to include all doc strings, including the single line and raw doc strings. The rule would be string.quoted.docstring, not limited to python, assuming that other languages using that scope use it for documentation as well (I didn't find other grammars using it)

@aeschli , done. I hope you'll approve it now.

I also tested it on the Python code and it did still work right.

@aeschli aeschli added this to the May 2023 milestone May 15, 2023
@aeschli aeschli enabled auto-merge (squash) May 15, 2023 12:25
@aeschli aeschli merged commit b436869 into microsoft:main May 15, 2023
6 checks passed
@k98kurz
Copy link

k98kurz commented Jun 9, 2023

How do I reverse this locally? I preferred it the way it was. Sudden and unexplained changes after a restart are far more obnoxious than the orange color -- at least it looked good, which I can't say for this ugly dark green.

Edit 2: finally figured out a solution that will not simply revert when the dark_vs.json file resets. Edit the settings.json file and add the following:

    "editor.tokenColorCustomizations": {
        "[*]": {
            "textMateRules": [
                {
                    "name": "Docstrings",
                    "scope": "string.quoted.docstring",
                    "settings": {
                        "foreground": "#CE9178"
                    }
                }
            ]
        }
    }

Had to use an asterisk because the theme names are aliased in some obscure fashion, and those aliases are not accessible via suggestions, so this will affect all themes. Putting in the "Visual Studio Dark" alias for "Dark (Visual Studio)" changes only that theme, not the ones that inherit from it, and I could not figure out what the alias for "Dark Modern" is. (Why the theme names cannot be used in the settings and must be replaced with some obscure, seemingly undocumented alias is beyond me.)

Edit: I figured it out. The color is coming from the dark_vs.json theme file, whether or not you have that theme selected. The problem was that in the "tokenColors" array was the following object (outdated):
        {
            "name": "Comments",
            "scope": ["comment", "string.quoted.docstring"],
            "settings": { "foreground": "#6a9955" }
        },

To restore all dark themes back to the way they were a couple hours ago, I replaced it with the following:

        {
            "name": "Docstrings",
            "scope": ["string.quoted.docstring"],
            "settings": { "foreground": "#ce9178" }
        },
        {
            "name": "Comments",
            "scope": ["comment"],
            "settings": { "foreground": "#6a9955" }
        },

This change requires a restart to take effect. Leaving this here for anyone else who got blindsided by this and wanted to revert it.

@starball5
Copy link

starball5 commented Jun 9, 2023

Related on Stack Overflow:

Help with closing the duplicates would be appreciated so that the answers all get put in one place for future readers.

@torext
Copy link

torext commented Jun 9, 2023

Very much agreed that sudden and unexplained changes like this, which ultimately boil down to purely someone's personal preference, are quite obnoxious. While the change makes conceptually sense, it would be better in future to maintain current behaviour while enacting the change.

@ElectricRCAircraftGuy ElectricRCAircraftGuy deleted the gstaples_fix_python_comment_strings branch June 9, 2023 12:47
@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented Jun 9, 2023

Very much agreed that sudden and unexplained changes like this, which ultimately boil down to purely someone's personal preference, are quite obnoxious. While the change makes conceptually sense, it would be better in future to maintain current behaviour while enacting the change.

I'm not with VSCode, but I'm wondering: how does one maintain current behavior while enacting the change? They seem so me to be mutually exclusive events. (Literally, if you have a way how, please explain how. I don't see how this request is possible without adding a menu setting to "enable correct syntax highlighting", which isn't practical).

Part of the pain is that the syntax highlighting schemes may need to be updated once the change happens, too, but it's not practical to expect that to happen at the same time, nor does it make sense to update all schemes to undo the very behavior this change makes. I think it's just an unfortunate side effect of having wrong behavior that some people will prefer the wrong behavior to continue because they got used to it.

@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented Jun 9, 2023

To help people go back:

How to colorize Python docstrings to your liking; ex: like strings <-- click here to see how to go back to the old settings


For anyone wishing to customize your theme to go back to how it was for your syntax highlighter, read my answer here to learn how to do that. It contains info about finding the names and colors of sections you like and how to change the ones you don't:

How to subdue Python block comment strings in VSCode in order to look like Sublime Text's Monokai

In my link above, I explain how to use the token and scope inspector, and then I say:

Use this technique to customize other scopes and colors to your liking, or to see what colors and formatting other scopes currently have if you want to copy them.

@starball5 also pointed out this Q&A: Restarted Visual Studio Code, and now Python docstrings are an ugly dark green. How do I return it to the original orange?, and has posted a great answer to it here.

@torext
Copy link

torext commented Jun 9, 2023

I'm not with VSCode, but I'm wondering: how does one maintain current behavior while enacting the change?

In this particular case it should've been doable to automatically generate a diff between the current user's theme settings and what the result of this change would be and to store the diff as explained in the other posts in the user's settings. I.e. automate pretty much what we now all did manually. I'm pretty sure I've seen updates to VSCode do this before...

I'm not saying that's necessarily easy or that I'd even know how to do it, but personally I wouldn't even attempt such a change without the above prophylaxis.

I think it's just an unfortunate side effect of having wrong behavior that some people will prefer the wrong behavior to continue because they got used to it.

That's the key observation however: people got used to it. Ultimately VSCode is just a tool and comfort in using it supersedes any abstract desire for "conceptually correct behaviour". I'm usually all for the latter, but sudden changes to something that's ultimately anyways customizable and user-specific is just a no-go for me.

@lkct
Copy link
Contributor

lkct commented Jun 10, 2023

It makes sense to render docstrings as comments.

I think it's just an unfortunate side effect of having wrong behavior that some people will prefer the wrong behavior to continue because they got used to it.

@ElectricRCAircraftGuy @aeschli
Sorry, but I even didn't get the point from the very beginning: why should we treat docstrings as comments?

I use docstrings as docstrings, containing information, e.g., for functions/methods, the summary of functionality and signature. These should be more important than just a "comment" and be highlighted differently.
Why is this "wrong behavior"?

@lkct
Copy link
Contributor

lkct commented Jun 12, 2023

Wait, so this got implemented, because dude was using fucked up theme?

However, the problem is no one commented with an objection before it was merged.

@lobsterkatie
Copy link

lobsterkatie commented Jun 12, 2023

FWIW, I saw the title of this PR in the release notes and thought, "Finally!" I am very much +1 on this change.

I understand that semantically, docstrings and comments aren't the same thing, but IMHO they are used for enough of the same purpose (documentation ignored by the interpreter*) that they should be lumped together, and should both be dimmed, since they don't affect the behavior of the function/method/module/whatever. Conversely, docstrings, while they are not comments, are also not strings in the normal sense, and therefore should not be colored the same way.

*I'm leaving aside the issue of doctests, which of course the interpreter does handle, but AFAIK they're not colored differently than any other part of a docstring, so for the purposes of this conversation I'm lumping them in with non-runnable docstring content.

@k98kurz
Copy link

k98kurz commented Jun 12, 2023

I understand that semantically, docstrings and comments aren't the same thing, but IMHO they are used for enough of the same purpose (documentation ignored by the interpreter*) that they should be lumped together, and should both be dimmed, since they don't affect the behavior of the function/method/module/whatever. Conversely, docstrings, while they are not comments, are also not strings in the normal sense, and therefore should not be colored the same way.

The interpreter does not actually ignore them in Python -- it puts them on a special __doc__ attribute to be used by the built-in help function. Perhaps there is an argument that they should be handled as their own special case, but which wrong behavior (i.e. whether they should be incorrectly handled as strings or incorrectly handled as block comment) is a matter of personal preference.

@lobsterkatie
Copy link

lobsterkatie commented Jun 12, 2023

The interpreter does not actually ignore them in Python -- it puts them on a special doc attribute to be used by the built-in help function.

Okay, yes, fair enough, but for the general running of whatever code I'm writing, they function much more like comments than like values, i.e., the 99% case is that my code doesn't involve __doc__ or help, and therefore their value has no effect on the behavior of my code and I can safely ignore them when reading through code and trying to figure out what it does.

@lkct
Copy link
Contributor

lkct commented Jun 12, 2023

and I can safely ignore them when reading through code and trying to figure out what it does.

Seems we have different opinions on docstrings. I think docstrings exist to help us figure out what it does without reading through code (especially when we just want to use a function/class etc. and don't care about the internals).

@Afrozodiacc
Copy link

I understand that semantically, docstrings and comments aren't the same thing, but IMHO they are used for enough of the same purpose (documentation ignored by the interpreter*) that they should be lumped together, and should both be dimmed, since they don't affect the behavior of the function/method/module/whatever. Conversely, docstrings, while they are not comments, are also not strings in the normal sense, and therefore should not be colored the same way.

The interpreter does not actually ignore them in Python -- it puts them on a special __doc__ attribute to be used by the built-in help function. Perhaps there is an argument that they should be handled as their own special case, but which wrong behavior (i.e. whether they should be incorrectly handled as strings or incorrectly handled as block comment) is a matter of personal preference.

Agreed. docstrings are documentation and not comments. There are python projects such as sphinx which automatically generate documents using docstrings. docstrings are not equivelant to comments hence they should not be coloured equivelantly. An option to collapse docstrings in VSCode much like you can collapse classes or methods may be more suitable than colouring them the same as comments for those which want to "declutter" the code space. VSCode also has special treatment of python docstrings already:

image

If a project is using docstrings as a place for comments, then I'd suggest the developers use comments for comments and docstrings for documentation.

@alexsaurber
Copy link

This PR was not the way to go about "fixing" this.

If the convention for docstrings was to treat them like comments and not documentation that is actually functionally accessible text, then we would use comment notation. Even PEP8 recommends multiline comments to begin with # on every line. These should have been read before changing the highlighting convention of millions of users to a new incorrect format. PEP8: Block Comments. PEP257: Docstring Conventions. Strings and docstrings have the same syntactic highlighting in IDLE. Call me silly but that seems pretty intentional. Additionally, they're called Docstrings, not doccomments. Why correct an error with an even worse error?

@lobsterkatie
Copy link

I feel like we're missing the forest for the trees here.

We all agree that string literals, docstrings, and comments are each their own thing, with different (albeit often overlapping) uses, syntax, integration with tooling, etc.

One option is to make all three different tokens (and therefore potentially all three different colors), as proposed in #184836. Another option is to lump docstrings in with either string literals (as has been the case up 'til now) or comments (as is the case after this change).

The implicit point of this PR, which I'm also arguing for, is that in the end, docstrings are documentation. They're English sentences. They're explanations. Changing them doesn't change how a function works, or the result it gives. This makes them fundamentally different than literal values (string or otherwise), but very much akin to comments. (This is why in JS they're grey, just like comments, and always have been.)

Bottom line, in the same way that I want to be able to tune out comments, I want to be able to tune out docstrings, too. I want them both to be there, each serving their purpose, available if I want them, but neither one grabbing my attention. And the most straightforward way to do that is to dim them both, which is what this PR does.

@torext
Copy link

torext commented Jun 13, 2023

I feel like we're missing the forest for the trees here.

I feel like you're just reiterating your personal preference?

The pure facts are that two basic mistakes are being or have been committed here:

  1. Any proposed change, "conceptually correct" or not, should leave each user's current IDE behaviour/look, all else being equal, unchanged on update. Even right now, after the change under discussion, docstrings remain a separately colorable option and so it was easy to revert the local behaviour. This however should've been done automatically by the PR itself, so as to not suddenly have a different looking IDE without explanation whatsoever.
  2. As pointed out many times above and again more clearly in Python docstrings mistakenly rendered as comment #184836, the current PR actually does not implement the "conceptually correct" behaviour. Docstrings are not comments, the former are programmatically accessible, the latter aren't, and that is that.

Each one of us, both before the PR under discussion and after, can set things up the way they like: making docstrings look like comments or not. But PR's like this one should not change the look of a user's IDE on update and ideally should implement "conceptually correct" behaviour once and for all.

P.S.: I'm assuming above that docstrings were separately configurable before this PR. If this was not the case, then this PR has some merit on that front. Either way, would be much better if it implemented conceptually sound behaviour to begin with as apposed to an approximation.

@PeterJCLaw
Copy link

PeterJCLaw commented Jun 13, 2023

However, the problem is no one commented with an objection before it was merged.

Kinda? This PR was open for four days before it was merged (May 11th until 15th). That didn't leave a lot of time for people to comment on a rather substantial change to the UX. I also can't see any attempt to gather input from the Python community, either in general or even the developers of the vscode-python or related extensions. The issue which the PR was marked as fixing was opened after the PR was, implying that no prior requests for this change existed (a quick skim of the issues on this repo seems to agree).

An issue (microsoft/pylance-release#4429) was raised on the 22nd May reporting this as a bug in the insiders build of VSCode, though unfortunately it was combined with a more general syntax highlighting issue and misattributed to pylance rather than VSCode itself. The discussion there suggests that even internal Microsoft folks were unaware of this change!

I'm assuming above that docstrings were separately configurable before this PR.

Looking at the diff of this PR and the workarounds which people are applying to fix this issue, customising this per-user seems to have already been possible. This change only flips the default so that VSCode itself now treats some Python strings as comments.


A conflating factor here is that the parser seems to be imprecise in what it considers docstrings, at least relative to what the interpreter will actually attach to a __doc__:

def foo():
    "docstring" # ends up in `foo.__doc__`, now considered a comment
    print(2)
    "not a docstring" # but still now considered a comment
    return

Personally I would ask the VSCode team to:

@lkct
Copy link
Contributor

lkct commented Jun 13, 2023

But PR's like this one should not change the look of a user's IDE on update and ideally should implement "conceptually correct" behaviour once and for all.

Agreed. That's the point I've been making: we should make things "conceptually correct"; if we can't (for the moment), we should choose the way that minimizes UX impact.

P.S.: I'm assuming above that docstrings were separately configurable before this PR.

Yes, they are, just like the workaround (see SO links above). However, all "literal string statements" currently share the token for "docstrings", and we can only render them as either docstrings or comments.
Note: docstrings do not necessarily look like strings (in an expression); it's just a choice by the themes. As @alexsaurber pointed out, "Strings and docstrings have the same syntactic highlighting in IDLE."


Kinda? This PR was open for four days before it was merged (May 11th until 15th). That didn't leave a lot of time for people to comment on a rather substantial change to the UX.

I don't know how many people are the same, but for me, I actually only looked into this repo after running into the current problem. However, a longer time may still help more people come across this PR.

I also can't see any attempt to gather input from the Python community, either in general or even the developers of the vscode-python or related extensions.

This is indeed a problem. But it could be because this is treated as a bugfix instead of a feature change.

The issue which the PR was marked as fixing was opened after the PR was, implying that no prior requests for this change existed

Actually, I sometimes do the similar thing if there's a quick bugfix. I think the core problem here is whether this is a bugfix (which only improves things) or a feature change (which may have a negative impact, at least on part of the users). It's better to gather community inputs before a feature change, while bugfixes may be merged through a faster track. Around this issue, the author thinks this is a bugfix, but I prefer to take it as a feature change. However, the maintainers should rectify how to categorize.

@lkct
Copy link
Contributor

lkct commented Jun 13, 2023

A conflating factor here is that the parser seems to be imprecise in what it considers docstrings, at least relative to what the interpreter will actually attach to a __doc__:

I have summarized the behavior in #184836, Python itself has definitions, and the python language server (pylance) has its own interpretation. Therefore I'm proposing to add new tokens and let the language server decide how they're assigned to every string.

For your convenience, I paste the relevant information here:

Python Docs:

docstring
A string literal which appears as the first expression in a class, function or module. While ignored when the suite is executed, it is recognized by the compiler and put into the doc attribute of the enclosing class, function or module. Since it is available via introspection, it is the canonical place for documentation of the object.

That is, only the first statement in a scope can be a "docstring".

Python extension of vscode:

class A:
    """This is the docstring for A. Shown on hover."""

    B = 1
    """Shown on hover for B."""

    def c(self, x:int)->int:
        """This is the docstring for c. Shown on hover."""
        d = 1
        """This is nothing but a comment."""

e = 1
"""Shown on hover for e."""

For B and e the strings are not docstrings, but pylance treats them "docstring-like".


Slightly off-topic

The discussion there suggests that even internal Microsoft folks were unaware of this change!

MS is quite large. I think it's normal that the left hand is unaware of what the right hand is doing.

@lobsterkatie
Copy link

I feel like we're missing the forest for the trees here.

I feel like you're just reiterating your personal preference?

...

Docstrings are not comments, the former are programmatically accessible, the latter aren't, and that is that.

Here's what I meant:

Trees: Docstrings and comments are not the same thing, for x, y, z technical reasons. (I think we all agree here.)
Forest: Docstrings are documentation. Comments are also documentation. (I actually think we agree here, too.)

Other than the questions around process (which are valid, but separate), the only part that's opinion/preference is which one of those two is most important. And on that topic I feel like we've all expressed our opinions here both thoroughly and cogently, so at this point I will leave this in the hands of the maintainers.

@Afrozodiacc
Copy link

I feel like we're missing the forest for the trees here.

I feel like you're just reiterating your personal preference?
...
Docstrings are not comments, the former are programmatically accessible, the latter aren't, and that is that.

Here's what I meant:

Trees: Docstrings and comments are not the same thing, for x, y, z technical reasons. (I think we all agree here.) Forest: Docstrings are documentation. Comments are also documentation. (I actually think we agree here, too.)

Other than the questions around process (which are valid, but separate), the only part that's opinion/preference is which one of those two is most important. And on that topic I feel like we've all expressed our opinions here both thoroughly and cogently, so at this point I will leave this in the hands of the maintainers.

I'd swap the forest and trees in your example. Ultimately the editor needs to respect the python language and the facts are that:

  • strings, string literal and docstrings are objects and that docstrings are special attributes of other objects
  • comments are not python objects

The fact that docstrings are documentation is true, however docstrings are customer facing documentation which are python objects whereas code comments are internally facing (and best practice is that comments are not python objects such as string) - hence the importance of being able to distinguish them from each other.

I share the opinion of others in this thread that this PR should be reverted. I also think that this PR does not respect what is most significant for the python color themes in VSCode: distinguishing the different features of the python language to the developer.

@Afrozodiacc
Copy link

Afrozodiacc commented Jun 13, 2023

Forest: Docstrings are documentation. Comments are also documentation. (I actually think we agree here, too.)

Testing the limits of this statement shows why this argument doesn't hold water for formatting things which are of documentation in nature as the same colour...

Trivial code:

class IceCream:
    """Yummy ice cream!"""
    def __init__(self, flavour):
        # Ice cream is typically served cold
        self.temperature_deg_c = -15
        # Support any flavour
        self.flavour = flavour

class IceCreamMachine(Machine):
    """An ice cream machine which dispenses delicious ice cream!"""
    def dispense(self, n, flavour):
        """Dispenses n number of icecreams in the requested flavour.

        Parameters
        ----------
        n: int
            Number of ice creams to dispense.
        flavour: str
            Any flavour you like so long as it is string.

        Returns
        -------
        list of IceCream
            IceCream class instances
        """
        # Catch any non-str flavour
        if flavour is not str:
            raise TypeError("Flavour must be str.")
        # Create n instances of requested flavour
        icecreams = [IceCream(flavour) for i in range(n)]
        return icecreams

Now testing the upper limit of how we can "comment" our code:

class IceCream:
    """Yummy ice cream!"""
    def __init__(self, flavour):
        comment = "Ice_cream_is_typically_served_cold"
        self.temperature_deg_c = -15
        Support_any_flavour = "comment"
        self.flavour = flavour

class IceCreamMachine(Machine):
    """An ice cream machine which dispenses delicious ice cream!"""
    def dispense(self, n, flavour):
        """Dispenses n number of icecreams in the requested flavour.

        Parameters
        ----------
        n: int
            Number of ice creams to dispense.
        flavour: str
            Any flavour you like so long as it is string.

        Returns
        -------
        list of IceCream
            IceCream class instances
        """
        Catch_any_non_str_flavour = "comment"
        if flavour is not str:
            raise TypeError("Flavour must be str.")
        "Create n instances of requested flavour"
        icecreams = [IceCream(flavour) for i in range(n)]
        return icecreams

Comment: Ok now we are using variables to comment our code, it's not python best practice but it is effectively documenting what the code is doing. Those variable assignments which are documentation should now be color formatted the same as code comments. Using variable assignments this way creates an object as documentation much the same as using unassigned str to document does.

Now testing the lower limit of how we can document our code:

class IceCream:
    def __init__(self, flavour):
        self.temperature_deg_c = -15
        self.flavour = flavour

class IceCreamMachine(Machine):
    def dispense(self, n, flavour):
        if flavour is not str:
            raise TypeError("Flavour must be str.")
        icecreams = [IceCream(flavour) for i in range(n)]
        return icecreams

Comment: In this case without the use of docstrings, unassigned str, comments or "comment" variable assignments we have sufficiently (though minimally) documented code through careful selection of class, attribute, method names and variable assignments. Python code is often written in ways where it ends up documenting itself. Should this also be formatted the same colour as code comments?

@Afrozodiacc
Copy link

Afrozodiacc commented Jun 14, 2023

Forest: Docstrings are documentation. Comments are also documentation. (I actually think we agree here, too.)

Testing the limits of this statement shows why this argument doesn't hold water for formatting things which are of documentation in nature as the same colour...

To summarise this demonstration shows that:

  • Docstrings are documentation
  • Comments are typically used as documentation
    • Note: sometimes they are used to prevent lines of code from being executed and not for documentation.
  • Variables and unassigned str can be used as documentation
  • Code itself can be used as documentation

Hence, we should not colour docstring/comment/variables with our inference of intent (such as documentation) but color them by what they are as features of the python language (otherwise everything will be coloured the same).

@minhaics
Copy link

Dear @ElectricRCAircraftGuy, could you please revert back to the old color scheme where docstrings had a distinct color for easier readability. This is beneficial and user-friendly for programmers' eyes. It's hard to believe that such a terrible request was approved, causing this disruption.

@ElectricRCAircraftGuy
Copy link
Contributor Author

@minhaics ,

Reminder, here is how to fix the colors for yourself:

  1. Restarted Visual Studio Code, and now Python docstrings are colored like comments. How do I return it to the string color?
    1. Answer by @starball5
    2. My own answer: How to colorize Python docstrings to your liking; ex: like strings

If you do this change and then click the little person icon in the bottom-left of VSCode, and sign in with GitHub (or one of the other sign-in options) and turn on sync, it will apply this change to all instances of VSCode you have, on all computers you have.

@minhaics
Copy link

minhaics commented Jun 24, 2023

hi @ElectricRCAircraftGuy
I think that having the color of the docstring the same as the comment in green is your personal preference. Personally, I find green harder to read and distinguish than orange. Moreover, programmers often pay more attention to and read docstrings more than comments. Therefore, it is not reasonable to have the docstring in the same green color as the comment. You should restore it to what it was before. If you like, you can customize it in the settings section. It's not appropriate to make everyone follow your preference. Since you introduced the setting, there have been many objections, as it is not reasonable to require everyone to go into settings to make adjustments. I sincerely hope that you will restore it to what it used to be! I like the previous orange color scheme, but I tried to find the specific code for it and couldn't. I don't know what it was

@minhaics
Copy link

minhaics commented Jun 24, 2023

dear @ElectricRCAircraftGuy
Python itself is unlike other languages, it is a language that doesn't use curly braces {}. Therefore, it can be quite challenging to identify the beginning of a function based solely on the "def func" declaration. This is insufficient for the programmer's eyes. That's where the presence of a docstring comes to the rescue. I find it very helpful to easily identify the existence of a function by adding a docstring to it. Hence, I frequently look at the docstring. A docstring that has a beautiful and distinct color from the green of comments enhances the recognition of the start of a function for the programmer's eyes.

@ElectricRCAircraftGuy
Copy link
Contributor Author

ElectricRCAircraftGuy commented Jun 24, 2023

but I tried to find the specific code for it and couldn't. I don't know what it was

@minhaics , what color scheme do you have? I'll give you the code.

The color you had is the color of a string. Open a Python file and type s = "string", then use the developer inspector, as I explain, to inspect that string and get its color code. If you give me the name of your color scheme I'll give it to you.

@scholer
Copy link

scholer commented Jun 28, 2023

This PR is based on the wrong assumption that in Python, tripple-quote strings that are not assigned to a variable are "block comments". This is not correct. It is just a string that is not assigned. Python doesn't have any true options for multi-line comments, like /* ... */ in C.

True, some people highjack tripple-quotes to quickly comment out blocks of code, but that is super flaky. For instance if the code you want to comment out already contains tripple-quoted strings. Or if used inside brackets (where python will concatenate strings if they are only separated by newlines and whitespace). The proper way to comment out parts of code in Python is to highlight the lines you want to comment out and ask your IDE to comment the selected lines (ctrl + / in vscode).

The """SAMPLE OUTPUT""" block-comment example in the original post could be just as easily be written using normal # SAMPLE OUTPUT: So, if you "don't like the yellow color" that your IDE is using for strings to apply to your tripple-quoted "comments", just do that—use # for those comments. In fact, you could argue that retaining the original behavior is also favorable even in these instances, since it gives people the option to highlight certain "comments". So, if I wanted to actually highlight the "SAMPLE OUTPUT" comment, I can either use a tripple-quoted string and make it stand out, or I can use # to make it a bit more muted.

Other questions:

  • Do you also want to color other expressions that are not assigned to a variable as comments? Like 2 + 2?
  • If unassigned tripple-quoted strings are treated as block comments, what about tripple-quoted f-strings? Why should """HELLO""" be colored as a comment, but an unassigned f""""HELLO"""" be colored as a string? (The answer is obvious, since an unassigned f-string can invoke functions. But symmetry says that they should appear similarly in the editor.)
  • What about "notebook-like" files, where many notebook applications (e.g. Jupyter) will output the last statement in a cell, if it is not assigned to a variable, including tripple-quoted strings. Since some notebook applications are just using plain .py files as notebooks (e.g. Databricks), the editor can't really know which tripple-quoted strings are at the end of a cells and which are not.
  • Have we thought about the teaching impact it will have to show unassigned strings as comments? People new to Python could get the impression that it is an official part of the language. Would it not be better, didactically, to represent strings as strings?

All in all, I understand the rationale for this PR, but it just seems like it can have too many unexpected/unintended side-effects or edge-cases. Which is what can easily happen when you change the interpretation of basic parts of a language.

And, as everyone else have already pointed out: Docstrings, in particular, are certainly not comments, since they affect the output being produced by the interpreter. They should probably have their own color-theme option in the editor. And if there is a desire to have other unassigned strings appear different from normal strings, then there should maybe be a separate token for unassigned strings (that are not docstrings). And then let the user decide which color each of those should be. If you want your docstrings to be colored the same color as comments, you can pick a theme that does that.

P.S: Regarding the best way to implement changes like these, that mostly comes down to personal preference: There is a reason that this "small change" has received such a major blowback. Whenever you change important parts of the UI, such as syntax highlighting, there is a mental overhead to users to adapt to the new change. So you should be really, really careful about applying changes to the UI, especially when the change is mostly a matter of personal preference. If you want to avoid drama, the best approach is generally to first make it possible to apply the change as a user setting (e.g. by changing the theme). And then, after some time, see if a majority of users prefer the new change, and then maybe consider making that the new default. In this case, we should have first added support for styling docstrings and unassigned strings separately from normal strings. Then created some new themes that would do just that, maybe one that would color docstrings the same color as comments, and another theme that would color docstrings differently from either comments and normal strings. And then see if those new themes suddenly become more popular. And if they do, consider changing the default theme to one of these, but leaving the old theme in place for people who prefer the old theme.

@minhaics
Copy link

Hi @ElectricRCAircraftGuy
Please restore it as it originally belonged before, so users don't have to manually customize it in the settings in the .json file

@minhaics
Copy link

Hi @ElectricRCAircraftGuy
You said, "The color you had is the color of a string. Open a Python file and type s = "string", then use the developer inspector, as I explain, to inspect that string and get its color code. If you give me the name of your color scheme, I'll give it to you." These manual steps are inconvenient for users, even in VS Code, I don't have a default tool to find the color code of the displayed string. I rely on and trust the VS Code core team for what they have done for Python. I prefer the default settings without additional customization. However, you have ruined it, so once again, please restore it to its original state, as it used to be. Thanks!

@github-actions github-actions bot locked and limited conversation to collaborators Jul 2, 2023
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.

Python multi-line comment strings should be treated as comments in all themes