Skip to content

Commit f9bf422

Browse files
authored
Type setuptools/msvc.py dir methods and properties (#4755)
2 parents 32f1c2c + 3ef7997 commit f9bf422

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

setuptools/msvc.py

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
from more_itertools import unique_everseen
1919

20+
from ._path import StrPath
21+
from .compat import py310
22+
2023
import distutils.errors
2124

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

138-
def cross_dir(self, forcex86=False):
141+
def cross_dir(self, forcex86=False) -> str:
139142
r"""
140143
Cross platform specific subfolder.
141144
@@ -306,7 +309,7 @@ def microsoft(self, key, x86=False):
306309
node64 = '' if self.pi.current_is_x86() or x86 else 'Wow6432Node'
307310
return os.path.join('Software', node64, 'Microsoft', key)
308311

309-
def lookup(self, key, name):
312+
def lookup(self, key: str, name: str) -> str | None:
310313
"""
311314
Look for values in registry in Microsoft software registry.
312315
@@ -319,7 +322,7 @@ def lookup(self, key, name):
319322
320323
Return
321324
------
322-
str
325+
str | None
323326
value
324327
"""
325328
key_read = winreg.KEY_READ
@@ -366,7 +369,7 @@ class SystemInfo:
366369
ProgramFiles = environ.get('ProgramFiles', '')
367370
ProgramFilesx86 = environ.get('ProgramFiles(x86)', ProgramFiles)
368371

369-
def __init__(self, registry_info, vc_ver=None) -> None:
372+
def __init__(self, registry_info: RegistryInfo, vc_ver=None) -> None:
370373
self.ri = registry_info
371374
self.pi = self.ri.pi
372375

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

488491
@property
489-
def VSInstallDir(self):
492+
def VSInstallDir(self) -> str:
490493
"""
491494
Microsoft Visual Studio directory.
492495
@@ -504,7 +507,7 @@ def VSInstallDir(self):
504507
return self.ri.lookup(self.ri.vs, f'{self.vs_ver:0.1f}') or default
505508

506509
@property
507-
def VCInstallDir(self):
510+
def VCInstallDir(self) -> str:
508511
"""
509512
Microsoft Visual C++ directory.
510513
@@ -608,7 +611,7 @@ def WindowsSdkLastVersion(self):
608611
return self._use_last_dir_name(os.path.join(self.WindowsSdkDir, 'lib'))
609612

610613
@property
611-
def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # FIXME
614+
def WindowsSdkDir(self) -> str: # noqa: C901 # is too complex (12) # FIXME
612615
"""
613616
Microsoft Windows SDK directory.
614617
@@ -651,13 +654,13 @@ def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # F
651654
return sdkdir
652655

653656
@property
654-
def WindowsSDKExecutablePath(self):
657+
def WindowsSDKExecutablePath(self) -> str | None:
655658
"""
656659
Microsoft Windows SDK executable directory.
657660
658661
Return
659662
------
660-
str
663+
str | None
661664
path
662665
"""
663666
# Find WinSDK NetFx Tools registry dir name
@@ -688,7 +691,7 @@ def WindowsSDKExecutablePath(self):
688691
return None
689692

690693
@property
691-
def FSharpInstallDir(self):
694+
def FSharpInstallDir(self) -> str:
692695
"""
693696
Microsoft Visual F# directory.
694697
@@ -701,13 +704,13 @@ def FSharpInstallDir(self):
701704
return self.ri.lookup(path, 'productdir') or ''
702705

703706
@property
704-
def UniversalCRTSdkDir(self):
707+
def UniversalCRTSdkDir(self) -> str | None:
705708
"""
706709
Microsoft Universal CRT SDK directory.
707710
708711
Return
709712
------
710-
str
713+
str | None
711714
path
712715
"""
713716
# Set Kit Roots versions for specified MSVC++ version
@@ -717,12 +720,12 @@ def UniversalCRTSdkDir(self):
717720
for ver in vers:
718721
sdkdir = self.ri.lookup(self.ri.windows_kits_roots, f'kitsroot{ver}')
719722
if sdkdir:
720-
return sdkdir or ''
723+
return sdkdir
721724

722725
return None
723726

724727
@property
725-
def UniversalCRTSdkLastVersion(self):
728+
def UniversalCRTSdkLastVersion(self) -> str:
726729
"""
727730
Microsoft Universal C Runtime SDK last version.
728731
@@ -731,7 +734,11 @@ def UniversalCRTSdkLastVersion(self):
731734
str
732735
version
733736
"""
734-
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib'))
737+
try:
738+
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib')) # type: ignore[arg-type] # Expected TypeError
739+
except TypeError as ex:
740+
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
741+
raise
735742

736743
@property
737744
def NetFxSdkVersion(self):
@@ -751,16 +758,16 @@ def NetFxSdkVersion(self):
751758
)
752759

753760
@property
754-
def NetFxSdkDir(self):
761+
def NetFxSdkDir(self) -> str | None:
755762
"""
756763
Microsoft .NET Framework SDK directory.
757764
758765
Return
759766
------
760-
str
767+
str | None
761768
path
762769
"""
763-
sdkdir = ''
770+
sdkdir: str | None = ''
764771
for ver in self.NetFxSdkVersion:
765772
loc = os.path.join(self.ri.netfx_sdk, ver)
766773
sdkdir = self.ri.lookup(loc, 'kitsinstallationfolder')
@@ -769,7 +776,7 @@ def NetFxSdkDir(self):
769776
return sdkdir
770777

771778
@property
772-
def FrameworkDir32(self):
779+
def FrameworkDir32(self) -> str:
773780
"""
774781
Microsoft .NET Framework 32bit directory.
775782
@@ -785,7 +792,7 @@ def FrameworkDir32(self):
785792
return self.ri.lookup(self.ri.vc, 'frameworkdir32') or guess_fw
786793

787794
@property
788-
def FrameworkDir64(self):
795+
def FrameworkDir64(self) -> str:
789796
"""
790797
Microsoft .NET Framework 64bit directory.
791798
@@ -855,13 +862,13 @@ def _find_dot_net_versions(self, bits) -> tuple[str, ...]:
855862
return ()
856863

857864
@staticmethod
858-
def _use_last_dir_name(path, prefix=''):
865+
def _use_last_dir_name(path: StrPath, prefix: str = '') -> str:
859866
"""
860867
Return name of the last dir in path or '' if no dir found.
861868
862869
Parameters
863870
----------
864-
path: str
871+
path: StrPath
865872
Use dirs in this path
866873
prefix: str
867874
Use only dirs starting by this prefix
@@ -877,7 +884,7 @@ def _use_last_dir_name(path, prefix=''):
877884
if os.path.isdir(os.path.join(path, dir_name))
878885
and dir_name.startswith(prefix)
879886
)
880-
return next(matching_dirs, None) or ''
887+
return next(matching_dirs, '')
881888

882889

883890
class _EnvironmentDict(TypedDict):
@@ -1200,7 +1207,7 @@ def _sdk_tools(self):
12001207
yield self.si.WindowsSDKExecutablePath
12011208

12021209
@property
1203-
def _sdk_subdir(self):
1210+
def _sdk_subdir(self) -> str:
12041211
"""
12051212
Microsoft Windows SDK version subdir.
12061213
@@ -1345,7 +1352,7 @@ def HTMLHelpWorkshop(self):
13451352
return [os.path.join(self.si.ProgramFilesx86, 'HTML Help Workshop')]
13461353

13471354
@property
1348-
def UCRTLibraries(self):
1355+
def UCRTLibraries(self) -> list[str]:
13491356
"""
13501357
Microsoft Universal C Runtime SDK Libraries.
13511358
@@ -1358,12 +1365,16 @@ def UCRTLibraries(self):
13581365
return []
13591366

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

13651376
@property
1366-
def UCRTIncludes(self):
1377+
def UCRTIncludes(self) -> list[str]:
13671378
"""
13681379
Microsoft Universal C Runtime SDK Include.
13691380
@@ -1375,11 +1386,15 @@ def UCRTIncludes(self):
13751386
if self.vs_ver < 14.0:
13761387
return []
13771388

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

13811396
@property
1382-
def _ucrt_subdir(self):
1397+
def _ucrt_subdir(self) -> str:
13831398
"""
13841399
Microsoft Universal C Runtime SDK version subdir.
13851400

0 commit comments

Comments
 (0)