pylance-release
pylance-release copied to clipboard
Stop Suggesting Enum member access on Enum members
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
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.
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).
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.
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.
we need to special case Enum in here - https://github.com/microsoft/pyright/blob/551c6acdcb2384b8a2ffbed270505e590cd9639f/packages/pyright-internal/src/languageService/completionProvider.ts#L1224
This issue has been fixed in prerelease version 2023.7.31, which we've just released. You can find the changelog here: CHANGELOG.md