You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am not sure where to post this, as this is not necessarily a bug. But I find this behavior strange, and I was unable to find resource anywhere, and unable to override the setting myself.
Typically, any constant variable defined in ALL UPPERCASE, (with or without underscore), are treated as constant, and is recognized by semantic tokenizer as readonly constant.
In vscode, we also can see that such constant also colored in different color than regular read-write variables.
(This depends on what color theme you're using, but applies to Light, Dark, and GitHub built-in themes.)
However, if an UPPERCASE constant is defined as
a static class variable right under class body, without dot notation,
or as a static class variable with dot notation, like ClassName.VARIABLE
or as a instance property variable, like inside method as self.VARIABLE or anywhere else as instance.VARIABLE
then the semantic tokenizer does not recognize the variable as readonly property, and therefore the constant text color is not applied.
Based on the guide about syntax highlighting, I was able to use Developer: Inspect Editor Tokens and Scopes from command palette to see what's going on.
Regular constants with uppercase, which are properly colored, have semantic token type of variable with declaration and readonly modifiers. This will be then matched as variable.other.constant according to this table, which causes the semantic color highlighting in the color theme to kick in.
And regular variables have semantic token type of variable, as expected.
However, when a uppercase constant is declared as a static class variable right under the class body, its semantic token type is property with just declaration modifier. We can see that readonly modifier, present in other uppercase constant, is not shown in this particular setup. Therefore, the semantic color highlighting does not apply for this variable.
We can also note that other regular variables have semantic token type of property, as expected.
I do not know if this is intended to be or if this is a bug in the code. I suppose that this is not intended, because when the variable is inspected with Pylance, it is correctly identified as a constant, based on its uppercase name.
My suggested solution for this is to have readonly modifier for those uppercase constants mentioned above, and the coloring mechanism will be properly applied due to the translation from property.readonly to variable.other.constant.property already present in vscode.
Below is the sample python code that you can also paste to your vscode to see the problem I am mentioning.
SOME_CONSTANT_WITH_UPPERCASE=10# variable is recognized as constant and is properly colored according to variable.other.constantsome_variable_with_lowercase=20# this is a normal variable with typical colorclassTestClass:
UPPERCASE_STATIC_CONSTANT=30# <<< variable is NOT colored according to variable.other.constant or variable.other.constant.property, but as just variable.other.propertylowercase_static_variable=40def__init__(self):
self.UPPERCASE_INSTANCE_CONSTANT=50# <<< variable is NOT colored according to variable.other.constant or variable.other.constant.property, but as just variable.other.propertyself.lowercase_instance_variable=60TestClass.UPPERCASE_STATIC_CONSTANT# <<< variable is NOT colored according to variable.other.constant or variable.other.constant.property, but as just variable.other.propertyTestClass.lowercase_static_variableUPPERCASE_CONSTANT_INSIDE_METHOD=70# variable is recognized as constant and is properly colored according to variable.other.constantlowercase_variable_inside_method=80# normal variable color
As I said, I am not sure where this issue belongs to. If this issue can be fixed in the python extension level, I may be in the right place. If anybody think this issue should be escalated to the vscode issue section, your feedback is also appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am not sure where to post this, as this is not necessarily a bug. But I find this behavior strange, and I was unable to find resource anywhere, and unable to override the setting myself.
Typically, any constant variable defined in ALL UPPERCASE, (with or without underscore), are treated as constant, and is recognized by semantic tokenizer as readonly constant.
In vscode, we also can see that such constant also colored in different color than regular read-write variables.
(This depends on what color theme you're using, but applies to Light, Dark, and GitHub built-in themes.)
However, if an UPPERCASE constant is defined as
ClassName.VARIABLE
self.VARIABLE
or anywhere else asinstance.VARIABLE
then the semantic tokenizer does not recognize the variable as readonly property, and therefore the constant text color is not applied.
Based on the guide about syntax highlighting, I was able to use
Developer: Inspect Editor Tokens and Scopes
from command palette to see what's going on.Regular constants with uppercase, which are properly colored, have semantic token type of

variable
withdeclaration
andreadonly
modifiers. This will be then matched asvariable.other.constant
according to this table, which causes the semantic color highlighting in the color theme to kick in.And regular variables have semantic token type of

variable
, as expected.However, when a uppercase constant is declared as a static class variable right under the class body, its semantic token type is

property
with justdeclaration
modifier. We can see thatreadonly
modifier, present in other uppercase constant, is not shown in this particular setup. Therefore, the semantic color highlighting does not apply for this variable.We can also note that other regular variables have semantic token type of

property
, as expected.I do not know if this is intended to be or if this is a bug in the code. I suppose that this is not intended, because when the variable is inspected with Pylance, it is correctly identified as a constant, based on its uppercase name.
My suggested solution for this is to have
readonly
modifier for those uppercase constants mentioned above, and the coloring mechanism will be properly applied due to the translation fromproperty.readonly
tovariable.other.constant.property
already present in vscode.Below is the sample python code that you can also paste to your vscode to see the problem I am mentioning.
As I said, I am not sure where this issue belongs to. If this issue can be fixed in the python extension level, I may be in the right place. If anybody think this issue should be escalated to the vscode issue section, your feedback is also appreciated.
Thank you for your attention to this matter.
Beta Was this translation helpful? Give feedback.
All reactions