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
More specifically, mypy will understand the use of sys.version_info and sys.platform checks
within if/elif/else statements.
and
As a special case, you can also use one of these checks in a top-level (unindented) assert; this makes mypy skip the rest of the file.
It would be nice if this kind of code would also typecheck on mac/linux.
Currently the option is to refactor such that you can define the entire function within an if sys.platform == "win32"
Just noting that the assert False form also works when filtering out single code branches within a larger function using a library or application specific check. For example:
_WINDOWS_BUILD=hasattr(os, "add_dll_directory")
defsome_function():
if_WINDOWS_BUILD:
ifsys.platform!="win32":
assertFalse# mypy allows Windows-only API usage hereelse:
ifsys.platform=="win32":
assertFalse# mypy allows *nix-only API usage here
@adam-grant-hendry Mypy only supports testing sys.platform using basic equality checks. If you rewrite that as sys.platform == 'win32' or sys.platform =='cygwin', it will work as expected.
Activity
AlexWaygood commentedon Apr 4, 2022
I figured out a hacky workaround for this in #11909. Instead of having
assert sys.platform == "win32"at the top of the function, instead do this:ncoghlan commentedon Aug 27, 2024
Just noting that the
assert Falseform also works when filtering out single code branches within a larger function using a library or application specific check. For example:adam-grant-hendry commentedon Dec 8, 2024
Actually...this seems to break within
if/elsestatements too: gist, playgroundbrianschubert commentedon Dec 8, 2024
@adam-grant-hendry Mypy only supports testing
sys.platformusing basic equality checks. If you rewrite that assys.platform == 'win32' or sys.platform =='cygwin', it will work as expected.adam-grant-hendry commentedon Dec 8, 2024
Ah bummer…I kinda figured. Okay, I’ll just
#type: ignorethis one…switch platform test to work around mypy bugs
switch platform check to work around mypy bugs