Skip to content

Added module path output for the supertype in case of an error #19113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
get_proper_types,
)
from mypy.typetraverser import TypeTraverserVisitor
from mypy.util import plural_s, unmangle
from mypy.util import get_qualified_name, plural_s, unmangle

TYPES_FOR_UNIMPORTED_HINTS: Final = {
"typing.Any",
Expand Down Expand Up @@ -1379,7 +1379,7 @@ def return_type_incompatible_with_supertype(
)

def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
target = f'supertype "{supertype}"'
target = f'supertype "{get_qualified_name(supertype)}"'
if name_in_super != name:
target = f'"{name_in_super}" of {target}'
return target
Expand Down
33 changes: 33 additions & 0 deletions mypy/test/test_signature_incompatible_with_supertype_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from unittest import TestCase

from mypy.util import get_qualified_name


class TestClassA:
pass


class TestClassB:
pass


class TestGetQualifiedName(TestCase):
def test_existing_class_in_current_module(self):
result = get_qualified_name("TestClassA")
expected = f"{__name__}.TestClassA"
self.assertEqual(result, expected)

def test_existing_class_in_current_module_another(self):
result = get_qualified_name("TestClassB")
expected = f"{__name__}.TestClassB"
self.assertEqual(result, expected)

def test_non_existing_class(self):
result = get_qualified_name("NonExistentClass")
self.assertEqual(result, "NonExistentClass")

def test_empty_class_name(self):
result = get_qualified_name("")
self.assertEqual(result, "")
12 changes: 12 additions & 0 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import hashlib
import inspect
import io
import json
import os
Expand Down Expand Up @@ -942,3 +943,14 @@ def json_loads(data: bytes) -> Any:
if orjson is not None:
return orjson.loads(data)
return json.loads(data)


def get_qualified_name(class_name: str) -> str:
for module_name, module in sys.modules.items():
if module is None:
continue
if hasattr(module, class_name):
cls = getattr(module, class_name)
if inspect.isclass(cls):
return f"{module.__name__}.{class_name}"
return class_name
Loading