Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nutti committed Jan 12, 2025
1 parent 6949bb6 commit 5f732e4
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/fake_bpy_module/transformer/data_type_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
_REGEX_DATA_TYPE_OPTION_OPTIONAL = re.compile(r"(^|^An |\()[oO]ptional(\s|\))")
_REGEX_DATA_TYPE_STARTS_WITH_COLLECTION = re.compile(r"^(list|tuple|dict)")
_REGEX_DATA_TYPE_MODIFIER_TYPES = re.compile(r"^(Sequence|Callable|list|dict|tuple|type)?\[(.+)\]$") # noqa: E501
_REGEX_DATA_TYPE_START_AND_END_WITH_PARENTHESES = re.compile(r"^\((.+)\)$") # noqa: E501
_REGEX_DATA_TYPE_START_AND_END_WITH_PARENTHESES = re.compile(r"^\((.+)\)$")

REGEX_SPLIT_OR = re.compile(r" \| | or |,")

Expand Down Expand Up @@ -727,12 +727,9 @@ def _get_refined_data_type_splited(
uniq_full_names = self._entry_points_cache["uniq_full_names"]
uniq_module_names = self._entry_points_cache["uniq_module_names"]

# [Ex] (Vector, Quaternion, Vector) # noqa: ERA001
if m := _REGEX_DATA_TYPE_START_AND_END_WITH_PARENTHESES.match(
dtype_str):
modifier = "tuple"

elements = split_string_by_comma(m.group(1), False)
def parse_multiple_data_type_elements(
elements_str: str, modifier: str) -> list[DataTypeNode]:
elements = split_string_by_comma(elements_str, False)
elm_dtype_nodes: list[list[DataTypeNode]] = []
for elm in elements:
dtype_nodes = self._get_refined_data_type_internal(
Expand All @@ -756,6 +753,11 @@ def _get_refined_data_type_splited(
append_child(new_dtype_node, nodes.Text(modifier))
return [new_dtype_node]

# [Ex] (Vector, Quaternion, Vector) # noqa: ERA001
if m := _REGEX_DATA_TYPE_START_AND_END_WITH_PARENTHESES.match(
dtype_str):
return parse_multiple_data_type_elements(m.group(1), "tuple")

# Handle Python typing syntax.
if m := _REGEX_DATA_TYPE_MODIFIER_TYPES.match(dtype_str):
pydoc_to_typing_annotation = {
Expand All @@ -768,29 +770,7 @@ def _get_refined_data_type_splited(
modifier = ""
modifier = pydoc_to_typing_annotation.get(modifier, modifier)

elements = split_string_by_comma(m.group(2), False)
elm_dtype_nodes: list[list[DataTypeNode]] = []
for elm in elements:
dtype_nodes = self._get_refined_data_type_internal(
elm, module_name, variable_kind, additional_info)
if len(dtype_nodes) >= 1:
elm_dtype_nodes.append(dtype_nodes)

new_dtype_node = DataTypeNode()
if len(elm_dtype_nodes) >= 1:
append_child(new_dtype_node, nodes.Text(f"{modifier}["))
for i, dtype_nodes in enumerate(elm_dtype_nodes):
for j, dtype_node in enumerate(dtype_nodes):
for child in dtype_node.children:
append_child(new_dtype_node, child)
if j != len(dtype_nodes) - 1:
append_child(new_dtype_node, nodes.Text(" | "))
if i != len(elm_dtype_nodes) - 1:
append_child(new_dtype_node, nodes.Text(", "))
append_child(new_dtype_node, nodes.Text("]"))
else:
append_child(new_dtype_node, nodes.Text(modifier))
return [new_dtype_node]
return parse_multiple_data_type_elements(m.group(1), modifier)

# Ex. string, default "", -> string
if m := REGEX_MATCH_DATA_TYPE_WITH_DEFAULT.match(dtype_str):
Expand Down

0 comments on commit 5f732e4

Please sign in to comment.