Skip to content

Dedup merged Literal args by type and value - #472

Open
youdie006 wants to merge 1 commit into
reagento:developfrom
youdie006:literal-merge-type-aware-dedup
Open

youdie006 wants to merge 1 commit into
reagento:developfrom
youdie006:literal-merge-type-aware-dedup

Conversation

@youdie006

Copy link
Copy Markdown

When literals are merged (a Literal inside a union, or None inside a Literal), _create_norm_literal removes duplicates with a plain set. Since True == 1, False == 0 and enum members compare equal to their values, one of them is dropped, and strict coercion then rejects it:

from typing import Literal, Optional
from adaptix import Retort

retort = Retort()
retort.load(True, Literal[1, True])            # True
retort.load(True, Optional[Literal[1, True]])  # UnionLoadError (allowed_values=frozenset({1}))
retort.load(False, Literal[0] | Literal[False])  # BadVariantLoadError

This dedups on (type, value) with the existing _type_and_value_iter, the same rule _LiteralNormType.__eq__ and the strict literal loader already use. Real duplicates are still removed.

One visible side effect: an enum member that equals a plain value, as in Literal["red", Color.RED, None], is now kept, so it loads and dumps the way Literal["red", Color.RED] already does without the merge.

Tests are added to test_normalize_type.py::test_literal and test_literal_provider.py; they fail on develop. The unit tests and ruff pass.

Written with AI assistance (Claude); I have reviewed the change.

_create_norm_literal removed duplicates with a plain set, and True == 1,
False == 0 and IntEnum/StrEnum members compare equal to their values, so
Literal[1, True, None], Optional[Literal[1, True]] and
Literal[0] | Literal[False] lost a case and rejected True/False under
strict coercion. Dedup on (type, value) as _LiteralNormType.__eq__ does.
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant