From 9c2cdf5ae4817bfd6c4065575c14bdb2f459a895 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 24 Dec 2021 11:52:48 +0100 Subject: [PATCH] Fix typing errors. --- src/antsibull/jinja2/filters.py | 2 +- src/antsibull/semantic_helper.py | 18 +++++++++--------- src/sphinx_antsibull_ext/roles.py | 4 ++-- stubs/jinja2/utils.pyi | 7 +++++++ tests/units/test_jinja2.py | 6 +++++- 5 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 stubs/jinja2/utils.pyi diff --git a/src/antsibull/jinja2/filters.py b/src/antsibull/jinja2/filters.py index 38bc2e59b..4c8d774d8 100644 --- a/src/antsibull/jinja2/filters.py +++ b/src/antsibull/jinja2/filters.py @@ -39,7 +39,7 @@ _UNESCAPE = re.compile(r"\\(.)") -def extract_plugin_data(context: Context) -> t.Tuple[str, str]: +def extract_plugin_data(context: Context) -> t.Tuple[t.Optional[str], t.Optional[str]]: plugin_fqcn = context.get('plugin_name') plugin_type = context.get('plugin_type') if plugin_fqcn is None or plugin_type is None: diff --git a/src/antsibull/semantic_helper.py b/src/antsibull/semantic_helper.py index 299d28a70..22788b053 100644 --- a/src/antsibull/semantic_helper.py +++ b/src/antsibull/semantic_helper.py @@ -18,8 +18,8 @@ def _remove_array_stubs(text: str) -> str: return _ARRAY_STUB_RE.sub('', text) -def parse_option(text: str, plugin_fqcn: str, plugin_type: str, require_plugin=False - ) -> t.Tuple[str, str, str, str, t.Optional[str]]: +def parse_option(text: str, plugin_fqcn: t.Optional[str], plugin_type: t.Optional[str], + require_plugin=False) -> t.Tuple[str, str, str, str, t.Optional[str]]: """ Given the contents of O(...) / :ansopt:`...` with potential escaping removed, split it into plugin FQCN, plugin type, option link name, option name, and option value. @@ -43,8 +43,8 @@ def parse_option(text: str, plugin_fqcn: str, plugin_type: str, require_plugin=F return plugin_fqcn, plugin_type, _remove_array_stubs(text), text, value -def parse_return_value(text: str, plugin_fqcn: str, plugin_type: str, require_plugin=False - ) -> t.Tuple[str, str, str, str, t.Optional[str]]: +def parse_return_value(text: str, plugin_fqcn: t.Optional[str], plugin_type: t.Optional[str], + require_plugin=False) -> t.Tuple[str, str, str, str, t.Optional[str]]: """ Given the contents of RV(...) / :ansretval:`...` with potential escaping removed, split it into plugin FQCN, plugin type, option link name, option name, and option value. @@ -68,7 +68,8 @@ def parse_return_value(text: str, plugin_fqcn: str, plugin_type: str, require_pl return plugin_fqcn, plugin_type, _remove_array_stubs(text), text, value -def augment_plugin_name_type(text: str, plugin_fqcn: str, plugin_type: str) -> str: +def augment_plugin_name_type(text: str, plugin_fqcn: t.Optional[str], plugin_type: t.Optional[str] + ) -> str: """ Given the text contents of O(...) or RV(...) and a plugin's FQCN and type, insert the FQCN and type if they are not already present. @@ -76,7 +77,6 @@ def augment_plugin_name_type(text: str, plugin_fqcn: str, plugin_type: str) -> s value = None if '=' in text: text, value = text.split('=', 1) - if ':' not in text: - text = '{plugin_fqcn}#{plugin_type}:{text}'.format( - plugin_fqcn=plugin_fqcn, plugin_type=plugin_type, text=text) - return text if value is None else ('{text}={value}'.format(text=text, value=value)) + if ':' not in text and plugin_fqcn and plugin_type: + text = f'{plugin_fqcn}#{plugin_type}:{text}' + return text if value is None else f'{text}={value}' diff --git a/src/sphinx_antsibull_ext/roles.py b/src/sphinx_antsibull_ext/roles.py index b4337e135..5fc451e73 100644 --- a/src/sphinx_antsibull_ext/roles.py +++ b/src/sphinx_antsibull_ext/roles.py @@ -47,8 +47,8 @@ def _create_ref_or_not(create_ref: t.Callable[[str, str, str], t.Optional[str]], 'refexplicit': True, 'refwarn': True, } - refnode = addnodes.pending_xref(text, content, **options) - refnode['reftarget'] = ref + refnode = addnodes.pending_xref(text, content, **options) # pyre-ignore[19] + refnode['reftarget'] = ref # pyre-ignore[16] return '', [refnode] diff --git a/stubs/jinja2/utils.pyi b/stubs/jinja2/utils.pyi new file mode 100644 index 000000000..7bc79be36 --- /dev/null +++ b/stubs/jinja2/utils.pyi @@ -0,0 +1,7 @@ +import typing as t + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + +def pass_context(f: F) -> F: ... +def pass_eval_context(f: F) -> F: ... +def pass_environment(f: F) -> F: ... diff --git a/tests/units/test_jinja2.py b/tests/units/test_jinja2.py index 03875d2b3..f21236674 100644 --- a/tests/units/test_jinja2.py +++ b/tests/units/test_jinja2.py @@ -30,7 +30,11 @@ @pytest.mark.parametrize('text, expected', RST_IFY_DATA.items()) def test_rst_ify(text, expected): - assert rst_ify(text) == expected + context = { + 'plugin_name': 'foo.bar.baz', + 'plugin_type': 'module', + } + assert rst_ify(context, text) == expected RST_ESCAPE_DATA = {