From 627d3ded611cf5be1791262d6a90a1541c6ad9e3 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 4 Apr 2024 16:47:01 -0700 Subject: [PATCH] Modified the handling of enum class attributes so those with private (mangled) names are exempted from being considered enum members. This reflects the runtime behavior. This addresses #7619. --- packages/pyright-internal/src/analyzer/enums.ts | 7 ++++++- packages/pyright-internal/src/tests/samples/enum1.py | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/analyzer/enums.ts b/packages/pyright-internal/src/analyzer/enums.ts index 5d73a3777a73..a0d60bfd9d77 100644 --- a/packages/pyright-internal/src/analyzer/enums.ts +++ b/packages/pyright-internal/src/analyzer/enums.ts @@ -19,7 +19,7 @@ import { isNodeContainedWithin, } from './parseTreeUtils'; import { Symbol, SymbolFlags } from './symbol'; -import { isSingleDunderName } from './symbolNameUtils'; +import { isPrivateName, isSingleDunderName } from './symbolNameUtils'; import { FunctionArgument, TypeEvaluator, TypeResult } from './typeEvaluatorTypes'; import { enumerateLiteralsForType } from './typeGuards'; import { MemberAccessFlags, computeMroLinearization, lookUpClassMember, makeInferenceContext } from './typeUtils'; @@ -366,6 +366,11 @@ export function transformTypeForPossibleEnumClass( return undefined; } + // The spec excludes private (mangled) names. + if (isPrivateName(nameNode.value)) { + return undefined; + } + // The enum spec doesn't explicitly specify this, but it // appears that callables are excluded. if (!findSubtype(valueType, (subtype) => !isFunction(subtype) && !isOverloadedFunction(subtype))) { diff --git a/packages/pyright-internal/src/tests/samples/enum1.py b/packages/pyright-internal/src/tests/samples/enum1.py index 144aa38fa0c5..80f44c60286d 100644 --- a/packages/pyright-internal/src/tests/samples/enum1.py +++ b/packages/pyright-internal/src/tests/samples/enum1.py @@ -240,3 +240,12 @@ def __new__(cls, val: int, doc: str) -> Self: class TestEnum18(TestEnum17): A = (1, "A") B = (2, "B") + + +class TestEnum19(Enum): + A = 1 + __B = 2 + + +reveal_type(TestEnum19.A, expected_text="Literal[TestEnum19.A]") +reveal_type(TestEnum19.__B, expected_text="Literal[2]")