Skip to content
Merged
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
14 changes: 10 additions & 4 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,17 @@ def populate_non_ext_bases(
name = "_NamedTuple"
base = builder.get_module_attr("typing", name, cdef.line)
else:
cls_module = cls.fullname.rsplit(".", 1)[0]
if cls_module == builder.current_module:
base = builder.load_global_str(cls.name, cdef.line)
# The base may be a nested class such as "mod.Outer.Inner".
attrs = cls.fullname[len(cls.module_name) + 1 :].split(".")
if cls.module_name == builder.current_module:
base = builder.load_global_str(attrs[0], cdef.line)
else:
base = builder.load_module_attr_by_fullname(cls.fullname, cdef.line)
# The module may not have been imported yet, for example if the base class
# was imported through a re-export, or if the module is only imported in
# a function body, so import it if needed.
base = builder.get_module_attr(cls.module_name, attrs[0], cdef.line)
for attr in attrs[1:]:
base = builder.py_get_attr(base, attr, cdef.line)
bases.append(base)
if cls.fullname in MAGIC_TYPED_DICT_CLASSES:
# The remaining base classes are synthesized by mypy and should be ignored.
Expand Down
78 changes: 76 additions & 2 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1593,11 +1593,15 @@ def test_raise_non_native_exception_subclass_2() -> None:
assert False
assert x

[case testSubclassUnknownPythonBase]
# The base class is unknown during compilation, so it has type Any
[case testSubclassExternalPythonBase]
# Subclass Python classes defined outside the compiled module (some are unknown during
# compilation and have type Any)
import ctypes
from mypy_extensions import mypyc_attr
from typing import Any, Generic, TypeVar
from unchecked import Base, MetaBase, SecondBase, StrBase # type: ignore
import pkg
from pkg import FunctionImportedBase, Outer, ReExportedBase, StrEnum

T = TypeVar("T")

Expand Down Expand Up @@ -1724,6 +1728,49 @@ def test_generic_bases() -> None:
assert KnownGenericBase.__orig_bases__ == (Base, KnownGeneric[int])
assert KnownGenericBase(7).y == 7

# The bases are imported through re-exports (from modules other than where they are defined)
class ReExportedStrEnum(StrEnum):
value = "testing-value"

@mypyc_attr(native_class=False)
class FromReExport(ReExportedBase):
pass

@mypyc_attr(native_class=False)
class FromReExportedError(pkg.Error, ValueError):
pass

# ctypes.Structure is defined in _ctypes
class CtypesStructure(ctypes.Structure):
pass

def import_in_function() -> None:
# This doesn't run before the class below is created
import pkg.function_imported

@mypyc_attr(native_class=False)
class FromFunctionImportedModule(FunctionImportedBase):
pass

@mypyc_attr(native_class=False)
class FromNested(Outer.Inner):
pass

@mypyc_attr(native_class=False)
class FromNestedViaModule(pkg.Outer.Inner):
pass

def test_re_exported_base() -> None:
assert list(ReExportedStrEnum) == [ReExportedStrEnum.value]
assert ReExportedStrEnum.value == "testing-value"
assert isinstance(FromReExport(), ReExportedBase)
assert isinstance(FromReExportedError(), pkg.Error)
assert isinstance(CtypesStructure(), ctypes.Structure)
assert isinstance(FromFunctionImportedModule(), FunctionImportedBase)
assert isinstance(FromNested(), Outer.Inner)
assert FromNested().inner() == "inner"
assert isinstance(FromNestedViaModule(), Outer.Inner)

[file unchecked.py]
StrBase = str

Expand All @@ -1748,6 +1795,33 @@ class Base:
def marker(self):
return "base"

[file pkg/__init__.py]
from pkg.strenum import StrEnum
from pkg.base import ReExportedBase, Error, Outer
from pkg.function_imported import FunctionImportedBase

[file pkg/strenum.py]
import enum

class StrEnum(str, enum.Enum):
pass

[file pkg/base.py]
class ReExportedBase:
pass

class Error(Exception):
pass

class Outer:
class Inner:
def inner(self):
return "inner"

[file pkg/function_imported.py]
class FunctionImportedBase:
pass

[case testSubclassPy]
from b import B, V
class A(B):
Expand Down
Loading