Skip to content
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

[mypyc] Fix self-compilation on Python 3.12 #15582

Merged
merged 3 commits into from
Jul 3, 2023
Merged
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
38 changes: 24 additions & 14 deletions mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,31 @@
from mypyc.namegen import exported_name
from mypyc.options import CompilerOptions

if TYPE_CHECKING:
from distutils.core import Extension as _distutils_Extension
from typing_extensions import TypeAlias
if sys.version_info < (3, 12):
if TYPE_CHECKING:
from distutils.core import Extension as _distutils_Extension
from typing_extensions import TypeAlias

from setuptools import Extension as _setuptools_Extension
from setuptools import Extension as _setuptools_Extension

Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension]
Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension]


try:
# Import setuptools so that it monkey-patch overrides distutils
try:
# Import setuptools so that it monkey-patch overrides distutils
import setuptools
except ImportError:
pass
from distutils import ccompiler, sysconfig
else:
import setuptools
except ImportError:
if sys.version_info >= (3, 12):
# Raise on Python 3.12, since distutils will go away forever
raise
from distutils import ccompiler, sysconfig
from setuptools import Extension
from setuptools._distutils import (
ccompiler as _ccompiler, # type: ignore[attr-defined]
sysconfig as _sysconfig, # type: ignore[attr-defined]
)

ccompiler = _ccompiler
sysconfig = _sysconfig
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assignment as a workaround to avoid errors about unimported Anys.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened python/typeshed#10401 to help with this



def get_extension() -> type[Extension]:
Expand All @@ -65,11 +73,13 @@ def get_extension() -> type[Extension]:
use_setuptools = "setuptools" in sys.modules
extension_class: type[Extension]

if not use_setuptools:
if sys.version_info < (3, 12) and not use_setuptools:
import distutils.core

extension_class = distutils.core.Extension
else:
if not use_setuptools:
sys.exit("error: setuptools not installed")
extension_class = setuptools.Extension

return extension_class
Expand Down