
    *eA                       U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlZddlZddlZddlm Z m!Z!m"Z" ddl#m$Z$m%Z% dd	l&m'Z' dd
l(m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z?m@Z@ ddlAmBZB dZCdeDd<   e
jE        dk    rddl	mFZF 	 dOdPd#ZGndQdRd&ZH	 dOdPd'ZGd(ZIdeDd)<   dSd.ZJdTd0ZKeeLeLf         ZMd1eDd2<   eeM         ZNd1eDd3<   eeNeNf         ZOd1eDd4<    G d5 d6          ZPd7ZQdeDd8<   dUd:ZR	 dVdWdAZSdXdFZTdYdGZUdZdNZVdS )[zServer for mypy daemon mode.

This implements a daemon process which keeps useful state in memory
to enable fine-grained incremental reprocessing of changes.
    )annotationsN)redirect_stderrredirect_stdout)AbstractSetAnyCallableFinalListSequenceTuple)	TypeAlias)WriteToConnreceivesend)InvalidSourceListcreate_source_list)FileSystemCache)FileDataFileSystemWatcher)InspectionEngine)	IPCServer)BuildSourceFindModuleCacheSearchPathscompute_search_paths)Options)FineGrainedBuildManagerrefresh_suppressed_submodules)SuggestionEngineSuggestionFailure)reset_global_state)FancyFormattercount_stats)__version__Fr	   MEM_PROFILEwin32)STARTUPINFOoptionsr   status_filestrtimeout
int | Nonelog_file
str | Nonereturnintc                   t           j        ddd|dg}t          j        |                                           }|                    dt          j        |                                           d           |r|                    d|            |r|                    d|            t                      }d	|_
        d
|_        	 t          j        |d|           d
S # t          j        $ r}|j        cY d}~S d}~ww xY w)a  Create the daemon process via "dmypy daemon" and pass options via command line

        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.
        z-mz
mypy.dmypyz--status-filedaemonz--options-data=""z
--timeout=z--log-file=   r      )creationflagsstartupinfoN)sys
executablepickledumpssnapshotappendbase64	b64encodedecoder'   dwFlagswShowWindow
subprocessPopenCalledProcessError
returncode)r(   r)   r+   r-   commandpickled_optionsinfoes           1lib/python3.11/site-packages/mypy/dmypy_server.py	daemonizerL   -   s"    >4U]^ ,w'7'7'9'9::W&*:?*K*K*R*R*T*TWWWXXX 	3NN111222 	5NN333444}}	 WDdKKKK1, 	  	  	 <	 s   C   D /C;5D ;D funcCallable[[], None]c                   t           j                                         t           j                                         t	          j                    }|rKt	          j        |d          \  }}|dz  }|rt          d|           | S |dz	  }|rt          d|           |S 	 t	          j                     t	          j	        d           t	          j
        dt          j                  }t	          j        |d           t	          j        |d           t	          j        |d	           t	          j        |           t	          j                    }|rt	          j        d           |rpt          |d
d          xt           _        t           _        t           j                                        }t	          j        |d	           t	          j        |d            |              t	          j        d           dS # t	          j        d           w xY w)zArrange to call func() in a grandchild of the current process.

        Return 0 for success, exit status for failure, negative if
        subprocess killed by signal.
        r      zChild killed by signal   zChild exit status   z	/dev/nullr4      a)	bufferingN)r8   stdoutflushstderrosforkwaitpidprintsetsidumaskopenO_RDWRdup2close_exitfileno)rM   r-   pidnpidstssigdevnullfds           rK   _daemonize_cbrk   L   s    	

gii 
	
3**ID#*C .444t(C 0)3///J	IKKKHTNNNgk2955GGGQGGQGGQHW'))C  *.x*J*J*JJ
SZZ&&((AADFFF HQKKKKKBHQKKKKs   D>G2 2Hc                J    t          t          | ||          j        |          S )zRun the mypy daemon in a grandchild of the current process

        Return 0 for success, exit status for failure, negative if
        subprocess killed by signal.
        )rk   Serverserve)r(   r)   r+   r-   s       rK   rL   rL   y   s#     VG['BBH(SSS    dmypyCONNECTION_NAMEflags	list[str]allow_sourcesboolc                .   t           j                            dg| z   dd          \  }}|j        rt	          d           |j        rt	          d           d |_        |j        st          j        d           |j	        dvrt          j        d	           |S )
N-iFT)require_targetsserver_optionszRdmypy: Ignoring report generation settings. Start/restart cannot generate reports.zxdmypy: Ignoring report generation settings. Start/restart does not support --junit-xml. Pass it to check/recheck insteadz8dmypy: start/restart should not disable incremental mode)skiperrornormalz*dmypy: follow-imports=silent not supported)
mypymainprocess_optionsreport_dirsr\   	junit_xmlincrementalr8   exitfollow_imports)rr   rt   _r(   s       rK   process_start_optionsr      s    **	d +  JAw  dbccc ![	
 	
 	
 ! MKLLL%@@@=>>>Nro   modulec                ,    |                      d          S )z=Can we skip looking for newly unsuppressed imports to module?z
encodings.)
startswithr   s    rK   ignore_suppressed_importsr      s    
 \***ro   
_TypeAliasModulePathPairModulePathPairsChangesAndRemovalsc                     e Zd Zd]d^d
Zd_dZd`dZdadZd]dbdZdcdZdddZ	ded Z
	 	 dfdgd$Zdhd(Zd`d)Zdid+Zdjd,Zdkd-Z	 	 	 dldmd1Z	 dndod2Zdpd;Zdqd>ZdrdBZdsdDZ	 	 dtdudFZdvdGZdwdIZdxdJZdydKZ	 	 	 	 	 	 	 dzd{dVZd|d[Zdcd\ZdS )}rm   Nr(   r   r)   r*   r+   r,   r/   Nonec                   || _         |                                | _        || _        d| _        t
          j                            |          rt          j        |           t                      | _
        d|_        d|_        d|_        d|_        |j        rd|_        nt
          j        |_        d|_        || _        t+          t,          j        t,          j        |j                  | _        dS )z2Initialize the server with the desired mypy flags.NT)r(   r<   options_snapshotr+   fine_grained_managerrY   pathisfileunlinkr   fscacheraise_exceptionsr   fine_grained_incrementalshow_tracebackuse_fine_grained_cachecache_fine_grainedri   	cache_dirlocal_partial_typesr)   r"   r8   rV   rX   hide_error_codes	formatter)selfr(   r)   r+   s       rK   __init__zServer.__init__   s     ' 0 0 2 2DH!7>>+&& 	#Ik"""&((#' "+/(!%) 	+ *.G&& "
G '+#& (
CJ@XYYro   dict[str, str]c                n    | j         j        d          d| j         j        d          }| j         j        |dS )Nr   r   r4   )platformpython_version)r(   r   r   )r   
py_versions     rK   _response_metadatazServer._response_metadata   s=    3A6YY9TUV9WYY
 L1ZPPPro   c                   d}t          t          | j                  }t          j        }t          j        }	 t          | j        d          5 }t          j	        t          j                    |j        d|           |                    d           ddd           n# 1 swxY w Y   	 |5  t          |          }t          |d          t          _        t          |d          t          _        i }d|vrd	d
i}n|d         }t!          |t"                    sd	di}n|                    d          }	 |                     ||          }n# t(          $ rw t+          j        t          j                     }d	dd                    |          z   i}|                    |                                            d|d<   t7          ||            w xY wd|d<   	 |                    |                                            t7          ||           n# t8          $ r Y nw xY w|dk    r"t;                       t          j        d           ddd           n# 1 swxY w Y   # |t          _        |t          _        |dk    rt          j        | j                   	 |                                  n# t8          $ r Y nw xY wt          j                    }	|	d         r|	d         tB          urt+          j"        |	  w w w xY w)z2Serve requests, synchronously (no thread or fork).Nw)re   connection_name
TrV   rX   rG   r{   zNo command found in requestzCommand is not a stringzDaemon crashed!
 finalstopr   )#r   rq   r+   r8   rV   rX   r_   r)   jsondumprY   getpidr   writer   r   
isinstancer*   poprun_command	Exception	tracebackformat_exceptionexc_infojoinupdater   r   OSErrorr!   r   r   cleanup
SystemExitprint_exception)
r   rG   serverorig_stdoutorig_stderrfdataresptbr   s
             rK   rn   zServer.serve   s{    ?DL99jj7	5d&,, 	")++&BXYY[\]]]               $ $ $"6??D!,VX!>!>CJ!,VX!>!>CJ+-D ,, ')FG"&y/)'377 &$+-F#GDD&*hhy&9&9G	&'+'7'7'F'F#, & & &%.%?%P(/1Drwwr{{1R'S $D,C,C,E,E F F F04W $VT 2 2 2 %& %)DMD$;$;$=$=>>>VT****"   &((*,,,?$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $F %CJ$CJ &  	$*+++       |~~H{ 5x{*<<)84445<s   I ABI BI "B#I )BI0EIBGI7H	I	
HIH+II II II 8K3J('K3(
J52K34J55>K3rG   r   dict[str, object]c                    d|z   }t          | j        |d          }|dd| diS |dvr|d= |d=  || fi |}t          |t                    sJ |S )	z)Run a specific command from the registry.cmd_Nr{   zUnrecognized command ''>   runcheckrecheckis_ttyterminal_width)getattr	__class__r   dict)r   rG   r   keymethodrets         rK   r   zServer.run_command  s    wd33>@g@@@AA999N)*&&&&&Cc4(((((Jro   fswatcher_dump_filer.   c                H   i }|                     t                                 |r|t          | d          r| j                                        ni }t          j        |          }t          |d          5 }|                    |           ddd           n# 1 swxY w Y   |S )zReturn daemon status.	fswatcherr   N)	r   get_meminfohasattrr   dump_file_datar   r;   r_   r   )r   r   resr   sr   s         rK   
cmd_statuszServer.cmd_status#  s    !#

;==!!! 	6=dK6P6PX4>00222VXD
4  A)3// 1


              
s   5BBBc                8    t          j        | j                   i S )zStop daemon.)rY   r   r)   r   s    rK   cmd_stopzServer.cmd_stop/  s     		$"###	ro   versionargsSequence[str]export_typesru   r   r   r0   c           
        t          j                    }t          j                    }	 t          |          5  t          |          5  t          j                            dgt          |          z   dd| j        dt          j
                  \  }}	ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |	                    | j                  sddiS t          |k    rddiS | j        rR| j        j        }
|
j        }t          j                            |	|
j        t(          j        d	
          \  }}||k    rddiS nk# t,          $ r}dt/          |          ddcY d}~S d}~wt0          $ r9}|                                |                                |j        dcY d}~S d}~ww xY w|                     ||||          S )z6Check a list of files, triggering a restart if needed.rw   Tzmypy-daemon)rx   ry   r   programheaderNrestartzconfiguration changedzmypy version changed )extra_pluginszplugins changedr   rS   outerrstatus)ioStringIOr   r   r}   r~   r   listr   argparseSUPPRESScompare_stabler   r$   r   managerplugins_snapshotbuildload_pluginserrorsr8   rV   r   r*   r   getvaluecoder   )r   r   r   r   r   r   rX   rV   sourcesr(   r   start_plugins_snapshotr   current_plugins_snapshotr   rJ   s                   rK   cmd_runzServer.cmd_run8  s    	Z !(( 	 	$V,,  '+y'@'@d+(,'+ $ -'0 (A ( ($GW              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ))$*?@@ <!#:;;g%%!#9::( :3;)0)A&.2j.E.EW^SZr /F / /++ ,/EEE%'899  	= 	= 	=c#hh!<<<<<<<< 	Z 	Z 	Z!??,,V__5F5FRSRXYYYYYYYY	Zzz'<HHHs   E B3A	BB3B 	 B3#B 	$B3'E 3B77E :B7; E E +AE 
F-E'!F-'F-4.F("F-(F-filesc                    	 t          || j        | j                  }n*# t          $ r}dt	          |          ddcY d}~S d}~ww xY w|                     ||||          S )zCheck a list of files.r   rS   r   N)r   r(   r   r   r*   r   )r   r  r   r   r   r   r   s          rK   	cmd_checkzServer.cmd_checkc  sz    	=(dlKKGG  	= 	= 	=c#hh!<<<<<<<<	=zz'<HHHs    
AA A Aremovelist[str] | Noner   c                R   t          j                     }| j        sddiS | j        }|rt          |          fd|D             }|rfd |D             fd|D             }	 t	          || j        | j                  }	n*# t          $ r}
dt          |
          ddcY d	}
~
S d	}
~
ww xY w||	z   }t          j                     }| j        j	        }|
                    d
||z
  dd           | j        j        }| j        j        p|| j        _        |                                 s|                     ||||          }n||J |                     ||          }|                     ||||          }|                                  |                     |           || j        _        |S )zCheck the same list of files we checked most recently.

        If remove/update is given, they modify the previous list;
        if all are None, stat() is called for each file in the previous list.
        r{   z7Command 'recheck' is only valid after a 'check' commandc                4    g | ]}|j         	|j         v|S r   r   ).0r   removalss     rK   
<listcomp>z&Server.cmd_recheck.<locals>.<listcomp>  s,    OOOQQVOh8N8Nq8N8N8Nro   c                *    h | ]}|j         	|j         S r   r
  r  r   s     rK   	<setcomp>z%Server.cmd_recheck.<locals>.<setcomp>  s!    7777QV777ro   c                    g | ]}|v|	S r   r   )r  pknowns     rK   r  z&Server.cmd_recheck.<locals>.<listcomp>  s    9991!5..Q...ro   r   rS   r   Nz%fine-grained increment: cmd_recheck: .3fr   explicit_export_types)timer   previous_sourcessetr   r(   r   r   r*   r   logr   following_importsfine_grained_increment%fine_grained_increment_follow_importsincrement_outputflush_cachesupdate_stats)r   r   r   r   r  r   t0r   addedadded_sourcesr   t1r   old_export_typesmessagesr   r  r  s                   @@rK   cmd_recheckzServer.cmd_recheckm  s    Y[[( 	XVWW' 	P6{{HOOOO'OOOG 	.77W777E9999999EA 25$, U U$ A A A!#c((a@@@@@@@@A-GY[[+3JBGJJJJKKK<4$(L$=$M!%%'' 	22| 3  HH >fnnnAA| B  H ##Hgv~NN#$4!
s   $B   
B'
B"B'"B'r   list[BuildSource]dict[str, Any]c                   | j         j        }| j         j        p|| j         _        | j        s|                     |||          }n[|                                 s|                     ||          }n|                     ||          }|                     ||||          }|                                  | 	                    |           || j         _        |S )zCheck using fine-grained incremental mode.

        If is_tty is True format the output nicely with colors and summary line
        (unless disabled in self.options). Also pass the terminal_width to formatter.
        r  )
r(   r   r   initialize_fine_grainedr  r  r  r  r  r   )r   r   r   r   r   r%  r   r&  s           rK   r   zServer.check  s      <4$(L$=$M!( 		S..wOOCC))++ 66wVb6ccEE< F   '''6>RRC#$4!
ro   c                |    | j                                          | j        r| j                                         d S d S N)r   rW   r   flush_cacher   s    rK   r  zServer.flush_caches  sF    $ 	4%1133333	4 	4ro   r   c                z    | j         r3| j         j        }|                                 |j        |d<   i |_        d S d S )Nstats)r   r   
dump_statsr0  )r   r   r   s      rK   r   zServer.update_stats  sJ    $ 	/7G   "=CLGMMM		 	ro   c                "    | j         j        dk    S )zAre we following imports?r|   )r(   r   r   s    rK   r  zServer.following_imports  s     |*h66ro   c           
        t          | j                  | _        t          j                    }|                     |           t          j                    }	 t
          j                            || j        | j                  }n[# t
          j        j	        $ rD}d
                    d |j        D                       }|j        r|d}
}	nd|}
}	|	|
ddcY d }~S d }~ww xY w|j        }t          |          | _        t          |          }|                                 r/t#          | j        j        |          }|                     |           || _        |j        rt          j                    }| j        j                                        D ]a}|j        }||j        J | j                            |j        t3          t5          |j                  |j        |j                             b|                     |          \  }}||                     | j        j        tA                      | j        j!        j"                  z  }| j        j                                        D ]@}|#                                s*|j        J |$                    |j%        |j        f           At          j                    }| j        &                    ||          }|                                 r| '                    |          }t          j                    }| j        j!        (                    ||z
  ||z
  ||z
  ||z
  t          |          t          |          z              n| j                                         tR          rdd	l*m+}  |d
           tY          |          \  }}}|r|t          |          k     rdnd}| -                    ||||          }d
                    d |D                       d|dS )N)r   r(   r   r   c              3      K   | ]	}|d z   V  
dS r   Nr   r  s     rK   	<genexpr>z1Server.initialize_fine_grained.<locals>.<genexpr>  s&      ::!QX::::::ro   rS   r   )st_mtimest_sizehash)update_sources_time
build_timefind_changes_timefg_update_timefiles_changedr   )print_memory_profileF)run_gcr4   c              3      K   | ]	}|d z   V  
dS r5  r   r  s     rK   r6  z1Server.initialize_fine_grained.<locals>.<genexpr>  &      ::Aq4x::::::ro   ).r   r   r   r  update_sourcesr}   r   r(   r   CompileErrorr   r&  
use_stdoutr   r   lenr  find_all_sources_in_buildgraphr  
used_cachevaluesmetar   set_file_datar   floatmtimesizer9  find_changedfind_added_suppressedr  r   search_pathsis_freshr=   idr   r  	add_statsr%   mypy.memprofiler?  r#   pretty_messages)r   r   r   r   r!  r$  resultrJ   outputr   r   r&  original_sources_lent2staterK  changedremovedt3t4r?  __n_notesr   s                           rK   r+  zServer.initialize_fine_grained  s    +4<88Y[[G$$$Y[[	9Z%%gt|UYUa%bbFF{' 	9 	9 	9WW::qz:::::F| &!2SvSsa88888888	9 =$;F$C$C!"7||!!## 	)/0I0OQXYYG((( '
  .	*B 28??AA  z<z---,,JeDJ&7&7QUQZ[[[   
  $0099GWt11)/)1>  G 28??AA ; ;~~'' ; :111NNEHej#9:::B077IIH%%'' OEEgNNB%-77$&G7"$r'!Bw!'llS\\9 8     N''))) 	/<<<<<<  ....%h//GRA7S]]#:#:''2FP^__ww:::::::2QWXXXs   ,B C9CCCFr  rs   c                >   | j         J | j         j        }t          j                    }|0|.|                     |           |                     |          \  }}n|                     ||pg |pg           \  }}|rt          ||           ||                     | j         j        t                      |j
                  z  }t          ||j        |j                  |_
        t          j                    }	|                    d|	|z
  dd           | j                             ||          }
t          j                    }|                    d||	z
  dd           |                    |	|z
  ||	z
  t#          |          t#          |          z              || _        |
S )a  Perform a fine-grained type checking increment.

        If remove and update are None, determine changed paths by using
        fswatcher. Otherwise, assume that only these files have changes.

        Args:
            sources: sources passed on the command line
            remove: paths of files that have been removed
            update: paths of files that have been changed or created
            explicit_export_types: --export-type was passed in a check command
              (as opposite to being set in dmypy start)
        N&fine-grained increment: find_changed: r  r    fine-grained increment: update: )r<  r=  r>  )r   r   r  rC  rP  update_changedadd_all_sources_to_changedrQ  rH  r  rR  r   r(   data_dirr  r   rU  rF  r  )r   r   r  r   r  r   r!  r]  r^  r$  r&  r[  s               rK   r  zServer.fine_grained_increment  s   & (444+3Y[[>fn (((#0099GWW  $227FLb&,TVWWGW  	9 'w8884--%+SUUG4H
 
 	
  4GW_gN^__Y[[KR"WKKKKLLL,33GWEEY[[ErBwEEEEFFF 2g7g,,W5 	 	
 	
 	
 !(ro   c           	     0   t          j                     }| j        J | j        j        }j        }t	          |                                          }|                     |           | j                                        }t          ||j
        |j                  |_        t          j                     }|                    d||z
  dd           d |D             |                     |||          \  }	}
|rt          ||	           |                    |
                               |	g d          }|	                                }|r|                                }|d         |vr!|                     ||          }fd	|D             }|                     |||          \  }	}
|                     |
                               |	g d          }|                    |	           |t          j                     }dfd}t	          |                                          D ],\  }}t-          ||j        j        || j        |          }||}-t          j                     }	 |                     ||j                  }|snyd |D             }
|                    |
           |                     |
                               |g d          }|D ]'\  }}t-          ||j        || j        |          }||}(t          j                     }g }|D ]3}||vr|vr(||         j        }|J |                    ||f           4|r                    g |          }t9          |           t;          |          | _        |                     | j                   | j                                         t          j                     }|                    d||z
  dd           |                    ||z
  ||z
  ||z
  ||z
  ||z
             |S )z0Like fine_grained_increment, but follow imports.Nrd  r  r   c                    h | ]	}|j         
S r   r   r  sources     rK   r  z?Server.fine_grained_increment_follow_imports.<locals>.<setcomp>k  s    444&444ro   Tfollowedr   c                &    g | ]}|j         v|S r   r   )r  rl  seens     rK   r  z@Server.fine_grained_increment_follow_imports.<locals>.<listcomp>  s%    SSS6d9R9R9R9R9Rro   r   r*   r   r/   rs   c                :                         | |fgg d          S )NTrm  )r   )r   r   r   s     rK   refresh_filezBServer.fine_grained_increment_follow_imports.<locals>.refresh_file  s%    '../?d.SSSro   c                J    g | ] }t          |d          |d         d          !S )r4   r   Trm  )r   )r  mods     rK   r  z@Server.fine_grained_increment_follow_imports.<locals>.<listcomp>  s/    ```SVSVdCCC```ro   re  )r<  r=  refresh_suppressed_timefind_added_supressed_timecleanup_time)r   r*   r   r*   r/   rs   ) r  r   rH  r   r   keysrC  r   rP  r   r(   rh  rR  r  find_reachable_changed_modulesrg  extendr   copyr   direct_importsitemsr   r   depsr   rQ  r=   fix_module_depsrG  r  rU  )r   r   r  r!  rH  r   orig_moduleschanged_pathsr$  r]  	new_filesr&  worklistr   sources2r[  rr  	module_idr\  new_messagesr_  new_unsuppressedr   r`  	to_deletemodule_patht5r   rp  s                              @@rK   r  z,Server.fine_grained_increment_follow_importsU  s    Y[[ (444#8$*&.EJJLL))G$$$33553GW_gN^__Y[[KR"WKKKKLLL44G444 "@@UD-
 
 ! 	9&w888y!!! (..wT.JJ <<>> 	%\\^^Fay%%**6599H TSSSXSSSH!%!D!D%}" "GY 	***+227B2NNHOOG$$$  	%  Y[[	T 	T 	T 	T 	T 	T !%U[[]] 3 3 	( 	(Iu85:';'@%Wc L ''Y[[	,#99%wG[\\# ``O_```INN9%%%	***+223CRRV2WWH#3 , ,	4<t%9%>t|Ua     ++H	,  Y[[ 	% 	; 	;I%%$$#I.3"...  )[!9::: 	B+222yAAH 9% @ @D1222 	##%%%Y[[ErBwEEEEFFF 2g7$&G&(2gb 	 	
 	
 	
 ro   rootsrH  mypy.build.Graphrp  set[str]r  AbstractSet[str]/tuple[list[tuple[str, str]], list[BuildSource]]c                h   g }g }|                                 }|                    d |D                        |r|                                }|j        |vr/|                    |j                   |                    |           |j        |v r+|j        J |                    |j        |j        f           nt|j        |v rk||j                 }	|	j        D ]V}
|
|vrP|                    |
           |                    t          ||
         j        ||
         j	        d                     W|||fS )aj  Follow imports within graph from given sources until hitting changed modules.

        If we find a changed module, we can't continue following imports as the imports
        may have changed.

        Args:
            roots: modules where to start search from
            graph: module graph to use for the search
            seen: modules we've seen before that won't be visited (mutated here!!)
            changed_paths: which paths have changed (stop search here and return any found)

        Return (encountered reachable changed modules,
                unchanged files not in sources_set traversed).
        c              3  $   K   | ]}|j         V  d S r-  r   rk  s     rK   r6  z8Server.find_reachable_changed_modules.<locals>.<genexpr>  s$      99fFM999999ro   NTrm  )
r{  r   r   r   addr=   r   dependenciesr   rT  )r   r  rH  rp  r  r]  r  r  nxtr\  deps              rK   ry  z%Server.find_reachable_changed_modules  sL   * 	::<<99999999 	d,,..Cz%%$$$  %%%x=((x+++
CH56666u$$cj) - d dC$ E#JOU3Z]]a(b(b(bccc  	d 	!!ro   r   tuple[str, str]c                F    |d                  }fd|j         D             S )z9Return the direct imports of module not included in seen.r   c                J    g | ]}t          |         j        |d            S )Trm  )r   r   )r  r  rH  s     rK   r  z)Server.direct_imports.<locals>.<listcomp>  s.    ___SE#JOS4@@@___ro   )r  )r   r   rH  r\  s     ` rK   r|  zServer.direct_imports  s0     fQi ____EL^____ro   rR  r   list[tuple[str, str]]c                X   t                      }                                D ]}||j        z  }fd|D             }d |D             }t          ||| j                  }t          || j        | j                  }g }|D ]}	|	                    dd          d         }
|
|vr#|                    |	d          }t          |t                    rZ|	|vrV|                                 s|                    d          s}|                    |	|f           |                    |	           |S )	zFind suppressed modules that have been added (and not included in seen).

        Args:
            seen: reachable modules we've seen before (mutated here!!)

        Return suppressed, added modules.
        c                :    h | ]}|vt          |          |S r   )r   )r  r   rH  s     rK   r  z/Server.find_added_suppressed.<locals>.<setcomp>  s<     
 
 
U""+DV+L+L" """ro   c                F    h | ]}|                     d d          d         S ).r4   r   )split)r  r   s     rK   r  z/Server.find_added_suppressed.<locals>.<setcomp>  s+    IIIFLLa((+IIIro   r  r4   r   T)	fast_path.pyi)r  rJ  suppressed_set%filter_out_missing_top_level_packagesr   r   r(   r  find_moduler   r*   r  endswithr=   r  )r   rH  rp  rR  all_suppressedr\  packagesfinderfoundr   top_level_pkgrX  s    `          rK   rQ  zServer.find_added_suppressed  se    \\^^ 	3 	3Ee22NN

 
 
 
(
 
 
 JI.III8<QUQ]^^ !t|T\JJ$ 	! 	!F"LLa003MH,,''$'??F&#&& !6+=+=--// 8O8O ff-...   ro   r&  c                    |rdnd}|                      |t          |          ||          }d                    d |D                       d|dS )Nr4   r   r   c              3      K   | ]	}|d z   V  
dS r5  r   r  s     rK   r6  z*Server.increment_output.<locals>.<genexpr>5  rB  ro   r   )rW  rF  r   )r   r&  r   r   r   r   s         rK   r  zServer.increment_output0  s]     %A''#g,,WWww:::::::2QWXXXro   	n_sourcesc                     j         j        o|} j         j        o|}|r j                            ||          } j         j        rnd }t          |          \  }}	}
|r j                            ||
||          }n0|r|	t          |          k    r j        	                    ||          }|r||gz   }|r fd|D             }|S )N)fixed_terminal_width)	use_colorc                D    g | ]}j                             |          S r   )r   colorize)r  mr   s     rK   r  z*Server.pretty_messages.<locals>.<listcomp>Q  s)    EEEq//22EEEro   )
r(   color_outputprettyr   fit_in_terminalerror_summaryr#   format_errorrF  format_success)r   r&  r  r   r   r  	fit_widthsummaryn_errorsrb  n_filess   `          rK   rW  zServer.pretty_messages7  s    L-8&	L'2F	 	~55~ 6  H <% 	0"&G)4X)>)>&Hgw N.55gyI 6    NCMM!9!9.77	9MM 0#wi/ 	FEEEEHEEEHro   c                     d |D             }                                  r fd|D             } j                            |           d S )Nc                *    g | ]}|j         	|j         S r-  r
  rk  s     rK   r  z)Server.update_sources.<locals>.<listcomp>U  s!    NNNfk6M6M6M6Mro   c                H    g | ]}j                             |          |S r   )r   r   )r  r   r   s     rK   r  z)Server.update_sources.<locals>.<listcomp>X  s.    IIIdt|/B/B4/H/HITIIIro   )r  r   add_watched_paths)r   r   pathss   `  rK   rC  zServer.update_sourcesT  s`    NN7NNN!!## 	JIIIIeIIIE((/////ro   r   c                d    | j                             ||          }|                     ||          S r-  )r   rf  _find_changed)r   r   r  r   r  s        rK   rf  zServer.update_changed[  s1     55ffEE!!'=999ro   c                `    | j                                         }|                     ||          S r-  )r   rP  r  )r   r   r  s      rK   rP  zServer.find_changeda  s+    3355!!'=999ro   c                X  
 fd|D             }d |D             fd| j         D             }g }|D ])}|j        }|sJ |                    |j        |f           *d | j         D             t	          |          
|                    
fd|D                        d | j         D             }|D ]r}	|	j        sJ |	j        |v r^||	j                 |	j        k    rH|                    ||	j                 |	j        f           |                    |	j        |	j        f           s||fS )Nc                L    g | ] }|j         r|j         v |j        |j         f!S r   r   r   )r  rl  r  s     rK   r  z(Server._find_changed.<locals>.<listcomp>i  sE     
 
 
{
  &{m;; ]FK(;;;ro   c                    h | ]	}|j         
S r   r   rk  s     rK   r  z'Server._find_changed.<locals>.<setcomp>p  s    777V6=777ro   c                &    g | ]}|j         v|S r   r   )r  rl  moduless     rK   r  z(Server._find_changed.<locals>.<listcomp>q  s&    ^^^fV]A]A]6A]A]A]ro   c                    h | ]	}|j         
S r   r   rk  s     rK   r  z'Server._find_changed.<locals>.<setcomp>{  s    NNNfFMNNNro   c                l    g | ]0}|j         r'|j        vr|j        |j         fv"|j        |j         f1S r   r  )r  rl  changed_setprevious_moduless     rK   r  z(Server._find_changed.<locals>.<listcomp>~  s]       ; M)999]FK0CC	 , DCCro   c                (    i | ]}|j         |j        S r   r  r  s     rK   
<dictcomp>z(Server._find_changed.<locals>.<dictcomp>  s    @@@Q@@@ro   )r  r   r=   r   r  rz  )r   r   r  r]  omittedr^  rl  r   lastr   r  r  r  s     `       @@@rK   r  zServer._find_changede  s   
 
 
 
!
 
 
 87w777^^^^(=^^^ 	2 	2F;DKKKNNFM401111
 ON8MNNN'll    %  	
 	
 	
 A@$*?@@@ 	3 	3A6MMMv~~$qv,!(":":QVaf5666!&1222ro   r   showlocation	verbositylimitinclude_spaninclude_kindinclude_object_attrsunion_attrsforce_reloadc
           
        | j         sddiS t          | j         |||||||	          }
| j        j        }d| j        _        	 |dk    r|
                    |          }nB|dk    r|
                    |          }n&|dk    r|
                    |          }n
J d	            || j        _        n# || j        _        w xY wd
|v r-t          |d
         t                    sJ |d
xx         dz  cc<   |S )z!Locate and inspect expression(s).r{   zWCommand "inspect" is only valid after a "check" command (that produces no parse errors))r  r  r  r  r  r  r  Ttypeattrs
definitionFzUnknown inspection kindr   r   )	r   r   r(   inspectionsget_type	get_attrsget_definitionr   r*   )r   r  r  r  r  r  r  r  r  r  engineold_inspectionsrX  s                rK   cmd_inspectzServer.cmd_inspect  s7    ( 	 3  "%%%!5#%	
 	
 	
 ,2#' 
	7v~~22))(33%%..x8877777'6DL$$DL$6666F??fUmS111115MMMT!MMMs   AB, ,B:function	callsiteskwargsr   c                   | j         sddiS t          | j         fi |}	 |r|                    |          }n|                    |          }|sd}n|                    d          s|dz  }|ddd|                                  S # t          $ r/}dt          |          icY d}~|                                  S d}~ww xY w# |                                  w xY w)	z#Suggest a signature for a function.r{   zWCommand 'suggest' is only valid after a 'check' command (that produces no parse errors)zNo suggestions
r   r   r   r   N)r   r   suggest_callsitessuggestr  r  r    r*   )r   r  r  r  r  r   r   s          rK   cmd_suggestzServer.cmd_suggest  s,   ( 	 3  "$";FFvFF	  /..x88nnX..  (\\$'' trQ77 ! 	' 	' 	'SXX&&&&&& 	' s5   -B $C 
B>B9B> C 9B>>C Cc                .    t          j        d           i S )z&Hang for 100 seconds, as a debug hack.d   )r  sleepr   s    rK   cmd_hangzServer.cmd_hang  s    
3	ro   r-  )r(   r   r)   r*   r+   r,   r/   r   )r/   r   )r/   r   )rG   r*   r   r   r/   r   )r   r.   r/   r   )r/   r   )r   r*   r   r   r   ru   r   ru   r   r0   r/   r   )
r  r   r   ru   r   ru   r   r0   r/   r   NN)r   ru   r   r0   r   ru   r  r  r   r  r/   r   )
r   r(  r   ru   r   ru   r   r0   r/   r)  )r   r)  r/   r   )r/   ru   )r   r(  r   ru   r   r0   r/   r)  )NNF)
r   r(  r  r  r   r  r  ru   r/   rs   )F)r   r(  r  ru   r/   rs   )
r  r(  rH  r  rp  r  r  r  r/   r  )r   r  rH  r  r/   r(  )rH  r  rp  r  rR  r   r/   r  )
r&  rs   r   r(  r   ru   r   r0   r/   r)  )FN)
r&  rs   r  r0   r   ru   r   r,   r/   rs   )r   r(  r/   r   )r   r(  r  rs   r   rs   r/   r   )r   r(  r/   r   )r   r(  r  r  r/   r   )r   r   FFFFF)r  r*   r  r*   r  r0   r  r0   r  ru   r  ru   r  ru   r  ru   r  ru   r/   r   )r  r*   r  ru   r  r   r/   r   )__name__
__module____qualname__r   r   rn   r   r   r   r  r  r'  r   r  r   r  r+  r  r  ry  r|  rQ  r  rW  rC  rf  rP  r  r  r  r  r   ro   rK   rm   rm      s       Z Z Z Z Z@Q Q Q Q?5 ?5 ?5 ?5B   "
 
 
 
 
   )I )I )I )IVI I I I $(#'. . . . .`   24 4 4 4
   7 7 7 7
VY VY VY VYv $(#'&+4 4 4 4 4n INw w w w wr'" '" '" '"R` ` ` `0 0 0 0dY Y Y Y %)    :0 0 0 0: : : :: : : :+  +  +  + b ""%*!", , , , ,\       0     ro   rm   i   MiBr)  c                    i } 	 dd l }|                                }|                                }|j        t          z  | d<   |j        t          z  | d<   t          j        dk    r|j        t          z  | d<   n^dd l	}|
                    |j                  }t          j        dk    rd}nd}|j        |z  t          z  | d<   n# t          $ r d	| d
<   Y nw xY w| S )Nr   memory_rss_mibmemory_vms_mibr&   memory_maxrss_mibdarwinr4   i   zXpsutil not found, run pip install mypy[dmypy] to install the needed components for dmypymemory_psutil_missing)psutilProcessmemory_inforssr  vmsr8   r   	peak_wsetresource	getrusageRUSAGE_SELF	ru_maxrssImportError)r   r  processmeminfor  rusagefactors          rK   r   r     s   CG ..""%%'' 'c 1 'c 1<7""'.'83'>C#$$ OOO''(<==F|x'''-'7&'@3'FC#$$+  
 
 
9 	#$$$
, Js   C   CCr   rH  r  extraSequence[BuildSource]r(  c                    t          |          }d |D             }|                                 D ]1\  }}||vr(|                    t          |j        |                     2|S )Nc                    h | ]	}|j         
S r   r   rk  s     rK   r  z,find_all_sources_in_build.<locals>.<setcomp>  s    ///fFM///ro   )r   r}  r=   r   r   )rH  r  rX  rp  r   r\  s         rK   rG  rG    sl     %[[F/////D ; ;MM+ej&99:::Mro   r   r]  r  r   c                h    t          |          |                    fd| D                        dS )zAdd all (explicit) sources to the list changed files in place.

    Use this when re-processing of unchanged files is needed (e.g. for
    the purpose of exporting types for inspections).
    c                Z    g | ]'}|j         r|j        |j         fv|j        |j         f(S r   r  )r  bsr  s     rK   r  z.add_all_sources_to_changed.<locals>.<listcomp>  sL     	
 	
 	
w	
 Irw/{BB Y BBBro   N)r  rz  )r   r]  r  s     @rK   rg  rg    sV     g,,KNN	
 	
 	
 	
	
 	
 	
    ro   c                0   |                                  D ]\  }}g }g }|j        |j        z   D ]1}|| v r|                    |           |                    |           2||_        t	          |          |_        ||_        t	          |          |_        dS )zAfter an incremental update, update module dependencies to reflect the new state.

    This can make some suppressed dependencies non-suppressed, and vice versa (if modules
    have been added to or removed from the build).
    N)r}  r  
suppressedr=   r  dependencies_setr  )rH  r   r\  new_suppressednew_dependenciesr  s         rK   r  r    s      3 3%(88 	+ 	+Ce|| '',,,,%%c****-!$%5!6!6)">223 3ro   r  r  rR  r   r   r   c                   t                      }|j        |j        z   |j        z   |j        z   }|D ]}	 |                    |          }n# t          $ r g }Y nw xY w|D ]z}|                    d          r|dd         }n?|                    d          r|dd         }n|                    d          r
|dd         }|| v r|                    |           {|S )a  Quickly filter out obviously missing top-level packages.

    Return packages with entries that can't be found removed.

    This is approximate: some packages that aren't actually valid may be
    included. However, all potentially valid packages must be returned.
    z.pyNr  z-stubsi)	r  python_path	mypy_pathpackage_pathtypeshed_pathlistdirr   r  r  )r  rR  r   r  r  r  entriesentrys           rK   r  r  0  s*    EEE 

 	!

#	$ 
$	% 
  ! !	ooa((GG 	 	 	GGG	 	! 	!E ~~e$$ #crc
'' #crc
)) #crc
  		%   	! Ls   A		AAr  )
r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r-  )rM   rN   r-   r.   r/   r0   )rr   rs   rt   ru   r/   r   )r   r*   r/   ru   )r/   r)  )r   )rH  r  r  r  r/   r(  )r   r(  r]  r  r/   r   )rH  r  r/   r   )r  r  rR  r   r   r   r/   r  )W__doc__
__future__r   r   r>   r   r   rY   r:   rC   r8   r  r   
contextlibr   r   typingr   r   r   r	   r
   r   r   typing_extensionsr   r   
mypy.buildr}   mypy.errors	mypy.mainmypy.dmypy_utilr   r   r   mypy.find_sourcesr   r   mypy.fscacher   mypy.fswatcherr   r   mypy.inspectionsr   mypy.ipcr   mypy.modulefinderr   r   r   r   mypy.optionsr   mypy.server.updater   r   mypy.suggestionsr   r    mypy.typestater!   	mypy.utilr"   r#   mypy.versionr$   r%   __annotations__r   r'   rL   rk   rq   r   r   r*   r   r   r   rm   r  r   rG  rg  r  r  r   ro   rK   <module>r8     s
     # " " " " "   				  				      



      7 7 7 7 7 7 7 7 K K K K K K K K K K K K K K K K K K 5 5 5 5 5 5             6 6 6 6 6 6 6 6 6 6 C C C C C C C C ( ( ( ( ( ( 6 6 6 6 6 6 6 6 - - - - - -       ] ] ] ] ] ] ] ] ] ] ] ]             U U U U U U U U @ @ @ @ @ @ @ @ - - - - - - 1 1 1 1 1 1 1 1 $ $ $ $ $ $    <7&&&&&& `d           >+ + + + +\ `dT T T T T !           &+ + + + #38_ , , , ,">2 2 2 2 2!&'G!H  H H H Hr r r r r r r rp        < =?        3 3 3 3(# # # # # #ro   