From e938270ab2aded46bffff2c487f81a80ceba1826 Mon Sep 17 00:00:00 2001 From: Paul Wankadia Date: Sun, 9 Jun 2024 15:38:18 +0000 Subject: [PATCH] More setting up to run mypy as per #496. We need `re2` to be a package, not a module, because it appears that modules can't have `.pyi` files, so munge the module into a package. Change-Id: I1a268875743390c32c0fb9cd58f6d83a670ce928 --- python/setup.py | 58 ++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/python/setup.py b/python/setup.py index c65aefa1d..a12096e2c 100644 --- a/python/setup.py +++ b/python/setup.py @@ -3,9 +3,11 @@ # license that can be found in the LICENSE file. import os +import re import setuptools import setuptools.command.build_ext import shutil +import tempfile long_description = r"""A drop-in replacement for the re module. @@ -103,25 +105,37 @@ def include_dirs(): extra_compile_args=['-fvisibility=hidden'], ) -setuptools.setup( - name='google-re2', - version='1.1.20240601', - description='RE2 Python bindings', - long_description=long_description, - long_description_content_type='text/plain', - author='The RE2 Authors', - author_email='re2-dev@googlegroups.com', - url='https://github.com/google/re2', - py_modules=['re2'], - ext_modules=[ext_module], - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Programming Language :: C++', - 'Programming Language :: Python :: 3.8', - ], - options=options(), - cmdclass={'build_ext': BuildExt}, - python_requires='~=3.8', -) +with tempfile.TemporaryDirectory(dir='.') as tmpdir: + # We need `re2` to be a package, not a module, because it appears that + # modules can't have `.pyi` files, so munge the module into a package. + with open('re2.py', 'r') as file: + contents = file.read() + contents = re.sub(r'^(?=import _)', 'from . ', contents, flags=re.MULTILINE) + with open(f'{tmpdir}/__init__.py', 'x') as file: + file.write(contents) + # TODO(junyer): `.pyi` files as per https://github.com/google/re2/issues/496. + + setuptools.setup( + name='google-re2', + version='1.1.20240601', + description='RE2 Python bindings', + long_description=long_description, + long_description_content_type='text/plain', + author='The RE2 Authors', + author_email='re2-dev@googlegroups.com', + url='https://github.com/google/re2', + packages=['re2'], + ext_package='re2', + ext_modules=[ext_module], + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Programming Language :: C++', + 'Programming Language :: Python :: 3.8', + ], + options=options(), + cmdclass={'build_ext': BuildExt}, + package_dir={'re2': tmpdir}, + python_requires='~=3.8', + )