pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Stop Suggesting Enum member access on Enum members

Open Gobot1234 opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. Currently accessing a member of an Enum from an Enum while supported is deprecated.

Describe the solution you'd like Stop showing autocomplete for Enum members on Enums or warn that this behaviour is deprecated.

>>> class MyEnum(enum.Enum): 
...     this = 1
>>> MyEnum.this.this  # this is currently shown as being valid
<stdin>:1: DeprecationWarning: accessing one member from another is not supported,  and will be disabled in 3.12
MyEnum.this

Gobot1234 avatar Oct 04 '21 08:10 Gobot1234

This isn't a core type checking issue; it's a language server issue. I'm going to transfer it to the pylance-release repo so it get the appropriate attention.

erictraut avatar Oct 06 '21 04:10 erictraut

Is this entirely a completion problem, or should there be a change in our enum handling code to disallow this? this isn't defined in the enum stub, so it seems like it'd be a change in the enum code to disallow .this multiple times for 3.12+ (and then maybe we change the completion provider now to not suggest it).

jakebailey avatar Oct 06 '21 17:10 jakebailey

I got a few wires crossed and completely missed that this was the enum member by the time I finished my thought.

I'm not sure what all is special cased here versus in the enum's stub itself; it seems like __getitem__ returns the self type, though that's the [] operator.

jakebailey avatar Oct 06 '21 18:10 jakebailey

This variation on the original sample might help clarify:

from enum import Enum

class MyEnum(Enum):
    this = 1
    that = 2

print(MyEnum.this.that)

This code runs fine today and will continue to run fine until Python 3.12. As such, I don't know that we should mark it is a type error today. But I agree that we shouldn't suggest this as a completion suggestion.

erictraut avatar Oct 06 '21 23:10 erictraut

we need to special case Enum in here - https://github.com/microsoft/pyright/blob/551c6acdcb2384b8a2ffbed270505e590cd9639f/packages/pyright-internal/src/languageService/completionProvider.ts#L1224

heejaechang avatar Apr 13 '22 15:04 heejaechang

This issue has been fixed in prerelease version 2023.7.31, which we've just released. You can find the changelog here: CHANGELOG.md

heejaechang avatar Jul 19 '23 21:07 heejaechang