diff --git a/mypy/test/testpep561.py b/mypy/test/testpep561.py index 95c7ca2acfee5..88220a767c9f0 100644 --- a/mypy/test/testpep561.py +++ b/mypy/test/testpep561.py @@ -32,6 +32,11 @@ alpha_func(2) """ +C_EXT_PROGRAM = """ +from typedpkg_c_ext.foo import speak +from typedpkg_c_ext.hello import helloworld +""" + class NamespaceProgramImportStyle(Enum): from_import = """\ @@ -166,12 +171,14 @@ def setUp(self) -> None: create_namespace_program_source(NamespaceProgramImportStyle.from_import)) self.regular_import_namespace_example_program = ExampleProgram( create_namespace_program_source(NamespaceProgramImportStyle.from_import)) + self.c_ext_example_program = ExampleProgram(C_EXT_PROGRAM) def tearDown(self) -> None: self.simple_example_program.cleanup() self.from_namespace_example_program.cleanup() self.import_as_namespace_example_program.cleanup() self.regular_import_namespace_example_program.cleanup() + self.c_ext_example_program.cleanup() def test_get_pkg_dirs(self) -> None: """Check that get_package_dirs works.""" @@ -302,6 +309,17 @@ def test_nested_and_namespace_regular_import(self) -> None: venv_dir=venv_dir, ) + def test_c_ext_from_import(self) -> None: + self.c_ext_example_program.init() + with self.virtualenv() as venv: + venv_dir, python_executable = venv + self.install_package('typedpkg_c_ext', python_executable) + self.c_ext_example_program.check_mypy_run( + python_executable, + [], + venv_dir=venv_dir, + ) + if __name__ == '__main__': main() diff --git a/test-data/packages/typedpkg_c_ext/hello.c b/test-data/packages/typedpkg_c_ext/hello.c new file mode 100644 index 0000000000000..5db4167646204 --- /dev/null +++ b/test-data/packages/typedpkg_c_ext/hello.c @@ -0,0 +1,31 @@ +#include + +// Function 1: A simple 'hello world' function +static PyObject* helloworld(PyObject* self, PyObject* args) +{ + printf("Hello World\n"); + return Py_None; +} + +// Our Module's Function Definition struct +// We require this `NULL` to signal the end of our method +// definition +static PyMethodDef myMethods[] = { + { "helloworld", helloworld, METH_NOARGS, "Prints Hello World" }, + { NULL, NULL, 0, NULL } +}; + +// Our Module Definition struct +static struct PyModuleDef myModule = { + PyModuleDef_HEAD_INIT, + "hello", + "Test Module", + -1, + myMethods +}; + +// Initializes our module using our above struct +PyMODINIT_FUNC PyInit_hello(void) +{ + return PyModule_Create(&myModule); +} diff --git a/test-data/packages/typedpkg_c_ext/setup.py b/test-data/packages/typedpkg_c_ext/setup.py new file mode 100644 index 0000000000000..152da74426dbc --- /dev/null +++ b/test-data/packages/typedpkg_c_ext/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup, Extension + +setup( + name='typedpkg_c_ext', + version='1.0', + packages=['typedpkg_c_ext'], + ext_modules=[Extension('typedpkg_c_ext.hello', ['hello.c'])], + zip_safe=False, + package_data={ + 'typedpkg_c_ext': ['py.typed'] + }, +) diff --git a/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/__init__.py b/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/foo.py b/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/foo.py new file mode 100644 index 0000000000000..782e77406b210 --- /dev/null +++ b/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/foo.py @@ -0,0 +1,5 @@ +from . import hello + + +def speak() -> None: + hello.helloworld() diff --git a/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/py.typed b/test-data/packages/typedpkg_c_ext/typedpkg_c_ext/py.typed new file mode 100644 index 0000000000000..e69de29bb2d1d