You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Type ignore annotation used on operating system specific code #9242
I try to type check the following code using mypy. As there are specific operating system lines of code, I have to tell to mypy to ignore them.
defis_user_admin() ->bool:
try:
system=platform.system()
ifsystemin ['Darwin', 'Linux']:
returnos.getuid() ==0# type: ignore[attr-defined, no-any-return]ifsystem=='Windows':
returnctypes.windll.shell32.IsUserAnAdmin() ==1# type: ignore[attr-defined, no-any-return]exceptAttributeError:
logger.info(f"Failed to determine if current user is an admin on {system} operating system.")
raiseAdminStateUnknownException
As expected, types are ignored but I have the following error (which seems to be legit too) running the command python -m mypy --strict admin.py
Obviously I need those annotations cause we have developpers using Windows and other using OSX/Linux.
I try to use the --warn-unused-ignore parameter for the command (python -m mypy --strict admin.py --warn-unused-ignore) but unfortunatly that doesn't seems to work and the error is still an error (breaking my CI 😱 !).
If you rewrite your checks to use sys.platform, mypy will understand that. (I think there's already an open issue about recognising platform.system as well).
If you're unwilling to do that or unable to make it work, the part about --warn-unused-ignore is basically a dupe of #8823 or #8990. As described in those issues, there are workarounds involving using cast or if TYPECHECKING.
I'm closing since I believe everything here is covered by existing issues, but feel free to reply if you need help!
Writing my function with sys.platform seems to work perfectly !
defis_user_admin() ->bool:
try:
ifsys.platform=='darwin'orsys.platform=='linux':
returnos.getuid() ==0ifsys.platform=='win32'orsys.platform=='cygwin':
returnctypes.windll.shell32.IsUserAnAdmin() ==1exceptAttributeError:
logger.info(f"Failed to determine if current user is an admin on {sys.platform} operating system.")
raiseAdminStateUnknownException
Thanks a lot for your help ! Really like mypy, you're doing a great job ;)
Activity
hauntsaninja commentedon Aug 1, 2020
If you rewrite your checks to use
sys.platform, mypy will understand that. (I think there's already an open issue about recognisingplatform.systemas well).If you're unwilling to do that or unable to make it work, the part about
--warn-unused-ignoreis basically a dupe of #8823 or #8990. As described in those issues, there are workarounds involving usingcastorif TYPECHECKING.I'm closing since I believe everything here is covered by existing issues, but feel free to reply if you need help!
ChamNouki commentedon Aug 3, 2020
Writing my function with sys.platform seems to work perfectly !
Thanks a lot for your help ! Really like mypy, you're doing a great job ;)