Skip to content

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

Closed

Description

@evandrocoan

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

Activity

  1. JelleZijlstra commented on Mar 19, 2020

    @JelleZijlstra
    Member

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

  2. evandrocoan commented on Mar 19, 2020

    @evandrocoan
    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.

  3. JukkaL commented on Mar 20, 2020

    @JukkaL
    Collaborator

    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.

  4. ErikBjare commented on Jul 25, 2020

    @ErikBjare

    @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()
  5. JelleZijlstra commented on Jul 26, 2020

    @JelleZijlstra
    Member

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

  6. hauntsaninja commented on Aug 7, 2020

    @hauntsaninja
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions