Skip to content

Commit

Permalink
Add support for labeling Literal types correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Aug 15, 2024
1 parent fe0ea6f commit ee96b15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec_classes/utils/type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def type_label(attr_type: Type) -> str:
"""
if hasattr(attr_type, "__origin__"): # Generics
label = type_label(attr_type.__origin__)
if attr_type.__origin__ is Literal:
return (
f"{label}[{', '.join(repr(arg) for arg in attr_type.__args__)}]"
)
if hasattr(attr_type, "__args__") and not any(
isinstance(arg, TypeVar) for arg in attr_type.__args__
):
Expand Down
1 change: 1 addition & 0 deletions tests/utils/test_type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def test_attr_type_label(self):
assert type_label(KeyedList[KeyedSpec, str]()) == "KeyedList[KeyedSpec, str]"
assert type_label(Any) == "Any"
assert type_label(Union[Any, Dict[str, int]]) == "Union[Any, dict[str, int]]"
assert type_label(Literal["a", True, 3, b"c"]) == "Literal['a', True, 3, b'c']"

def test_type_instantiate(self):
assert type_instantiate(str) == ""
Expand Down

0 comments on commit ee96b15

Please sign in to comment.