Skip to content

Commit 784d446

Browse files
ilevkivskyiIvan Levkivskyi
authored andcommitted
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 <ilevkivskyi@dropbox.com>
1 parent bf143d9 commit 784d446

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
if options.strict_optional_whitelist is None:
393393
self.suppress_none_errors = not options.show_none_errors

mypy/errors.py

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

611611
def generate_unused_ignore_errors(self, file: str) -> None:
612-
if is_typeshed_file(file) or file in self.ignored_files:
612+
if (
613+
is_typeshed_file(self.options.abs_custom_typeshed_dir if self.options else None, file)
614+
or file in self.ignored_files
615+
):
613616
return
614617
ignored_lines = self.ignored_lines[file]
615618
used_ignored_lines = self.used_ignored_lines[file]
@@ -650,7 +653,10 @@ def generate_unused_ignore_errors(self, file: str) -> None:
650653
def generate_ignore_without_code_errors(
651654
self, file: str, is_warning_unused_ignores: bool
652655
) -> None:
653-
if is_typeshed_file(file) or file in self.ignored_files:
656+
if (
657+
is_typeshed_file(self.options.abs_custom_typeshed_dir if self.options else None, file)
658+
or file in self.ignored_files
659+
):
654660
return
655661

656662
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
@@ -1267,6 +1267,10 @@ def set_strict_flags() -> None:
12671267
# Enabling an error code always overrides disabling
12681268
options.disabled_error_codes -= options.enabled_error_codes
12691269

1270+
# Compute absolute path for custom typeshed (if present).
1271+
if options.custom_typeshed_dir is not None:
1272+
options.abs_custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)
1273+
12701274
# Set build flags.
12711275
if options.strict_optional_whitelist is not None:
12721276
# TODO: Deprecate, then kill this flag

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
@@ -736,7 +736,9 @@ def file_context(
736736
self.cur_mod_id = file_node.fullname
737737
with scope.module_scope(self.cur_mod_id):
738738
self._is_stub_file = file_node.path.lower().endswith(".pyi")
739-
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
739+
self._is_typeshed_stub_file = is_typeshed_file(
740+
options.abs_custom_typeshed_dir, file_node.path
741+
)
740742
self.globals = file_node.names
741743
self.tvar_scope = TypeVarLikeScope()
742744

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)