|
15 | 15 | import gc |
16 | 16 | import json |
17 | 17 | import os |
| 18 | +import pathlib |
18 | 19 | import platform |
19 | 20 | import re |
20 | 21 | import stat |
@@ -605,6 +606,29 @@ def __init__(self, data_dir: str, |
605 | 606 | self.fscache = fscache |
606 | 607 | self.find_module_cache = FindModuleCache(self.search_paths, self.fscache, self.options, |
607 | 608 | source_set=self.source_set) |
| 609 | + for module in CORE_BUILTIN_MODULES: |
| 610 | + if options.use_builtins_fixtures: |
| 611 | + continue |
| 612 | + if module == "_importlib_modulespec": |
| 613 | + continue |
| 614 | + path = self.find_module_cache.find_module(module) |
| 615 | + if not isinstance(path, str): |
| 616 | + raise CompileError([ |
| 617 | + f"Failed to find builtin module {module}, perhaps typeshed is broken?", |
| 618 | + ]) |
| 619 | + if is_typeshed_file(path): |
| 620 | + continue |
| 621 | + if is_stub_package_file(path): |
| 622 | + continue |
| 623 | + if options.custom_typeshed_dir is not None: |
| 624 | + if pathlib.Path(path) in pathlib.Path(options.custom_typeshed_dir).parents: |
| 625 | + continue |
| 626 | + |
| 627 | + raise CompileError([ |
| 628 | + f'mypy: "{os.path.relpath(path)}" shadows library module "{module}"', |
| 629 | + f'note: A user-defined top-level module with name "{module}" is not supported' |
| 630 | + ]) |
| 631 | + |
608 | 632 | self.metastore = create_metastore(options) |
609 | 633 |
|
610 | 634 | # a mapping from source files to their corresponding shadow files |
@@ -2459,15 +2483,6 @@ def find_module_and_diagnose(manager: BuildManager, |
2459 | 2483 | if is_sub_path(result, dir): |
2460 | 2484 | # Silence errors in site-package dirs and typeshed |
2461 | 2485 | follow_imports = 'silent' |
2462 | | - if (id in CORE_BUILTIN_MODULES |
2463 | | - and not is_typeshed_file(result) |
2464 | | - and not is_stub_package_file(result) |
2465 | | - and not options.use_builtins_fixtures |
2466 | | - and not options.custom_typeshed_dir): |
2467 | | - raise CompileError([ |
2468 | | - f'mypy: "{os.path.relpath(result)}" shadows library module "{id}"', |
2469 | | - f'note: A user-defined top-level module with name "{id}" is not supported' |
2470 | | - ]) |
2471 | 2486 | return (result, follow_imports) |
2472 | 2487 | else: |
2473 | 2488 | # Could not find a module. Typically the reason is a |
|
0 commit comments