Repository: python/mypy

Issue:
Running tests on Windows causes cmd windows to rapidly pop up and disappear
When I try running the tests on Windows, after a certain point the tests will suddenly start spawning and killing cmd windows rapidly. Each window shows up for a fraction of a second, and disappears. 

From one perspective, this is a non-issue: the temporary windows always disappear and the tests appear to finish and complete successfully. On the other hand, it's sort of alarming: I ran tests and switched over to my browser, and had a mild moment of panic when these cmd windows suddenly starting showing up and stealing focus.

I'm assuming this related to the recent work we've been doing with the windows daemon code: I can repro this fairly quickly by running `pytest -k Daemon` using both Powershell and the standard command prompt on Windows 10, and this behavior appears to have been introduced by https://github.com/python/mypy/commit/abd319ab80d476f14e96918d5615fa0ac5b41d75. 

I think ideally we'd stop making these windows appear and disappear. If that's not possible, we could instead maybe do one of the following:

1. Have the windows spawn in the background (so they don't steal focus).
2. Move the daemon-related tests out of `pytest-fast` or something so that users can opt-out of these tests by running `py -3 ./runtests.py pytest-fast` or something similar.
3. Document that this is expected behavior somewhere.



Discussion:

Resolving patch:
diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py
--- a/mypy/dmypy_server.py
+++ b/mypy/dmypy_server.py
@@ -42,21 +42,31 @@
 MEM_PROFILE = False  # type: Final  # If True, dump memory profile after initialization
 
 if sys.platform == 'win32':
+    from subprocess import STARTUPINFO
+
     def daemonize(options: Options,
                   timeout: Optional[int] = None,
                   log_file: Optional[str] = None) -> int:
         """Create the daemon process via "dmypy daemon" and pass options via command line
 
-        This uses the DETACHED_PROCESS flag to invoke the Server.
-        See https://docs.microsoft.com/en-us/windows/desktop/procthread/process-creation-flags
+        When creating the daemon grandchild, we create it in a new console, which is
+        started hidden. We cannot use DETACHED_PROCESS since it will cause console windows
+        to pop up when starting. See
+        https://github.com/python/cpython/pull/4150#issuecomment-340215696
+        for more on why we can't have nice things.
 
         It also pickles the options to be unpickled by mypy.
         """
         command = [sys.executable, '-m', 'mypy.dmypy', 'daemon']
         pickeled_options = pickle.dumps((options.snapshot(), timeout, log_file))
         command.append('--options-data="{}"'.format(base64.b64encode(pickeled_options).decode()))
+        info = STARTUPINFO(dwFlags=0x1,  # STARTF_USESHOWWINDOW aka use wShowWindow's value
+                           wShowWindow=0,  # SW_HIDE aka make the window invisible
+                           )
         try:
-            subprocess.Popen(command, creationflags=0x8)  # DETACHED_PROCESS
+            subprocess.Popen(command,
+                             creationflags=0x10,  # CREATE_NEW_CONSOLE
+                             startupinfo=info)
             return 0
         except subprocess.CalledProcessError as e:
             return e.returncode


Test patch: