Skip to content

Commit 0510646

Browse files
committed
Type setuptools/msvc.py dir methods and properties
1 parent 32f1c2c commit 0510646

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

setuptools/msvc.py

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

1818
from more_itertools import unique_everseen
1919

20+
from setuptools.compat import py310
21+
22+
from ._path import StrPath
23+
2024
import distutils.errors
2125

2226
if TYPE_CHECKING:
@@ -135,7 +139,7 @@ def target_dir(self, hidex86=False, x64=False) -> str:
135139
else rf'\{self.target_cpu}'
136140
)
137141

138-
def cross_dir(self, forcex86=False):
142+
def cross_dir(self, forcex86=False) -> str:
139143
r"""
140144
Cross platform specific subfolder.
141145
@@ -306,7 +310,7 @@ def microsoft(self, key, x86=False):
306310
node64 = '' if self.pi.current_is_x86() or x86 else 'Wow6432Node'
307311
return os.path.join('Software', node64, 'Microsoft', key)
308312

309-
def lookup(self, key, name):
313+
def lookup(self, key: str, name: str) -> str | None:
310314
"""
311315
Look for values in registry in Microsoft software registry.
312316
@@ -319,7 +323,7 @@ def lookup(self, key, name):
319323
320324
Return
321325
------
322-
str
326+
str | None
323327
value
324328
"""
325329
key_read = winreg.KEY_READ
@@ -366,7 +370,7 @@ class SystemInfo:
366370
ProgramFiles = environ.get('ProgramFiles', '')
367371
ProgramFilesx86 = environ.get('ProgramFiles(x86)', ProgramFiles)
368372

369-
def __init__(self, registry_info, vc_ver=None) -> None:
373+
def __init__(self, registry_info: RegistryInfo, vc_ver=None) -> None:
370374
self.ri = registry_info
371375
self.pi = self.ri.pi
372376

@@ -486,7 +490,7 @@ def _as_float_version(version):
486490
return float('.'.join(version.split('.')[:2]))
487491

488492
@property
489-
def VSInstallDir(self):
493+
def VSInstallDir(self) -> str:
490494
"""
491495
Microsoft Visual Studio directory.
492496
@@ -504,7 +508,7 @@ def VSInstallDir(self):
504508
return self.ri.lookup(self.ri.vs, f'{self.vs_ver:0.1f}') or default
505509

506510
@property
507-
def VCInstallDir(self):
511+
def VCInstallDir(self) -> str:
508512
"""
509513
Microsoft Visual C++ directory.
510514
@@ -608,7 +612,7 @@ def WindowsSdkLastVersion(self):
608612
return self._use_last_dir_name(os.path.join(self.WindowsSdkDir, 'lib'))
609613

610614
@property
611-
def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # FIXME
615+
def WindowsSdkDir(self) -> str: # noqa: C901 # is too complex (12) # FIXME
612616
"""
613617
Microsoft Windows SDK directory.
614618
@@ -651,13 +655,13 @@ def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # F
651655
return sdkdir
652656

653657
@property
654-
def WindowsSDKExecutablePath(self):
658+
def WindowsSDKExecutablePath(self) -> str | None:
655659
"""
656660
Microsoft Windows SDK executable directory.
657661
658662
Return
659663
------
660-
str
664+
str | None
661665
path
662666
"""
663667
# Find WinSDK NetFx Tools registry dir name
@@ -688,7 +692,7 @@ def WindowsSDKExecutablePath(self):
688692
return None
689693

690694
@property
691-
def FSharpInstallDir(self):
695+
def FSharpInstallDir(self) -> str:
692696
"""
693697
Microsoft Visual F# directory.
694698
@@ -701,13 +705,13 @@ def FSharpInstallDir(self):
701705
return self.ri.lookup(path, 'productdir') or ''
702706

703707
@property
704-
def UniversalCRTSdkDir(self):
708+
def UniversalCRTSdkDir(self) -> str | None:
705709
"""
706710
Microsoft Universal CRT SDK directory.
707711
708712
Return
709713
------
710-
str
714+
str | None
711715
path
712716
"""
713717
# Set Kit Roots versions for specified MSVC++ version
@@ -717,12 +721,12 @@ def UniversalCRTSdkDir(self):
717721
for ver in vers:
718722
sdkdir = self.ri.lookup(self.ri.windows_kits_roots, f'kitsroot{ver}')
719723
if sdkdir:
720-
return sdkdir or ''
724+
return sdkdir
721725

722726
return None
723727

724728
@property
725-
def UniversalCRTSdkLastVersion(self):
729+
def UniversalCRTSdkLastVersion(self) -> str:
726730
"""
727731
Microsoft Universal C Runtime SDK last version.
728732
@@ -731,7 +735,11 @@ def UniversalCRTSdkLastVersion(self):
731735
str
732736
version
733737
"""
734-
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib'))
738+
try:
739+
return self._use_last_dir_name(os.path.join(self.UniversalCRTSdkDir, 'lib')) # type: ignore[arg-type] # Expected TypeError
740+
except TypeError as ex:
741+
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
742+
raise
735743

736744
@property
737745
def NetFxSdkVersion(self):
@@ -751,16 +759,16 @@ def NetFxSdkVersion(self):
751759
)
752760

753761
@property
754-
def NetFxSdkDir(self):
762+
def NetFxSdkDir(self) -> str | None:
755763
"""
756764
Microsoft .NET Framework SDK directory.
757765
758766
Return
759767
------
760-
str
768+
str | None
761769
path
762770
"""
763-
sdkdir = ''
771+
sdkdir: str | None = ''
764772
for ver in self.NetFxSdkVersion:
765773
loc = os.path.join(self.ri.netfx_sdk, ver)
766774
sdkdir = self.ri.lookup(loc, 'kitsinstallationfolder')
@@ -769,7 +777,7 @@ def NetFxSdkDir(self):
769777
return sdkdir
770778

771779
@property
772-
def FrameworkDir32(self):
780+
def FrameworkDir32(self) -> str:
773781
"""
774782
Microsoft .NET Framework 32bit directory.
775783
@@ -785,7 +793,7 @@ def FrameworkDir32(self):
785793
return self.ri.lookup(self.ri.vc, 'frameworkdir32') or guess_fw
786794

787795
@property
788-
def FrameworkDir64(self):
796+
def FrameworkDir64(self) -> str:
789797
"""
790798
Microsoft .NET Framework 64bit directory.
791799
@@ -855,13 +863,13 @@ def _find_dot_net_versions(self, bits) -> tuple[str, ...]:
855863
return ()
856864

857865
@staticmethod
858-
def _use_last_dir_name(path, prefix=''):
866+
def _use_last_dir_name(path: StrPath, prefix: str = '') -> str:
859867
"""
860868
Return name of the last dir in path or '' if no dir found.
861869
862870
Parameters
863871
----------
864-
path: str
872+
path: StrPath
865873
Use dirs in this path
866874
prefix: str
867875
Use only dirs starting by this prefix
@@ -877,7 +885,7 @@ def _use_last_dir_name(path, prefix=''):
877885
if os.path.isdir(os.path.join(path, dir_name))
878886
and dir_name.startswith(prefix)
879887
)
880-
return next(matching_dirs, None) or ''
888+
return next(matching_dirs, '')
881889

882890

883891
class _EnvironmentDict(TypedDict):
@@ -1200,7 +1208,7 @@ def _sdk_tools(self):
12001208
yield self.si.WindowsSDKExecutablePath
12011209

12021210
@property
1203-
def _sdk_subdir(self):
1211+
def _sdk_subdir(self) -> str:
12041212
"""
12051213
Microsoft Windows SDK version subdir.
12061214
@@ -1345,7 +1353,7 @@ def HTMLHelpWorkshop(self):
13451353
return [os.path.join(self.si.ProgramFilesx86, 'HTML Help Workshop')]
13461354

13471355
@property
1348-
def UCRTLibraries(self):
1356+
def UCRTLibraries(self) -> list[str]:
13491357
"""
13501358
Microsoft Universal C Runtime SDK Libraries.
13511359
@@ -1358,12 +1366,16 @@ def UCRTLibraries(self):
13581366
return []
13591367

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

13651377
@property
1366-
def UCRTIncludes(self):
1378+
def UCRTIncludes(self) -> list[str]:
13671379
"""
13681380
Microsoft Universal C Runtime SDK Include.
13691381
@@ -1375,11 +1387,15 @@ def UCRTIncludes(self):
13751387
if self.vs_ver < 14.0:
13761388
return []
13771389

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

13811397
@property
1382-
def _ucrt_subdir(self):
1398+
def _ucrt_subdir(self) -> str:
13831399
"""
13841400
Microsoft Universal C Runtime SDK version subdir.
13851401

0 commit comments

Comments
 (0)