Skip to content
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

How can I make this # type: ignore platform specific? #8547

Closed
evandrocoan opened this issue Mar 16, 2020 · 6 comments
Closed

How can I make this # type: ignore platform specific? #8547

evandrocoan opened this issue Mar 16, 2020 · 6 comments

Comments

@evandrocoan
Copy link

evandrocoan commented Mar 16, 2020

The project I use, runs both on Windows and Linux. The code is all platform specific, but mypy keeps ignoring it:

File: anki/pylib/anki/utils.py
286: def call(argv: List[str], wait: bool = True, **kwargs) -> int:
287:     "Execute a command. If WAIT, return exit code."
288:     # ensure we don't open a separate window for forking process on windows
289:     if isWin:
290:         si = subprocess.STARTUPINFO()
291:         try:
292:             si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  1. If I add # type: ignore to lines 290 and 292 then, mypy works on Linux, but it fails on Windows with error: unused 'type: ignore' comment
  2. If I remove the # type: ignore from the lines 290 and 292 then, mypy works on Windows, but it fails on Linux with error: Module has no attribute "STARTUPINFO" [attr-defined].

The STARTUPINFO is not defined for Linux Implementations, but is is for Windows implementation. That is why both of them are both protected by a if isWin or if _mswindows on this project and on the subprocess module implementation:

File: F:/Python/lib/subprocess.py
164: if _mswindows:
165:     class STARTUPINFO:

How can I make this # type: ignore platform specific?

(pyenv) F:\anki>mypy --version
mypy 0.761
@JelleZijlstra
Copy link
Member

If you use a mypy-recognized platform check like if sys.platform == 'win32', mypy should behave as expected.

@evandrocoan
Copy link
Author

The isWin Anki uses on the if is this:

File: anki/pylib/anki/utils.py
323: isWin = sys.platform.startswith("win32")

The only way to mypy to work is replacing the if isWin by if sys.platform == 'win32'? This should be improved.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 20, 2020

Mypy supports what specified in PEP 484 (https://www.python.org/dev/peps/pep-0484/#version-and-platform-checking). We could perhaps extend this to cover more cases, but it's hard to decide where we should draw the line, since the platform checks could be arbitrarily creative.

@ErikBjare
Copy link

ErikBjare commented Jul 25, 2020

@JukkaL I'm having problems with a simple case like:

if platform.system() == "Windows":
    something_platform_specific()  # type: ignore

There are some valid cases where you might want to do this, especially for Windows where sys.platform can be one of ["win32", "cygwin"].

Edit: I tried with this and it worked:

if platform.sys == "win32" or platform.sys == "cygwin":
    something_platform_specific()

@JelleZijlstra
Copy link
Member

There is an open PR #8461 to add support for platform.system(), but it looks like it has stalled.

@hauntsaninja
Copy link
Collaborator

Closing this as a dupe of other issues, basically this entire comment #9242 (comment) applies to this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants