Search code examples
python-3.xpylance

Does pylance not support functional enum syntax or is there a configuration to get it working?


I am using pylance with vscode.

I discovered the following:

from enum import Enum

# class syntax
# I get IntelliSense 
class Color(Enum):
    RED: str = "RED"
    GREEN: str = "GREEN"
    BLUE: str = "BLUE"

# functional syntax
# I don't get IntelliSense
Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])

I was originally using functional syntax, but if it breaks IntelliSense then I'll use class syntax exclusively.

I also call the enum like this often instance_of_my_enum.name. To do so I need quite a bit more code then that one liner functional syntax.

Is it just impossible or extremely detrimental to performance for pylance to read functional syntax?

Also, the two different syntaxes have no other differences right? From the perspective of the interpreter it doesn't matter?


Solution

  • Apparently it's not supported by design, as mentioned in this issue.

    That's a pity because for example Typer needs StrEnum for choice parameters, and creating the enum entries dynamically from dict keys, for example, would allow linking those str to arbitrary objects while avoiding duplication.

    Anyway, I'm just rambling about my use case, the take home is that we shouldn't expect support to dynamically generated enums in pylance any time soon :'-)