Skip to content

Commit 3d81dcb

Browse files
authored
Add internal options flag for exporting global type map (#5325)
Previously type map export was enabled by ad hoc rules. This makes it more explicit, and allows it to be used by mypyc.
1 parent 624a94f commit 3d81dcb

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

mypy/build.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(self, manager: 'BuildManager', graph: Graph) -> None:
8686
self.manager = manager
8787
self.graph = graph
8888
self.files = manager.modules
89-
self.types = manager.all_types # Non-empty for tests only or if dumping deps
89+
self.types = manager.all_types # Non-empty if export_types True in options
9090
self.used_cache = manager.cache_enabled
9191
self.errors = [] # type: List[str] # Filled in by build if desired
9292

@@ -662,7 +662,7 @@ class BuildManager:
662662
Semantic analyzer, pass 2
663663
semantic_analyzer_pass3:
664664
Semantic analyzer, pass 3
665-
all_types: Map {Expression: Type} collected from all modules (tests only)
665+
all_types: Map {Expression: Type} from all modules (enabled by export_types)
666666
options: Build options
667667
missing_modules: Set of modules that could not be imported encountered so far
668668
stale_modules: Set of modules that needed to be rechecked (only used by tests)
@@ -708,7 +708,7 @@ def __init__(self, data_dir: str,
708708
self.errors, self.plugin)
709709
self.semantic_analyzer_pass3 = SemanticAnalyzerPass3(self.modules, self.errors,
710710
self.semantic_analyzer)
711-
self.all_types = {} # type: Dict[Expression, Type] # Used by tests only
711+
self.all_types = {} # type: Dict[Expression, Type] # Enabled by export_types
712712
self.indirection_detector = TypeIndirectionVisitor()
713713
self.stale_modules = set() # type: Set[str]
714714
self.rechecked_modules = set() # type: Set[str]
@@ -2173,10 +2173,9 @@ def finish_passes(self) -> None:
21732173
if self.options.semantic_analysis_only:
21742174
return
21752175
with self.wrap_context():
2176-
# Some tests want to look at the set of all types.
2176+
# Some tests (and tools) want to look at the set of all types.
21772177
options = manager.options
2178-
if ((options.use_builtins_fixtures and not options.fine_grained_incremental) or
2179-
manager.options.dump_deps):
2178+
if options.export_types:
21802179
manager.all_types.update(self.type_map())
21812180

21822181
# We should always patch indirect dependencies, even in full (non-incremental) builds,

mypy/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def __init__(self) -> None:
195195
self.local_partial_types = False
196196
# Some behaviors are changed when using Bazel (https://bazel.build).
197197
self.bazel = False
198+
# If True, export inferred types for all expressions as BuildResult.types
199+
self.export_types = False
198200
# List of package roots -- directories under these are packages even
199201
# if they don't have __init__.py.
200202
self.package_root = [] # type: List[str]

mypy/test/testdeps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def build(self,
8383
options.show_traceback = True
8484
options.cache_dir = os.devnull
8585
options.python_version = python_version
86+
options.export_types = True
8687
try:
8788
result = build.build(sources=[BuildSource('main', None, source)],
8889
options=options,

mypy/test/testtypegen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
3434
options.strict_optional = False # TODO: Enable strict optional checking
3535
options.use_builtins_fixtures = True
3636
options.show_traceback = True
37+
options.export_types = True
3738
result = build.build(sources=[BuildSource('main', None, src)],
3839
options=options,
3940
alt_lib_path=test_temp_dir)

0 commit comments

Comments
 (0)