Skip to content

Commit c0372cc

Browse files
ilevkivskyiIvan Levkivskyi
and
Ivan Levkivskyi
authored
Fix custom typeshed dir handling by is_typeshed_file() (#13629)
This was broken by #13155, fix is straightforward, pass custom typeshed dir from options everywhere. Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent d8652b4 commit c0372cc

File tree

8 files changed

+34
-17
lines changed

8 files changed

+34
-17
lines changed

mypy/build.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,10 @@ def __init__(
668668
raise CompileError(
669669
[f"Failed to find builtin module {module}, perhaps typeshed is broken?"]
670670
)
671-
if is_typeshed_file(path):
671+
if is_typeshed_file(options.abs_custom_typeshed_dir, path) or is_stub_package_file(
672+
path
673+
):
672674
continue
673-
if is_stub_package_file(path):
674-
continue
675-
if options.custom_typeshed_dir is not None:
676-
# Check if module lives under custom_typeshed_dir subtree
677-
custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)
678-
path = os.path.abspath(path)
679-
if os.path.commonpath((path, custom_typeshed_dir)) == custom_typeshed_dir:
680-
continue
681675

682676
raise CompileError(
683677
[

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def __init__(
387387
self.pass_num = 0
388388
self.current_node_deferred = False
389389
self.is_stub = tree.is_stub
390-
self.is_typeshed_stub = is_typeshed_file(path)
390+
self.is_typeshed_stub = is_typeshed_file(options.abs_custom_typeshed_dir, path)
391391
self.inferred_attribute_types = None
392392

393393
# If True, process function definitions. If False, don't. This is used

mypy/errors.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,10 @@ def clear_errors_in_targets(self, path: str, targets: set[str]) -> None:
617617
self.has_blockers.remove(path)
618618

619619
def generate_unused_ignore_errors(self, file: str) -> None:
620-
if is_typeshed_file(file) or file in self.ignored_files:
620+
if (
621+
is_typeshed_file(self.options.abs_custom_typeshed_dir if self.options else None, file)
622+
or file in self.ignored_files
623+
):
621624
return
622625
ignored_lines = self.ignored_lines[file]
623626
used_ignored_lines = self.used_ignored_lines[file]
@@ -658,7 +661,10 @@ def generate_unused_ignore_errors(self, file: str) -> None:
658661
def generate_ignore_without_code_errors(
659662
self, file: str, is_warning_unused_ignores: bool
660663
) -> None:
661-
if is_typeshed_file(file) or file in self.ignored_files:
664+
if (
665+
is_typeshed_file(self.options.abs_custom_typeshed_dir if self.options else None, file)
666+
or file in self.ignored_files
667+
):
662668
return
663669

664670
used_ignored_lines = self.used_ignored_lines[file]

mypy/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,10 @@ def set_strict_flags() -> None:
12641264
# Enabling an error code always overrides disabling
12651265
options.disabled_error_codes -= options.enabled_error_codes
12661266

1267+
# Compute absolute path for custom typeshed (if present).
1268+
if options.custom_typeshed_dir is not None:
1269+
options.abs_custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)
1270+
12671271
# Set build flags.
12681272
if special_opts.find_occurrences:
12691273
state.find_occurrences = special_opts.find_occurrences.split(".")

mypy/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def __init__(self) -> None:
7777
self.platform = sys.platform
7878
self.custom_typing_module: str | None = None
7979
self.custom_typeshed_dir: str | None = None
80+
# The abspath() version of the above, we compute it once as an optimization.
81+
self.abs_custom_typeshed_dir: str | None = None
8082
self.mypy_path: list[str] = []
8183
self.report_dirs: dict[str, str] = {}
8284
# Show errors in PEP 561 packages/site-packages modules

mypy/semanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,9 @@ def file_context(
741741
self.cur_mod_id = file_node.fullname
742742
with scope.module_scope(self.cur_mod_id):
743743
self._is_stub_file = file_node.path.lower().endswith(".pyi")
744-
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
744+
self._is_typeshed_stub_file = is_typeshed_file(
745+
options.abs_custom_typeshed_dir, file_node.path
746+
)
745747
self.globals = file_node.names
746748
self.tvar_scope = TypeVarLikeScope()
747749

mypy/semanal_main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ def check_type_arguments(graph: Graph, scc: list[str], errors: Errors) -> None:
367367
for module in scc:
368368
state = graph[module]
369369
assert state.tree
370-
analyzer = TypeArgumentAnalyzer(errors, state.options, is_typeshed_file(state.path or ""))
370+
analyzer = TypeArgumentAnalyzer(
371+
errors,
372+
state.options,
373+
is_typeshed_file(state.options.abs_custom_typeshed_dir, state.path or ""),
374+
)
371375
with state.wrap_context():
372376
with mypy.state.state.strict_optional_set(state.options.strict_optional):
373377
state.tree.accept(analyzer)
@@ -381,7 +385,11 @@ def check_type_arguments_in_targets(
381385
This mirrors the logic in check_type_arguments() except that we process only
382386
some targets. This is used in fine grained incremental mode.
383387
"""
384-
analyzer = TypeArgumentAnalyzer(errors, state.options, is_typeshed_file(state.path or ""))
388+
analyzer = TypeArgumentAnalyzer(
389+
errors,
390+
state.options,
391+
is_typeshed_file(state.options.abs_custom_typeshed_dir, state.path or ""),
392+
)
385393
with state.wrap_context():
386394
with mypy.state.state.strict_optional_set(state.options.strict_optional):
387395
for target in targets:

mypy/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,10 @@ def format_error(
769769
return self.style(msg, "red", bold=True)
770770

771771

772-
def is_typeshed_file(file: str) -> bool:
772+
def is_typeshed_file(typeshed_dir: str | None, file: str) -> bool:
773+
typeshed_dir = typeshed_dir if typeshed_dir is not None else TYPESHED_DIR
773774
try:
774-
return os.path.commonpath((TYPESHED_DIR, os.path.abspath(file))) == TYPESHED_DIR
775+
return os.path.commonpath((typeshed_dir, os.path.abspath(file))) == typeshed_dir
775776
except ValueError: # Different drives on Windows
776777
return False
777778

0 commit comments

Comments
 (0)