@@ -1032,9 +1032,7 @@ def parse_all(self, states: list[State], post_parse: bool = True) -> None: |
1032 | 1032 |
|
1033 | 1033 | parallel_states = [] |
1034 | 1034 | for state in states: |
1035 | | - if not self.fscache.exists(state.xpath, real_only=True) or ( |
1036 | | - self.shadow_map and self.maybe_swap_for_shadow_path(state.xpath) != state.xpath |
1037 | | - ): |
| 1035 | + if state.requires_read(): |
1038 | 1036 | state.source = state.get_source() |
1039 | 1037 | if state.tree is not None: |
1040 | 1038 | # The file was already parsed. |
@@ -1077,13 +1075,6 @@ def parse_all(self, states: list[State], post_parse: bool = True) -> None: |
1077 | 1075 | for state in parallel_states: |
1078 | 1076 | assert state.tree is not None |
1079 | 1077 | if state in parallel_parsed_states_set: |
1080 | | - if state.tree.raw_data is not None: |
1081 | | - # source_hash was already extracted above, but raw_data |
1082 | | - # may have been preserved for workers (imports_only=True). |
1083 | | - pass |
1084 | | - elif state.source_hash is None: |
1085 | | - # At least namespace packages may not have source. |
1086 | | - state.get_source() |
1087 | 1078 | state.early_errors = list(self.errors.error_info_map.get(state.xpath, [])) |
1088 | 1079 | state.semantic_analysis_pass1() |
1089 | 1080 | self.ast_cache[state.id] = (state.tree, state.early_errors, state.source_hash) |
@@ -3146,6 +3137,26 @@ def fix_cross_refs(self) -> None: |
3146 | 3137 |
|
3147 | 3138 | # Methods for processing modules from source code. |
3148 | 3139 |
|
| 3140 | + def requires_read(self) -> bool: |
| 3141 | + """Do we need to force reading the file source before parsing? |
| 3142 | +
|
| 3143 | + Ruff parser can do all the work faster, but there are situation where |
| 3144 | + we cannot delegate the work to it (because it needs actual file on disk): |
| 3145 | + * Non-trivial shadow file mapping. |
| 3146 | + * Various "fake" files like namespace packages, etc. |
| 3147 | + """ |
| 3148 | + if ( |
| 3149 | + self.manager.shadow_map |
| 3150 | + and self.manager.maybe_swap_for_shadow_path(self.xpath) != self.xpath |
| 3151 | + ): |
| 3152 | + return True |
| 3153 | + return ( |
| 3154 | + self.manager.fscache.isdir(self.xpath) |
| 3155 | + or not self.manager.fscache.exists(self.xpath, real_only=True) |
| 3156 | + # For a better error in case one tries to parse a .pyd file (which is a DLL). |
| 3157 | + or self.xpath.endswith(".pyd") |
| 3158 | + ) |
| 3159 | + |
3149 | 3160 | def get_source(self) -> str: |
3150 | 3161 | """Get module source and parse inline mypy configurations.""" |
3151 | 3162 | manager = self.manager |
@@ -3211,10 +3222,10 @@ def parse_file(self, *, temporary: bool = False, raw_data: FileRawData | None = |
3211 | 3222 | # The file was already parsed. |
3212 | 3223 | return |
3213 | 3224 |
|
3214 | | - if raw_data is None: |
| 3225 | + if not self.options.native_parser or raw_data is None and self.requires_read(): |
3215 | 3226 | source = self.get_source() |
3216 | 3227 | else: |
3217 | | - source = "" |
| 3228 | + source = None |
3218 | 3229 | manager = self.manager |
3219 | 3230 | # Can we reuse a previously parsed AST? This avoids redundant work in daemon. |
3220 | 3231 | if self.id not in manager.ast_cache: |
@@ -3235,6 +3246,9 @@ def parse_file(self, *, temporary: bool = False, raw_data: FileRawData | None = |
3235 | 3246 | # New parser returns serialized trees that need to be de-serialized. |
3236 | 3247 | if self.tree.raw_data is not None: |
3237 | 3248 | assert raw_data is None |
| 3249 | + # Same as above, apply inline configuration first. |
| 3250 | + self.source_hash = self.tree.raw_data.source_hash |
| 3251 | + self.apply_inline_configuration(self.tree.raw_data.mypy_comments) |
3238 | 3252 | self.tree = load_from_raw( |
3239 | 3253 | self.xpath, |
3240 | 3254 | self.id, |
|