GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid eagerly reading sources for singe-file batches by ilevkivskyi · Pull Request #21996 · python/mypy

Avoid eagerly reading sources for singe-file batches #21996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ilevkivskyi merged 2 commits into python:master from ilevkivskyi:fix-parse-1
Sep 16, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Avoid eagerly reading sources for singe-file batches #21996

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
37e4f7a
Avoid eagerly reading sources for singe-file batches
ilevkivskyi Sep 16, 2026
dd12b31
Keep better error for parsing .pyd files
ilevkivskyi Sep 16, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Avoid eagerly reading sources for singe-file batches
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi
ilevkivskyi committed Sep 16, 2026
commit 37e4f7a4ca8f59447dea326e15d91ebd507ed295

There are no files selected for viewing

35 changes: 23 additions & 12 deletions 35 mypy/build.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1032,9 +1032,7 @@ def parse_all(self, states: list[State], post_parse: bool = True) -> None:

parallel_states = []
for state in states:
if not self.fscache.exists(state.xpath, real_only=True) or (
self.shadow_map and self.maybe_swap_for_shadow_path(state.xpath) != state.xpath
):
if state.requires_read():
state.source = state.get_source()
if state.tree is not None:
# The file was already parsed.
Expand Down Expand Up @@ -1077,13 +1075,6 @@ def parse_all(self, states: list[State], post_parse: bool = True) -> None:
for state in parallel_states:
assert state.tree is not None
if state in parallel_parsed_states_set:
if state.tree.raw_data is not None:
# source_hash was already extracted above, but raw_data
# may have been preserved for workers (imports_only=True).
pass
elif state.source_hash is None:
# At least namespace packages may not have source.
state.get_source()
state.early_errors = list(self.errors.error_info_map.get(state.xpath, []))
state.semantic_analysis_pass1()
self.ast_cache[state.id] = (state.tree, state.early_errors, state.source_hash)
Expand Down Expand Up @@ -3146,6 +3137,23 @@ def fix_cross_refs(self) -> None:

# Methods for processing modules from source code.

def requires_read(self) -> bool:
"""Do we need to force reading the file source before parsing?

Ruff parser can do all the work faster, but there are situation where
we cannot delegate the work to it (because it needs actual file on disk):
* Non-trivial shadow file mapping.
* Various "fake" files like namespace packages, etc.
"""
if (
self.manager.shadow_map
and self.manager.maybe_swap_for_shadow_path(self.xpath) != self.xpath
):
return True
return self.manager.fscache.isdir(self.xpath) or not self.manager.fscache.exists(
self.xpath, real_only=True
)

def get_source(self) -> str:
"""Get module source and parse inline mypy configurations."""
manager = self.manager
Expand Down Expand Up @@ -3211,10 +3219,10 @@ def parse_file(self, *, temporary: bool = False, raw_data: FileRawData | None =
# The file was already parsed.
return

if raw_data is None:
if not self.options.native_parser or raw_data is None and self.requires_read():
source = self.get_source()
else:
source = ""
source = None
manager = self.manager
# Can we reuse a previously parsed AST? This avoids redundant work in daemon.
if self.id not in manager.ast_cache:
Expand All @@ -3235,6 +3243,9 @@ def parse_file(self, *, temporary: bool = False, raw_data: FileRawData | None =
# New parser returns serialized trees that need to be de-serialized.
if self.tree.raw_data is not None:
assert raw_data is None
# Same as above, apply inline configuration first.
self.source_hash = self.tree.raw_data.source_hash
self.apply_inline_configuration(self.tree.raw_data.mypy_comments)
self.tree = load_from_raw(
self.xpath,
self.id,
Expand Down
8 changes: 7 additions & 1 deletion 8 test-data/unit/cmdline.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ undef
sub.pkg contains __init__.py but is not a valid Python package name
== Return code: 2

[case testBadFileEncoding]
[case testBadFileEncodingIgnored]
# cmd: mypy a.py
[file a.py]
# coding: uft-8
[out]

[case testBadFileEncodingOldParser]
# cmd: mypy a.py --no-native-parser
[file a.py]
# coding: uft-8
[out]
a.py: error: Cannot decode file: unknown encoding: uft-8
== Return code: 2

Expand Down
10 changes: 5 additions & 5 deletions 10 test-data/unit/fine-grained-blockers.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ a.py:1: error: Simple statements must be separated by newlines or semicolons
b.py:2: error: Incompatible return value type (got "str", expected "int")
==

[case testDecodeErrorBlocker1-posix]
[case testDecodeErrorNonBlocker1-posix]
import a
a.f(1)
[file a.py]
Expand All @@ -502,11 +502,11 @@ def f(x: int) -> None: ...
def f(x: str) -> None: ...
[out]
==
a.py: error: Cannot decode file: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
main:2: error: Module has no attribute "f"
==
main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"

[case testDecodeErrorBlocker2-windows]
[case testDecodeErrorNonBlocker2-windows]
import a
a.f(1)
[file a.py]
Expand All @@ -518,11 +518,11 @@ def f(x: int) -> None: ...
def f(x: str) -> None: ...
[out]
==
a.py: error: Cannot decode file: 'ascii' codec can't decode byte 0xc3 in position 17: ordinal not in range(128)
main:2: error: Module has no attribute "f"
==
main:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"

[case testDecodeErrorBlockerOnInitialRun-posix-skip]
[case testDecodeErrorNonBlockerOnInitialRun-posix]
# Note that there's no test variant for Windows, since the above Windows test case is good enough.
import a
a.f(1)
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.