-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Type setuptools/msvc.py dir methods and properties #4755
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
Type setuptools/msvc.py dir methods and properties #4755
Conversation
setuptools/msvc.py
Outdated
return sdkdir or '' | ||
return sdkdir | ||
|
||
return None | ||
return '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one I'm not entirely certain of, and does change the possible return types.
dir-related properties of this class are a mixed bag on whether they return None
or ''
as a fallthrough case. And are almost never documented as returning None (which is fixed in this PR).
However all other properties that can return None
are checked for truthiness when used, this one isn't. (hence it was causing issues for #4744)
The redundant or ''
makes me think this may have been intended to not return None
. But the true intention is unclear to me.
After further research, it was auto-added here as an explicit return: https://github.com/pypa/setuptools/pull/4169/files#diff-2f10e0b183f00587feb575c7716d38f4951d4f7ded5b714396e96066ae7254bdR953
Now I'm pretty sure that the original intention was to return a ''
by default. But that means maybe other methods as well ?
Anyway, if you think this should return None
by default, I'll add checks to the methods that use this property instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for having a look on this.
It is a complicated decision. For the sake of backwards compatibility I think a safe approach would be to stick with the observed behaviour instead of the original intention?
Even if return None
was a late automatic addition, the original function could still returning None
under certain circumstances isn't it? So it may be safer for us to accept it can return None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8ff634c
(#4755) Shows the diff letting this return None
.
Same resolved TypeError
in UniversalCRTSdkLastVersion
, UCRTLibraries
, UCRTIncludes
and their caller when UniversalCRTSdkDir
returns None
.
If you want these to also keep the exact same exception raising behaviour, I can raise an explicit TypeError
in them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. I think it may make sense to keep the behaviour.
Do the type checkers allow something like the following?
try:
...
except TypeError as ex:
py310.add_note(ex, "Cannot find UniversalCRTSdkDir")
raise
(py310
=> from .compat import py310
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python's type system does not propagate possible exceptions to be raised.
No type-checkers that I know of special-cases try-except either.
So no, type-checkers, won't know to silence an issue if you catch a TypeError
.
It'd look more like this:
if (UniversalCRTSdkDir := self.UniversalCRTSdkDir) is None:
raise TypeError("Cannot find UniversalCRTSdkDir")
return self._use_last_dir_name(os.path.join(UniversalCRTSdkDir, 'lib'))
or
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
without the additional note:
return self._use_last_dir_name(
# Let TypeError bubble up when UniversalCRTSdkDir is None for backwards compatibility
os.path.join(self.UniversalCRTSdkDir, 'lib') # type: ignore[arg-type]
)
All 3 options above preserve the behaviour of raising a TypeError because of a None
UniversalCRTSdkDir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am more inclined towards the 2nd option for being the balance between leaving things as they are and providing debugging information if necessary.
5ab8346
to
6ad33ba
Compare
0fa9594
to
9d79a59
Compare
9d79a59
to
2185ae9
Compare
2185ae9
to
7fc946a
Compare
7fc946a
to
1e0fa31
Compare
50880f0
to
8ff634c
Compare
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') |
There was a problem hiding this comment.
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)
106287a
to
d971dd7
Compare
d971dd7
to
0510646
Compare
Co-authored-by: Anderson Bravalheri <andersonbravalheri+github@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much!
Summary of changes
Extracted from #4744 with additional fixes
Pull Request Checklist
newsfragments/
.(See documentation for details)