From 3e5ce3210f6c95256a8866f1ba27bf9cfca0b80f Mon Sep 17 00:00:00 2001 From: Timothy Buzzelli Date: Mon, 18 Sep 2023 11:33:54 -0700 Subject: [PATCH 1/5] Properly strip types that come before a [ --- mypy/stubgenc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 31487f9d0dcf1..c0984df7e9c69 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -424,7 +424,7 @@ def strip_or_import( stripped_subtyp = strip_or_import(subtyp.strip(), module, known_modules, imports) if stripped_subtyp != subtyp: stripped_type = re.sub( - r"(^|[\[, ]+)" + re.escape(subtyp) + r"($|[\], ]+)", + r"(^|[\[, ]+)" + re.escape(subtyp) + r"($|[\[\], ]+)", r"\1" + stripped_subtyp + r"\2", stripped_type, ) From 759ee9788afb74d2c22152b6a9baf69231f8caa3 Mon Sep 17 00:00:00 2001 From: Timothy Buzzelli Date: Thu, 21 Sep 2023 11:51:34 -0700 Subject: [PATCH 2/5] Update teststubgen.py Add test for fix to strip_or_import --- mypy/test/teststubgen.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 7e30515ac8926..cca44cef82ee9 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -1146,6 +1146,33 @@ def test(arg0: str) -> None: assert_equal(output, ["def test(arg0: argparse.Action) -> argparse.Action: ..."]) assert_equal(set(imports), {"import argparse"}) + def test_generate_c_function_same_module_custom_nested(self) -> None: + """Test that if annotation references a complicated nested type from same module but using full path, + no module will be imported, and type specification will be striped to local reference. + """ + + # Provide different type in python spec than in docstring to make sure, that docstring + # information is used. + def test(arg0: str) -> None: + """ + test(arg0: argparse.Action[int, argparse.OtherType[str]]) -> argparse.Action[argparse.OtherType[str, str], int] + """ + + output: list[str] = [] + imports: list[str] = [] + mod = ModuleType("argparse", "") + generate_c_function_stub( + mod, + "test", + test, + output=output, + imports=imports, + known_modules=[mod.__name__], + sig_generators=get_sig_generators(parse_options([])), + ) + assert_equal(output, ["def test(arg0: Action[int, OtherType[str]]) -> Action[OtherType[str, str], int]: ..."]) + assert_equal(imports, []) + def test_generate_c_function_same_module_nested(self) -> None: """Test that if annotation references type from same module but using full path, no module will be imported, and type specification will be stripped to local reference. From a41e5f3e336d4c35ce768ff2916b1e7d22aa5e92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:51:56 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/test/teststubgen.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index cca44cef82ee9..e646c4e53ef9f 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -1170,7 +1170,12 @@ def test(arg0: str) -> None: known_modules=[mod.__name__], sig_generators=get_sig_generators(parse_options([])), ) - assert_equal(output, ["def test(arg0: Action[int, OtherType[str]]) -> Action[OtherType[str, str], int]: ..."]) + assert_equal( + output, + [ + "def test(arg0: Action[int, OtherType[str]]) -> Action[OtherType[str, str], int]: ..." + ], + ) assert_equal(imports, []) def test_generate_c_function_same_module_nested(self) -> None: From aad9c7569ea3724773b598cbf175373faf5e8a25 Mon Sep 17 00:00:00 2001 From: Timothy Buzzelli Date: Thu, 21 Sep 2023 13:52:40 -0700 Subject: [PATCH 4/5] Update teststubgen.py Fix unit test to check for proper spaces in output. --- mypy/test/teststubgen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index e646c4e53ef9f..2ddfbf5e22935 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -1155,7 +1155,7 @@ def test_generate_c_function_same_module_custom_nested(self) -> None: # information is used. def test(arg0: str) -> None: """ - test(arg0: argparse.Action[int, argparse.OtherType[str]]) -> argparse.Action[argparse.OtherType[str, str], int] + test(arg0: argparse.Action[int, argparse.OtherType[str]]) -> argparse.Action[argparse.OtherType[str,str], int] """ output: list[str] = [] @@ -1173,7 +1173,7 @@ def test(arg0: str) -> None: assert_equal( output, [ - "def test(arg0: Action[int, OtherType[str]]) -> Action[OtherType[str, str], int]: ..." + "def test(arg0: Action[int,OtherType[str]]) -> Action[OtherType[str,str],int]: ..." ], ) assert_equal(imports, []) From 763775d01e3b9fb15e233f683e108808a0010862 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 20:53:36 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/test/teststubgen.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 2ddfbf5e22935..102986f7123d2 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -1172,9 +1172,7 @@ def test(arg0: str) -> None: ) assert_equal( output, - [ - "def test(arg0: Action[int,OtherType[str]]) -> Action[OtherType[str,str],int]: ..." - ], + ["def test(arg0: Action[int,OtherType[str]]) -> Action[OtherType[str,str],int]: ..."], ) assert_equal(imports, [])