From 30cef7decbb4d2a3a7ca56921314ed7be379c529 Mon Sep 17 00:00:00 2001 From: liubo Date: Tue, 7 Nov 2023 15:45:38 +0800 Subject: [PATCH] Detect typing module attributes with 'import typing as ' Signed-off-by: liubo (cherry picked from commit bb121d6e37b64db2dc78bc103aaeb570b3d00b56) --- ...dule-attributes-with-import-typing-a.patch | 75 +++++++++++++++++++ pyflakes.spec | 9 ++- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 0001-Detect-typing-module-attributes-with-import-typing-a.patch diff --git a/0001-Detect-typing-module-attributes-with-import-typing-a.patch b/0001-Detect-typing-module-attributes-with-import-typing-a.patch new file mode 100644 index 0000000..d02da65 --- /dev/null +++ b/0001-Detect-typing-module-attributes-with-import-typing-a.patch @@ -0,0 +1,75 @@ +From 13cad915e6b181b2f6a85efc2ead4856b23bccc0 Mon Sep 17 00:00:00 2001 +From: Angus L'Herrou +Date: Tue, 5 Oct 2021 18:44:29 -0400 +Subject: [PATCH] Detect typing module attributes with 'import typing as + ' (#632) + +* added functionality to detect typing module attributes with 'import typing as ' + +* remove async keyword from test_aliased_import +--- + pyflakes/checker.py | 12 +++++++++++- + pyflakes/test/test_type_annotations.py | 17 +++++++++++++++++ + 2 files changed, 28 insertions(+), 1 deletion(-) + +diff --git a/pyflakes/checker.py b/pyflakes/checker.py +index 6fafe89..45c7a4a 100644 +--- a/pyflakes/checker.py ++++ b/pyflakes/checker.py +@@ -720,6 +720,16 @@ def _is_typing_helper(node, is_name_match_fn, scope_stack): + + return False + ++ def _module_scope_is_typing(name): ++ for scope in reversed(scope_stack): ++ if name in scope: ++ return ( ++ isinstance(scope[name], Importation) and ++ scope[name].fullName in TYPING_MODULES ++ ) ++ ++ return False ++ + return ( + ( + isinstance(node, ast.Name) and +@@ -727,7 +737,7 @@ def _is_typing_helper(node, is_name_match_fn, scope_stack): + ) or ( + isinstance(node, ast.Attribute) and + isinstance(node.value, ast.Name) and +- node.value.id in TYPING_MODULES and ++ _module_scope_is_typing(node.value.id) and + is_name_match_fn(node.attr) + ) + ) +diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py +index 6a66bcd..f3b6c24 100644 +--- a/pyflakes/test/test_type_annotations.py ++++ b/pyflakes/test/test_type_annotations.py +@@ -121,6 +121,23 @@ class TestTypeAnnotations(TestCase): + def f(self, x): return x + """) + ++ def test_aliased_import(self): ++ """Detect when typing is imported as another name""" ++ self.flakes(""" ++ import typing as t ++ ++ @t.overload ++ def f(s): # type: (None) -> None ++ pass ++ ++ @t.overload ++ def f(s): # type: (int) -> int ++ pass ++ ++ def f(s): ++ return s ++ """) ++ + def test_not_a_typing_overload(self): + """regression test for @typing.overload detection bug in 2.1.0""" + self.flakes(""" +-- +2.42.0.windows.2 + diff --git a/pyflakes.spec b/pyflakes.spec index b3a9038..9a46a69 100644 --- a/pyflakes.spec +++ b/pyflakes.spec @@ -3,7 +3,7 @@ Pyflakes A simple program which checks Python source files for errors.Pyflakes Name: python-pyflakes Version: 2.3.1 -Release: 1 +Release: 2 Summary: passive checker of Python programs License: MIT URL: https://github.com/PyCQA/pyflakes @@ -12,6 +12,8 @@ BuildArch: noarch %description %{common_desc} +Patch0001: 0001-Detect-typing-module-attributes-with-import-typing-a.patch + %package -n python3-pyflakes Summary: passive checker of Python programs Provides: python-pyflakes @@ -31,7 +33,7 @@ Provides: python3-pyflakes-doc %{common_desc} %prep -%autosetup -n pyflakes-2.3.1 +%autosetup -n pyflakes-2.3.1 -p1 %build %py3_build @@ -76,6 +78,9 @@ mv %{buildroot}/doclist.lst . %{_docdir}/* %changelog +* Tue Nov 7 2023 liubo - 2.3.1-2 +- Detect typing module attributes with 'import typing as ' + * Mon Jul 19 2021 OpenStack_SIG - 2.3.1-1 - update to 2.3.1