Skip to content
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
73 changes: 44 additions & 29 deletions setuptools/msvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

from more_itertools import unique_everseen

from ._path import StrPath
from .compat import py310

import distutils.errors

if TYPE_CHECKING:
Expand Down Expand Up @@ -135,7 +138,7 @@ def target_dir(self, hidex86=False, x64=False) -> str:
else rf'\{self.target_cpu}'
)

def cross_dir(self, forcex86=False):
def cross_dir(self, forcex86=False) -> str:
r"""
Cross platform specific subfolder.

Expand Down Expand Up @@ -306,7 +309,7 @@ def microsoft(self, key, x86=False):
node64 = '' if self.pi.current_is_x86() or x86 else 'Wow6432Node'
return os.path.join('Software', node64, 'Microsoft', key)

def lookup(self, key, name):
def lookup(self, key: str, name: str) -> str | None:
"""
Look for values in registry in Microsoft software registry.

Expand All @@ -319,7 +322,7 @@ def lookup(self, key, name):

Return
------
str
str | None
value
"""
key_read = winreg.KEY_READ
Expand Down Expand Up @@ -366,7 +369,7 @@ class SystemInfo:
ProgramFiles = environ.get('ProgramFiles', '')
ProgramFilesx86 = environ.get('ProgramFiles(x86)', ProgramFiles)

def __init__(self, registry_info, vc_ver=None) -> None:
def __init__(self, registry_info: RegistryInfo, vc_ver=None) -> None:
self.ri = registry_info
self.pi = self.ri.pi

Expand Down Expand Up @@ -486,7 +489,7 @@ def _as_float_version(version):
return float('.'.join(version.split('.')[:2]))

@property
def VSInstallDir(self):
def VSInstallDir(self) -> str:
"""
Microsoft Visual Studio directory.

Expand All @@ -504,7 +507,7 @@ def VSInstallDir(self):
return self.ri.lookup(self.ri.vs, f'{self.vs_ver:0.1f}') or default

@property
def VCInstallDir(self):
def VCInstallDir(self) -> str:
"""
Microsoft Visual C++ directory.

Expand Down Expand Up @@ -608,7 +611,7 @@ def WindowsSdkLastVersion(self):
return self._use_last_dir_name(os.path.join(self.WindowsSdkDir, 'lib'))

@property
def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # FIXME
def WindowsSdkDir(self) -> str: # noqa: C901 # is too complex (12) # FIXME
"""
Microsoft Windows SDK directory.

Expand Down Expand Up @@ -651,13 +654,13 @@ def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # F
return sdkdir

@property
def WindowsSDKExecutablePath(self):
def WindowsSDKExecutablePath(self) -> str | None:
"""
Microsoft Windows SDK executable directory.

Return
------
str
str | None
path
"""
# Find WinSDK NetFx Tools registry dir name
Expand Down Expand Up @@ -688,7 +691,7 @@ def WindowsSDKExecutablePath(self):
return None

@property
def FSharpInstallDir(self):
def FSharpInstallDir(self) -> str:
"""
Microsoft Visual F# directory.

Expand All @@ -701,13 +704,13 @@ def FSharpInstallDir(self):
return self.ri.lookup(path, 'productdir') or ''

@property
def UniversalCRTSdkDir(self):
def UniversalCRTSdkDir(self) -> str | None:
"""
Microsoft Universal CRT SDK directory.

Return
------
str
str | None
path
"""
# Set Kit Roots versions for specified MSVC++ version
Expand All @@ -717,12 +720,12 @@ def UniversalCRTSdkDir(self):
for ver in vers:
sdkdir = self.ri.lookup(self.ri.windows_kits_roots, f'kitsroot{ver}')
if sdkdir:
return sdkdir or ''
return sdkdir

return None

@property
def UniversalCRTSdkLastVersion(self):
def UniversalCRTSdkLastVersion(self) -> str:
"""
Microsoft Universal C Runtime SDK last version.

Expand All @@ -731,7 +734,11 @@ def UniversalCRTSdkLastVersion(self):
str
version
"""
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib'))
try:
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib')) # type: ignore[arg-type] # Expected TypeError
except TypeError as ex:
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
raise

@property
def NetFxSdkVersion(self):
Expand All @@ -751,16 +758,16 @@ def NetFxSdkVersion(self):
)

@property
def NetFxSdkDir(self):
def NetFxSdkDir(self) -> str | None:
"""
Microsoft .NET Framework SDK directory.

Return
------
str
str | None
path
"""
sdkdir = ''
sdkdir: str | None = ''
for ver in self.NetFxSdkVersion:
loc = os.path.join(self.ri.netfx_sdk, ver)
sdkdir = self.ri.lookup(loc, 'kitsinstallationfolder')
Copy link
Contributor Author

@Avasam Avasam May 23, 2025

Choose a reason for hiding this comment

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

Because RegistryInfo.lookup can return None, and this method returns the last falsy result if all falsy. Then the return type is potentially None. (which will happen if no kitsinstallationfolder value is found)

Expand All @@ -769,7 +776,7 @@ def NetFxSdkDir(self):
return sdkdir

@property
def FrameworkDir32(self):
def FrameworkDir32(self) -> str:
"""
Microsoft .NET Framework 32bit directory.

Expand All @@ -785,7 +792,7 @@ def FrameworkDir32(self):
return self.ri.lookup(self.ri.vc, 'frameworkdir32') or guess_fw

@property
def FrameworkDir64(self):
def FrameworkDir64(self) -> str:
"""
Microsoft .NET Framework 64bit directory.

Expand Down Expand Up @@ -855,13 +862,13 @@ def _find_dot_net_versions(self, bits) -> tuple[str, ...]:
return ()

@staticmethod
def _use_last_dir_name(path, prefix=''):
def _use_last_dir_name(path: StrPath, prefix: str = '') -> str:
"""
Return name of the last dir in path or '' if no dir found.

Parameters
----------
path: str
path: StrPath
Use dirs in this path
prefix: str
Use only dirs starting by this prefix
Expand All @@ -877,7 +884,7 @@ def _use_last_dir_name(path, prefix=''):
if os.path.isdir(os.path.join(path, dir_name))
and dir_name.startswith(prefix)
)
return next(matching_dirs, None) or ''
return next(matching_dirs, '')


class _EnvironmentDict(TypedDict):
Expand Down Expand Up @@ -1200,7 +1207,7 @@ def _sdk_tools(self):
yield self.si.WindowsSDKExecutablePath

@property
def _sdk_subdir(self):
def _sdk_subdir(self) -> str:
"""
Microsoft Windows SDK version subdir.

Expand Down Expand Up @@ -1345,7 +1352,7 @@ def HTMLHelpWorkshop(self):
return [os.path.join(self.si.ProgramFilesx86, 'HTML Help Workshop')]

@property
def UCRTLibraries(self):
def UCRTLibraries(self) -> list[str]:
"""
Microsoft Universal C Runtime SDK Libraries.

Expand All @@ -1358,12 +1365,16 @@ def UCRTLibraries(self):
return []

arch_subdir = self.pi.target_dir(x64=True)
lib = os.path.join(self.si.UniversalCRTSdkDir, 'lib')
try:
lib = os.path.join(self.si.UniversalCRTSdkDir, 'lib') # type: ignore[arg-type] # Expected TypeError
except TypeError as ex:
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
raise
ucrtver = self._ucrt_subdir
return [os.path.join(lib, f'{ucrtver}ucrt{arch_subdir}')]

@property
def UCRTIncludes(self):
def UCRTIncludes(self) -> list[str]:
"""
Microsoft Universal C Runtime SDK Include.

Expand All @@ -1375,11 +1386,15 @@ def UCRTIncludes(self):
if self.vs_ver < 14.0:
return []

include = os.path.join(self.si.UniversalCRTSdkDir, 'include')
try:
include = os.path.join(self.si.UniversalCRTSdkDir, 'include') # type: ignore[arg-type] # Expected TypeError
except TypeError as ex:
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
raise
return [os.path.join(include, f'{self._ucrt_subdir}ucrt')]

@property
def _ucrt_subdir(self):
def _ucrt_subdir(self) -> str:
"""
Microsoft Universal C Runtime SDK version subdir.

Expand Down
Loading