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

Avoid type issues with _build_ext being Any #4599

Merged
merged 1 commit into from
Aug 27, 2024
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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
Loading