Skip to content

Commit

Permalink
Avoid issues with _build_ext being Any (#4599)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored Aug 27, 2024
1 parent 4c990b9 commit defa725
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
24 changes: 13 additions & 11 deletions setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from importlib.machinery import EXTENSION_SUFFIXES
from importlib.util import cache_from_source as _compiled_file_name
from pathlib import Path
from typing import Iterator
from typing import TYPE_CHECKING, Iterator

from setuptools.dist import Distribution
from setuptools.errors import BaseError
Expand All @@ -16,17 +16,19 @@
from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler, get_config_var

try:
# Attempt to use Cython for building extensions, if available
from Cython.Distutils.build_ext import ( # type: ignore[import-not-found] # Cython not installed on CI tests
build_ext as _build_ext,
)

# Additionally, assert that the compiler module will load
# also. Ref #1229.
__import__('Cython.Compiler.Main')
except ImportError:
if TYPE_CHECKING:
# Cython not installed on CI tests, causing _build_ext to be `Any`
from distutils.command.build_ext import build_ext as _build_ext
else:
try:
# Attempt to use Cython for building extensions, if available
from Cython.Distutils.build_ext import build_ext as _build_ext

# Additionally, assert that the compiler module will load
# also. Ref #1229.
__import__('Cython.Compiler.Main')
except ImportError:
from distutils.command.build_ext import build_ext as _build_ext

# make sure _config_vars is initialized
get_config_var("LDSHARED")
Expand Down
3 changes: 1 addition & 2 deletions setuptools/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ def get_build_ext_cmd(self, optional: bool, **opts):
"eggs.c": "#include missingheader.h\n",
".build": {"lib": {}, "tmp": {}},
}
path.build(files)
path.build(files) # type: ignore[arg-type] # jaraco/path#232
extension = Extension('spam.eggs', ['eggs.c'], optional=optional)
dist = Distribution(dict(ext_modules=[extension]))
dist.script_name = 'setup.py'
cmd = build_ext(dist)
# TODO: False-positive [attr-defined], raise upstream
vars(cmd).update(build_lib=".build/lib", build_temp=".build/tmp", **opts)
cmd.ensure_finalized()
return cmd
Expand Down

0 comments on commit defa725

Please sign in to comment.