1111from collections .abc import Sequence
1212from gettext import gettext
1313from io import TextIOWrapper
14- from typing import IO , TYPE_CHECKING , Any , Final , NoReturn , TextIO
14+ from typing import IO , TYPE_CHECKING , Any , Callable , Final , NoReturn , TextIO
1515
1616from mypy import build , defaults , state , util
1717from mypy .config_parser import (
@@ -86,7 +86,7 @@ def main(
8686 stdout .reconfigure (errors = "backslashreplace" )
8787
8888 fscache = FileSystemCache ()
89- sources , options = process_options (args , stdout = stdout , stderr = stderr , fscache = fscache )
89+ sources , options , on_plugins_loaded = process_options (args , stdout = stdout , stderr = stderr , fscache = fscache )
9090 if clean_exit :
9191 options .fast_exit = False
9292
@@ -124,7 +124,7 @@ def main(
124124 install_types (formatter , options , non_interactive = options .non_interactive )
125125 return
126126
127- res , messages , blockers = run_build (sources , options , fscache , t0 , stdout , stderr )
127+ res , messages , blockers = run_build (sources , options , fscache , t0 , stdout , stderr , on_plugins_loaded = on_plugins_loaded )
128128
129129 if options .non_interactive :
130130 missing_pkgs = read_types_packages_to_install (options .cache_dir , after_run = True )
@@ -182,6 +182,7 @@ def run_build(
182182 t0 : float ,
183183 stdout : TextIO ,
184184 stderr : TextIO ,
185+ on_plugins_loaded : Callable [[], None ] | None = None
185186) -> tuple [build .BuildResult | None , list [str ], bool ]:
186187 formatter = util .FancyFormatter (
187188 stdout , stderr , options .hide_error_codes , hide_success = bool (options .output )
@@ -208,7 +209,7 @@ def flush_errors(filename: str | None, new_messages: list[str], serious: bool) -
208209 try :
209210 # Keep a dummy reference (res) for memory profiling afterwards, as otherwise
210211 # the result could be freed.
211- res = build .build (sources , options , None , flush_errors , fscache , stdout , stderr )
212+ res = build .build (sources , options , None , flush_errors , fscache , stdout , stderr , on_plugins_loaded = on_plugins_loaded )
212213 except CompileError as e :
213214 blockers = True
214215 if not e .use_stdout :
@@ -1347,13 +1348,14 @@ def process_options(
13471348 fscache : FileSystemCache | None = None ,
13481349 program : str = "mypy" ,
13491350 header : str = HEADER ,
1350- ) -> tuple [list [BuildSource ], Options ]:
1351+ ) -> tuple [list [BuildSource ], Options , Callable [[], None ] ]:
13511352 """Parse command line arguments.
13521353
13531354 If a FileSystemCache is passed in, and package_root options are given,
13541355 call fscache.set_package_root() to set the cache's package root.
13551356
1356- Returns a tuple of: a list of source files, an Options collected from flags.
1357+ Returns a tuple of: a list of source files, an Options collected from
1358+ flags, and a callback to be called once plugins have loaded.
13571359 """
13581360 stdout = stdout if stdout is not None else sys .stdout
13591361 stderr = stderr if stderr is not None else sys .stderr
@@ -1456,9 +1458,14 @@ def set_strict_flags() -> None:
14561458 validate_package_allow_list (options .untyped_calls_exclude )
14571459 validate_package_allow_list (options .deprecated_calls_exclude )
14581460
1459- options .process_error_codes (error_callback = parser .error )
14601461 options .process_incomplete_features (error_callback = parser .error , warning_callback = print )
14611462
1463+ def on_plugins_loaded () -> None :
1464+ # Processing error codes after plugins have loaded since plugins may
1465+ # register custom error codes that we don't know about until plugins
1466+ # have loaded.
1467+ options .process_error_codes (error_callback = parser .error )
1468+
14621469 # Compute absolute path for custom typeshed (if present).
14631470 if options .custom_typeshed_dir is not None :
14641471 options .abs_custom_typeshed_dir = os .path .abspath (options .custom_typeshed_dir )
@@ -1550,7 +1557,7 @@ def set_strict_flags() -> None:
15501557 # exceptions of different types.
15511558 except InvalidSourceList as e2 :
15521559 fail (str (e2 ), stderr , options )
1553- return targets , options
1560+ return targets , options , on_plugins_loaded
15541561
15551562
15561563def process_package_roots (
0 commit comments