Remove `[]` union syntactic sugar
Closed this issue · 3 comments
criccomini commented
When Recap supports optional: true
I'm thinking of removing the type: ["int32", "float32"]
syntactic sugar. The vast majority of union
usage was for type: ["null", ...]
to create nullable fields. Now that we have optional
, I don't feel the added complexity of supporting array shorthand is worth it.
Instead, all unions will be defined as:
type: struct
fields:
- type: union
types: ["int32", "float32"]
This style will no longer be supported:
type: struct
fields:
- type: ["int32", "float32"]
WDYT? @adrianisk @gwukelic @cpard
cpard commented
looks good to me!
gwukelic commented
Looks good to me too!
criccomini commented
I took a look at what's needed to eliminate this feature. Turns out, it's only this:
def from_dict(
- type_dict: dict | list | str,
+ type_dict: dict | str,
registry: RecapTypeRegistry | None = None,
) -> RecapType:
"""
- Create a RecapType from a dictionary, list, or string. Lists are treated as
- unions and strings are treated as aliases or simple types (e.g. "null",
- "bool", "string", or "bytes").
+ Create a RecapType from a dictionary or string.
- :param type_dict: A dictionary, list, or string representing a Recap type.
+ :param type_dict: A dictionary or string representing a Recap type.
:param registry: A RecapTypeRegistry to use for resolving aliases.
:return: A RecapType.
"""
- if isinstance(type_dict, list):
- type_dict = {"type": "union", "types": type_dict}
- elif isinstance(type_dict, str):
+ if isinstance(type_dict, str):
type_dict = {"type": type_dict}
# Create a copy to avoid modifying the input dictionary
@@ -449,7 +445,7 @@ def from_dict(
return recap_type
-def to_dict(recap_type: RecapType, clean=True, alias=True) -> dict | list | str:
+def to_dict(recap_type: RecapType, clean=True, alias=True) -> dict | str:
"""
Convert a RecapType to a dictionary, list, or string.
@@ -539,7 +535,7 @@ def to_dict(recap_type: RecapType, clean=True, alias=True) -> dict | list | str:
return type_dict
-def clean_dict(type_dict: dict | list | str) -> dict | list | str:
+def clean_dict(type_dict: dict | str) -> dict | str:
"""
Remove defaults from a type dictionary, replace unions with lists when
possible, and replace simple types with strings.
@@ -667,10 +663,6 @@ def clean_dict(type_dict: dict | list | str) -> dict | list | str:
if len(type_dict) == 1:
return type_dict["type"]
- # Shorten {"type": "union", "types": ["type"]} to ["type"]
- if type_dict["type"] == "union" and len(type_dict) == 2:
- return type_dict["types"]
-
return type_dict
Given that it's only 4 lines to support the type: [] syntactic sugar, I'll just leave it.