From 19444614fe34e75ae1b2a596524dd87d8ecd05af Mon Sep 17 00:00:00 2001 From: KotlinIsland <65446343+kotlinisland@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:07:56 +1000 Subject: [PATCH] remove stupid error about erased self --- mypy/checker.py | 13 +++++++++---- test-data/unit/check-based-misc.test | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 6ccbf5b4e..1e2d4a0e2 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1497,10 +1497,15 @@ def check_func_def( # the consistency check will be performed at call sites. msg = None elif typ.arg_names[i] in {"self", "cls"}: - msg = message_registry.ERASED_SELF_TYPE_NOT_SUPERTYPE.format( - erased.str_with_options(self.options), - ref_type.str_with_options(self.options), - ) + if mypy.options._based: + msg = None + else: + msg = ( + message_registry.ERASED_SELF_TYPE_NOT_SUPERTYPE.format( + erased.str_with_options(self.options), + ref_type.str_with_options(self.options), + ) + ) else: msg = message_registry.MISSING_OR_INVALID_SELF_TYPE if msg: diff --git a/test-data/unit/check-based-misc.test b/test-data/unit/check-based-misc.test index 1be0f1451..cc2ddf655 100644 --- a/test-data/unit/check-based-misc.test +++ b/test-data/unit/check-based-misc.test @@ -235,3 +235,13 @@ class B(A): \[[tool.mypy.overrides]] module="other" work_not_properly_function_names=true + + +[case testErasedSelfType] + +class A: + def f(self: B): + reveal_type(self) # N: Revealed type is "__main__.B" +class B(A): ... +A().f() # E: Invalid self argument "A" to attribute function "f" with type "def (self: B) -> None" [misc] +B().f() # no error