From bd99419e3e9be403a88b8ba07283ef35b1db539f Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 02:20:07 +0000 Subject: [PATCH 01/10] Fix a slip up I made --- mypy/semanal.py | 3 +-- test-data/unit/check-typeguard.test | 10 +++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 67e1f9c128df..fe3b3b4989dc 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -869,8 +869,7 @@ def analyze_func_def(self, defn: FuncDef) -> None: # type guards need to have a positional argument, to spec if ( result.type_guard - and ARG_POS not in result.arg_kinds[self.is_class_scope() :] - and not defn.is_static + and ARG_POS not in result.arg_kinds[self.is_class_scope() and not defn.is_static :] ): self.fail( "TypeGuard functions must have a positional argument", diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 39bcb091f09e..05ccd9fc16fe 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -602,7 +602,15 @@ def func(names: Tuple[str, ...]): from typing_extensions import TypeGuard class Z: - def typeguard(self, *, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument + def typeguard1(self, *, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument + ... + + @staticmethod + def typeguard2(x: object) -> TypeGuard[int]: + ... + + @staticmethod + def typeguard3(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument ... def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument From 9aa5b4048a75482f434d384cdf4b443e00e95c57 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 02:24:37 +0000 Subject: [PATCH 02/10] Appease black (?) --- mypy/semanal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index fe3b3b4989dc..15570bd2c766 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -867,9 +867,10 @@ def analyze_func_def(self, defn: FuncDef) -> None: assert isinstance(result, ProperType) if isinstance(result, CallableType): # type guards need to have a positional argument, to spec + skip_self = self.is_class_scope() and not defn.is_static if ( result.type_guard - and ARG_POS not in result.arg_kinds[self.is_class_scope() and not defn.is_static :] + and ARG_POS not in result.arg_kinds[skip_self:] ): self.fail( "TypeGuard functions must have a positional argument", From 96182d256e80b1197e8fd2ebc2363068db8f4087 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 02:27:11 +0000 Subject: [PATCH 03/10] Appease flake8 --- mypy/semanal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 15570bd2c766..743aec11356b 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -867,7 +867,7 @@ def analyze_func_def(self, defn: FuncDef) -> None: assert isinstance(result, ProperType) if isinstance(result, CallableType): # type guards need to have a positional argument, to spec - skip_self = self.is_class_scope() and not defn.is_static + skip_self = self.is_class_scope() and not defn.is_static if ( result.type_guard and ARG_POS not in result.arg_kinds[skip_self:] From e019308f5dc9aad9f9e3eda9b894cb0095e56595 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 02:29:36 +0000 Subject: [PATCH 04/10] Appease black one more time... --- mypy/semanal.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 743aec11356b..4935fc871ba0 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -868,10 +868,7 @@ def analyze_func_def(self, defn: FuncDef) -> None: if isinstance(result, CallableType): # type guards need to have a positional argument, to spec skip_self = self.is_class_scope() and not defn.is_static - if ( - result.type_guard - and ARG_POS not in result.arg_kinds[skip_self:] - ): + if result.type_guard and ARG_POS not in result.arg_kinds[skip_self:]: self.fail( "TypeGuard functions must have a positional argument", result, From 964a6e990ed278e80b29c1abd5daf43aa7db0214 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 02:44:30 +0000 Subject: [PATCH 05/10] Add a forgotten fixture --- test-data/unit/check-typeguard.test | 1 + 1 file changed, 1 insertion(+) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 05ccd9fc16fe..c3f8e34cf39d 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -616,6 +616,7 @@ class Z: def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument ... [builtins fixtures/tuple.pyi] +[builtins fixtures/classmethod.pyi] [case testTypeGuardWithKeywordArg] from typing_extensions import TypeGuard From a5c48e4a48b14f99cac20a4e98f837c6a09c17eb Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Fri, 24 Mar 2023 11:54:28 +0000 Subject: [PATCH 06/10] Move error message in test? --- test-data/unit/check-typeguard.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index c3f8e34cf39d..3ce244f1c82b 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -609,8 +609,8 @@ class Z: def typeguard2(x: object) -> TypeGuard[int]: ... - @staticmethod - def typeguard3(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument + @staticmethod # E: TypeGuard functions must have a positional argument + def typeguard3(*, x: object) -> TypeGuard[int]: ... def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument From b9ee1015e2a9f2798e6767713fa93fdbb7eb1cf7 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sat, 25 Mar 2023 10:43:44 +0900 Subject: [PATCH 07/10] Maybe fix the test for good --- test-data/unit/check-typeguard.test | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 3ce244f1c82b..8807d34c55e9 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -602,21 +602,31 @@ def func(names: Tuple[str, ...]): from typing_extensions import TypeGuard class Z: - def typeguard1(self, *, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument + def typeguard1(self, *, x: object) -> TypeGuard[int]: # line 4 ... @staticmethod def typeguard2(x: object) -> TypeGuard[int]: ... - @staticmethod # E: TypeGuard functions must have a positional argument + @staticmethod # line 11 def typeguard3(*, x: object) -> TypeGuard[int]: ... -def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument +def bad_typeguard(*, x: object) -> TypeGuard[int]: # line 15 ... + +# seperated outputs are due to a parsing difference (?) [builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] +[out] +main:4: error: TypeGuard functions must have a positional argument +main:11: error: TypeGuard functions must have a positional argument +main:15: error: TypeGuard functions must have a positional argument +[out version>=3.8] +main:4: error: TypeGuard functions must have a positional argument +main:12: error: TypeGuard functions must have a positional argument +main:15: error: TypeGuard functions must have a positional argument [case testTypeGuardWithKeywordArg] from typing_extensions import TypeGuard From 96fadf7e74e699142925243277e42a1b289ff8f4 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Sat, 25 Mar 2023 10:47:57 +0900 Subject: [PATCH 08/10] Update test-data/unit/check-typeguard.test Co-authored-by: Alex Waygood --- test-data/unit/check-typeguard.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 8807d34c55e9..1e3fdbd71a56 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -616,7 +616,7 @@ class Z: def bad_typeguard(*, x: object) -> TypeGuard[int]: # line 15 ... -# seperated outputs are due to a parsing difference (?) +# separated outputs are due to a parsing difference (?) [builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] [out] From 9270eabe1e28a09681cbeb24713a06ef035abf77 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sat, 25 Mar 2023 15:03:53 -0700 Subject: [PATCH 09/10] Update test-data/unit/check-typeguard.test --- test-data/unit/check-typeguard.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 1e3fdbd71a56..437921f367de 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -616,7 +616,7 @@ class Z: def bad_typeguard(*, x: object) -> TypeGuard[int]: # line 15 ... -# separated outputs are due to a parsing difference (?) +# In Python 3.8 the line number associated with FunctionDef nodes changed [builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] [out] From 93c95f03e04b5c21fa55cfbb98c98b80d542ec34 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sun, 26 Mar 2023 09:18:35 +0900 Subject: [PATCH 10/10] Remove some unnecessary fixtures --- test-data/unit/check-typeguard.test | 2 -- 1 file changed, 2 deletions(-) diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 437921f367de..a5ab35649320 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -617,7 +617,6 @@ def bad_typeguard(*, x: object) -> TypeGuard[int]: # line 15 ... # In Python 3.8 the line number associated with FunctionDef nodes changed -[builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] [out] main:4: error: TypeGuard functions must have a positional argument @@ -659,7 +658,6 @@ if Y().typeguard(x): reveal_type(x) # N: Revealed type is "builtins.int" if Y.typeguard(x): reveal_type(x) # N: Revealed type is "builtins.int" -[builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] [case testTypeGuardKwargFollowingThroughOverloaded]