From ac27540c57ded325399dedb070322d54fa040eda Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 22 Sep 2022 10:53:07 +0200 Subject: [PATCH 1/2] Generate somewhat valid SafeStringWrapper-wrapped class names I don't think there's any downside, and the upside is that we can use https://github.com/kensho-technologies/pytest-annotate to kickstart type annotations. Applying collected annotations (e.g. `pyannotate --py3 --type-info ./annotations.json lib/galaxy/workflow/run.py` currently fails with: ``` Traceback (most recent call last): File "/Users/mvandenb/src/galaxy/.venv/bin/pyannotate", line 8, in sys.exit(main()) File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/__main__.py", line 122, in main data = generate_annotations_json_string( File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/main.py", line 60, in generate_annotations_json_string signature = unify_type_comments(item.type_comments) File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/main.py", line 27, in unify_type_comments arg_types, return_type = infer_annotation(type_comments) File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/infer.py", line 45, in infer_annotation arg_types, return_type = parse_type_comment(comment) File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/parse.py", line 216, in parse_type_comment return Parser(comment).parse() File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/parse.py", line 225, in __init__ self.tokens = tokenize(comment) File "/Users/mvandenb/src/galaxy/.venv/lib/python3.9/site-packages/pyannotate_tools/annotations/parse.py", line 193, in tokenize raise ParseError(original) pyannotate_tools.annotations.parse.ParseError: Invalid type comment: (galaxy.app.UniverseApplication, Dict, Dict[str, galaxy.model.HistoryDatasetAssociation], Dict[Union[galaxy.security.object_wrapper:SafeStringWrapper(str:,,,,,,,), str], Union[galaxy.security.object_wrapper.CallableSafeStringWrapper, galaxy.security.object_wrapper:SafeStringWrapper(str:,,,,,,,), str]]) -> None ``` This fixes that. --- lib/galaxy/security/object_wrapper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/security/object_wrapper.py b/lib/galaxy/security/object_wrapper.py index 359035aabbe..a756c81e05c 100644 --- a/lib/galaxy/security/object_wrapper.py +++ b/lib/galaxy/security/object_wrapper.py @@ -168,7 +168,9 @@ def wrap_with_safe_string(value, no_wrap_classes=None): value_mod = inspect.getmodule(value) if value_mod: wrapped_class_name = f"{value_mod.__name__}.{wrapped_class_name}" - wrapped_class_name = f"SafeStringWrapper({wrapped_class_name}:{','.join(sorted(map(str, no_wrap_classes)))})" + wrapped_class_name = ( + f"SafeStringWrapper__{wrapped_class_name}__{'__'.join(sorted([c.__name__ for c in no_wrap_classes]))}" + ) do_wrap_func_name = f"__do_wrap_{wrapped_class_name}" do_wrap_func = __do_wrap global_dict = globals() From 71ec24ec87bbadd3efd06d3b06a6fe2d6877d5ff Mon Sep 17 00:00:00 2001 From: Marius van den Beek Date: Thu, 22 Sep 2022 13:13:13 +0200 Subject: [PATCH 2/2] Drop unnecessary list comprehension Co-authored-by: Nicola Soranzo --- lib/galaxy/security/object_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/security/object_wrapper.py b/lib/galaxy/security/object_wrapper.py index a756c81e05c..15352cfa9fd 100644 --- a/lib/galaxy/security/object_wrapper.py +++ b/lib/galaxy/security/object_wrapper.py @@ -169,7 +169,7 @@ def wrap_with_safe_string(value, no_wrap_classes=None): if value_mod: wrapped_class_name = f"{value_mod.__name__}.{wrapped_class_name}" wrapped_class_name = ( - f"SafeStringWrapper__{wrapped_class_name}__{'__'.join(sorted([c.__name__ for c in no_wrap_classes]))}" + f"SafeStringWrapper__{wrapped_class_name}__{'__'.join(sorted(c.__name__ for c in no_wrap_classes))}" ) do_wrap_func_name = f"__do_wrap_{wrapped_class_name}" do_wrap_func = __do_wrap