Skip to content

Equivalence of single-member enums not fully implemented when deeply nested inside other types #874

@AlexWaygood

Description

@AlexWaygood

Summary

from enum import Enum
from ty_extensions import static_assert, is_equivalent_to
from typing import Literal, reveal_type, Protocol

class Foo(Enum):
    X = 1

static_assert(
    is_equivalent_to(
        tuple[Foo] | int | str,
        str | int | tuple[Literal[Foo.X]]
    )
)

class Bar(Protocol):
    a: Foo

class Baz(Protocol):
    a: Literal[Foo.X]

static_assert(is_equivalent_to(Bar, Baz))

Both these static_assert calls fail currently, but they should both succeed. I think this should be easily fixable by fiddling with the implementation of Type::normalized() for enums: either we normalize Literal[Foo.X] to Foo if the enum only has a single member, or we normalize Foo to Literal[Foo.X] if Foo only has a single member.

https://play.ty.dev/441ade21-3807-4571-a0de-e8f317a08599

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingenumstype propertiessubtyping, assignability, equivalence, and more

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions