Skip to content

Commit

Permalink
Modified the handling of enum class attributes so those with private …
Browse files Browse the repository at this point in the history
…(mangled) names are exempted from being considered enum members. This reflects the runtime behavior. This addresses #7619.
  • Loading branch information
erictraut committed Apr 4, 2024
1 parent aff916f commit 627d3de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/pyright-internal/src/analyzer/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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))) {
Expand Down
9 changes: 9 additions & 0 deletions packages/pyright-internal/src/tests/samples/enum1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")

0 comments on commit 627d3de

Please sign in to comment.