Skip to content

Commit

Permalink
Update: Strategy to find 'never none' and 'accept none' (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nutti committed Sep 28, 2024
1 parent 475e5b1 commit bb956aa
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/fake_bpy_module/analyzer/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,11 @@ def _parse_arg_option(self, arg_list_node: ArgumentListNode,
arg_node = n.parent
break
if arg_node:
for dtype_node in arg_node.findall(DataTypeNode):
dtype_node.attributes[option_type] = option_body.astext()
if option_body.astext() == "update-argument-type":
arg_node.attributes[option_type] = option_body.astext()
else:
for dtype_node in arg_node.findall(DataTypeNode):
dtype_node.attributes[option_type] = option_body.astext()

def _parse_return_option(self, return_node: FunctionReturnNode,
option_type: str,
Expand Down
23 changes: 21 additions & 2 deletions src/fake_bpy_module/transformer/data_type_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,20 +664,39 @@ def _get_refined_data_type(
dtype_str_changed, module_name, variable_kind,
additional_info=additional_info)

def is_cls_attr_in_never_none_blacklist(
class_full_name: str, attr_name: str) -> bool:
blacklist = (
"bpy.types.ID.library"
)
return f"{class_full_name}.{attr_name}" in blacklist

# Add options.
for r in result:
option_results = original_options.copy()
option_results.extend(options.copy())
if "option" in r.attributes:
option_results.extend(r.attributes["option"].split(","))

# list object will not be None.
if (variable_kind in ('CLS_ATTR', 'CONST') and
"never none" not in option_results):
if _REGEX_DATA_TYPE_STARTS_WITH_COLLECTION.match(r.to_string()):
option_results.append("never none")
# If data type is bpy.types.Context, it will be never None.

# If data type is bpy.types.Context, it will accept None.
if r.to_string() == "bpy.types.Context":
option_results.append("never none")
while "never none" in option_results:
option_results.remove("never none")
option_results.append("accept none")

if variable_kind == 'CLS_ATTR':
if is_cls_attr_in_never_none_blacklist(
additional_info["self_class"],
additional_info["data_name"]):
option_results.append("never none")

option_results = sorted(set(option_results))
r.attributes["option"] = ",".join(option_results)

output_log(
Expand Down
8 changes: 8 additions & 0 deletions src/fake_bpy_module/transformer/mod_applier.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def _mod_update_function_arguments(
arg_name = arg_node.element(NameNode).astext()

if arg_name == mod_arg_name:
mod_options = []
node_attrs = mod_arg_node.attributes
if "mod-option" in node_attrs:
mod_options = node_attrs["mod-option"].split(",")
if "update-argument-type" in mod_options:
arg_node.attributes["argument_type"] = \
node_attrs["argument_type"]

mod_desc_node = mod_arg_node.element(DescriptionNode)
if not mod_desc_node.empty():
arg_node.replace_node(mod_desc_node)
Expand Down
18 changes: 17 additions & 1 deletion src/mods/common/analyzer/append/bpy.types.mod.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,28 @@
:mod-option rtype: skip-refine
:option function: overload

.. method:: __setitem__(key, value)

:type key: int | str
:mod-option arg key: skip-refine
:type value: GenericType1 | None
:mod-option arg value: skip-refine

.. method:: __setitem__(key, value)

:type key: int
:mod-option arg key: skip-refine
:type value: _GenericType1 | None
:mod-option arg value: skip-refine
:option function: overload

.. method:: __setitem__(key, value)

:type key: str
:mod-option arg key: skip-refine
:type value: _GenericType1
:mod-option arg value: skip-refine
:option function: overload

.. method:: __iter__()

Expand All @@ -76,7 +92,7 @@

.. method:: __contains__(key)

:type key: str | tuple[str, ...]
:type key: str | tuple[str, ...] | _GenericType1
:mod-option arg key: skip-refine
:rtype: bool
:mod-option rtype: skip-refine
Expand Down
14 changes: 13 additions & 1 deletion src/mods/common/analyzer/update/bpy.types.mod.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

.. method:: values()

:rtype: list[_GenericType1]
:rtype: list[_GenericType1 | None]
:mod-option rtype: skip-refine

.. method:: find(key)

:type key: str (never None)

.. method:: get(key, default=None)

:type key: str (never None)
:type default: _GenericType2
:mod-option arg default: skip-refine
:generic-types: _GenericType2
Expand Down Expand Up @@ -147,3 +152,10 @@
.. attribute:: node_tree

:type: :class:`bpy.types.TextureNodeTree`

.. class:: IDMaterials

.. method:: pop(*, index=-1)

:type index: int (never None)
:mod-option arg index: update-argument-type
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,35 @@
<return>
<description>
<data-type-list>
<function function_type="method">
<name>
method_2
<description>
<argument-list>
<argument argument_type="arg" mod-option="update-argument-type">
<name>
arg_1
<description>
<default-value>
<data-type-list>
<data-type>
method_2 arg_1 type
<return>
<description>
<data-type-list>
<function function_type="method">
<name>
method_3
<description>
<argument-list>
<argument argument_type="arg">
<name>
arg_1
<description>
<default-value>
<data-type-list>
<data-type mod-option="skip-refine">
method_3 arg_1 type
<return>
<description>
<data-type-list>
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
.. method:: method_1()

:option function: overload

.. method:: method_2(arg_1)

:type arg_1: method_2 arg_1 type
:mod-option arg arg_1: update-argument-type

.. method:: method_3(arg_1)

:type arg_1: method_3 arg_1 type
:mod-option arg arg_1: skip-refine
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,21 @@ def test_option(self) -> None:
analyzer = BaseAnalyzer()
documents = analyzer.analyze(rst_files)

entry_points = []
entry_point = EntryPoint("bpy.types", "Context", "class")
entry_points.append(entry_point)
entry_point = EntryPoint("bpy.types", "ID", "class")
entry_points.append(entry_point)

self.assertEqual(len(documents), len(expect_files))
for doc, expect in zip(documents, expect_files, strict=True):
self.compare_with_file_contents(doc.pformat(), expect)

transformer = Transformer(["data_type_refiner"], {})
transformer = Transformer(["data_type_refiner"], {
"data_type_refiner": {
"entry_points": entry_points,
}
})
transformed = transformer.transform(documents)

self.assertEqual(len(transformed), len(expect_transformed_files))
Expand Down Expand Up @@ -1210,6 +1220,35 @@ def test_update_function(self) -> None:
for doc, expect_file in zip(transformed, expect_files, strict=True):
self.compare_with_file_contents(doc.pformat(), expect_file)

def test_update_function_update_argument_type(self) -> None:
rst_files = ["base.rst"]
mod_files = ["update_function_update_argument_type.mod.rst"]
expect_mod_files = ["update_function_update_argument_type.mod.xml"]
expect_files = ["update_function_update_argument_type.xml"]
rst_files = [f"{self.data_dir}/input/{f}" for f in rst_files]
mod_files = [f"{self.data_dir}/input/{f}" for f in mod_files]
expect_mod_files = [f"{self.data_dir}/expect/{f}"
for f in expect_mod_files]
expect_files = [f"{self.data_dir}/expect/{f}" for f in expect_files]

analyzer = BaseAnalyzer()
documents = analyzer.analyze(rst_files)

transformer = Transformer(["mod_applier"],
{"mod_applier": {"mod_files": mod_files}})
transformed = transformer.transform(documents)
self.assertEqual(len(transformer.get_transformers()), 1)
mod_documents = transformer.get_transformers()[0].get_mod_documents()

self.assertEqual(len(mod_documents), len(expect_mod_files))
for mod_doc, expect_file in zip(mod_documents, expect_mod_files,
strict=True):
self.compare_with_file_contents(mod_doc.pformat(), expect_file)

self.assertEqual(len(transformed), len(rst_files))
for doc, expect_file in zip(transformed, expect_files, strict=True):
self.compare_with_file_contents(doc.pformat(), expect_file)

def test_update_class(self) -> None:
rst_files = ["base.rst"]
mod_files = ["update_class.mod.rst"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@
<return>
<description>
<data-type-list>
<function function_type="function">
<name>
function_3
<description>
<argument-list>
<argument argument_type="arg">
<name>
arg_accept_none
<description>
function_3 arg_accept_none description
<default-value>
<data-type-list>
<data-type>
<class-ref>
bpy.types.Context
<return>
<description>
<data-type-list>
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@
<return>
<description>
<data-type-list>
<function function_type="function">
<name>
function_3
<description>
<argument-list>
<argument argument_type="arg">
<name>
arg_accept_none
<description>
function_3 arg_accept_none description
<default-value>
<data-type-list>
<data-type option="accept none">
<class-ref>
bpy.types.Context
<return>
<description>
<data-type-list>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
option_rna_based.rst
<module>
<name>
bpy.type
bpy.types
<description>
<function function_type="function">
<name>
Expand Down Expand Up @@ -71,6 +71,20 @@
<data-type>
int
<function-list>
<class>
<name>
ID
<description>
<base-class-list>
<attribute-list>
<attribute>
<name>
library
<description>
<data-type-list>
<data-type>
int
<function-list>
<data>
<name>
active_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
option_rna_based.rst
<module>
<name>
bpy.type
bpy.types
<description>
<function function_type="function">
<name>
Expand Down Expand Up @@ -71,6 +71,20 @@
<data-type option="accept none">
int
<function-list>
<class>
<name>
ID
<description>
<base-class-list>
<attribute-list>
<attribute>
<name>
library
<description>
<data-type-list>
<data-type option="never none">
int
<function-list>
<data>
<name>
active_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
:type arg_accept_none_4: int
:arg arg_never_none: Optional function_2 arg_never_none description
:type arg_never_none: str

.. function:: function_3(arg_accept_none)

:arg arg_accept_none: function_3 arg_accept_none description
:type arg_accept_none: :class:`bpy.types.Context`
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. module:: bpy.type
.. module:: bpy.types

.. function:: function_1(arg_optional, arg_readonly, arg_never_none)

Expand All @@ -20,6 +20,12 @@

:type: int

.. class:: ID

.. attribute:: library

:type: int

.. data:: active_data

:type: int
Loading

0 comments on commit bb956aa

Please sign in to comment.