From 616fea2ed803776000620a630c8222749f449d55 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 2 Nov 2023 13:31:24 +0300 Subject: [PATCH 1/4] Improve error messages for `super` checks and add more tests --- mypy/message_registry.py | 4 ++-- test-data/unit/check-super.test | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/mypy/message_registry.py b/mypy/message_registry.py index dc46eb5033909..21db8847aa639 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -206,10 +206,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage: ) TARGET_CLASS_HAS_NO_BASE_CLASS: Final = ErrorMessage("Target class has no base class") SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED: Final = ErrorMessage( - "super() outside of a method is not supported" + '"super" outside of a method is not supported' ) SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED: Final = ErrorMessage( - "super() requires one or more positional arguments in enclosing function" + '"super" requires one or more positional arguments in enclosing function' ) # Self-type diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 48a0a0250ecf5..86289ea4e990f 100644 --- a/test-data/unit/check-super.test +++ b/test-data/unit/check-super.test @@ -280,8 +280,12 @@ class B(A): [case testSuperOutsideMethodNoCrash] -class C: - a = super().whatever # E: super() outside of a method is not supported +class A: + x = 1 +class B(A): pass +class C(B): + a = super().whatever # E: "super" outside of a method is not supported + b = super(B, B).x [case testSuperWithObjectClassAsFirstArgument] class A: @@ -366,13 +370,22 @@ class C(B): [case testSuperInMethodWithNoArguments] class A: def f(self) -> None: pass + @staticmethod + def st() -> int: + return 1 class B(A): def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument? - super().f() # E: super() requires one or more positional arguments in enclosing function + super().f() # E: "super" requires one or more positional arguments in enclosing function def h(self) -> None: def a() -> None: - super().f() # E: super() requires one or more positional arguments in enclosing function + super().f() # E: "super" requires one or more positional arguments in enclosing function + @staticmethod + def st() -> int: + reveal_type(super(B, B).st()) # N: Revealed type is "builtins.int" + super().st() # E: "super" requires one or more positional arguments in enclosing function + return 2 +[builtins fixtures/staticmethod.pyi] [case testSuperWithUnsupportedTypeObject] from typing import Type From ef4b51a4554074bf051fe9e97346a54c9229c706 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 2 Nov 2023 13:32:45 +0300 Subject: [PATCH 2/4] Fix test --- test-data/unit/check-super.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 86289ea4e990f..937296c78a520 100644 --- a/test-data/unit/check-super.test +++ b/test-data/unit/check-super.test @@ -284,8 +284,8 @@ class A: x = 1 class B(A): pass class C(B): - a = super().whatever # E: "super" outside of a method is not supported b = super(B, B).x + a = super().whatever # E: "super" outside of a method is not supported [case testSuperWithObjectClassAsFirstArgument] class A: From 0820f8fdb8149f610c4d7c9e257eaf5ecfda4f45 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 3 Nov 2023 10:36:11 +0300 Subject: [PATCH 3/4] Address review --- mypy/message_registry.py | 2 +- test-data/unit/check-super.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/message_registry.py b/mypy/message_registry.py index 21db8847aa639..2f795c68b0378 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -206,7 +206,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage: ) TARGET_CLASS_HAS_NO_BASE_CLASS: Final = ErrorMessage("Target class has no base class") SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED: Final = ErrorMessage( - '"super" outside of a method is not supported' + '"super()" outside of a method is not supported' ) SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED: Final = ErrorMessage( '"super" requires one or more positional arguments in enclosing function' diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 937296c78a520..29de49315321b 100644 --- a/test-data/unit/check-super.test +++ b/test-data/unit/check-super.test @@ -285,7 +285,7 @@ class A: class B(A): pass class C(B): b = super(B, B).x - a = super().whatever # E: "super" outside of a method is not supported + a = super().whatever # E: "super()" outside of a method is not supported [case testSuperWithObjectClassAsFirstArgument] class A: From 8219eebe6884c46ebd9bf1e93e44d50f04d917bb Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 3 Nov 2023 11:16:21 +0300 Subject: [PATCH 4/4] Address review --- mypy/message_registry.py | 2 +- test-data/unit/check-super.test | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mypy/message_registry.py b/mypy/message_registry.py index 2f795c68b0378..93581d5aca906 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -209,7 +209,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage: '"super()" outside of a method is not supported' ) SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED: Final = ErrorMessage( - '"super" requires one or more positional arguments in enclosing function' + '"super()" requires one or two positional arguments in enclosing function' ) # Self-type diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 29de49315321b..8816322a270af 100644 --- a/test-data/unit/check-super.test +++ b/test-data/unit/check-super.test @@ -376,14 +376,14 @@ class A: class B(A): def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument? - super().f() # E: "super" requires one or more positional arguments in enclosing function + super().f() # E: "super()" requires one or two positional arguments in enclosing function def h(self) -> None: def a() -> None: - super().f() # E: "super" requires one or more positional arguments in enclosing function + super().f() # E: "super()" requires one or two positional arguments in enclosing function @staticmethod def st() -> int: reveal_type(super(B, B).st()) # N: Revealed type is "builtins.int" - super().st() # E: "super" requires one or more positional arguments in enclosing function + super().st() # E: "super()" requires one or two positional arguments in enclosing function return 2 [builtins fixtures/staticmethod.pyi]