Skip to content

Commit b921a83

Browse files
authored
Refactor: move is_typeshed_file() to mypy.util (python#8404)
This way we can call it in contexts that don't have access to an `Errors` instance.
1 parent 35bdb07 commit b921a83

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
from mypy import state, errorcodes as codes
7979
from mypy.traverser import has_return_statement, all_return_statements
8080
from mypy.errorcodes import ErrorCode
81+
from mypy.util import is_typeshed_file
8182

8283
T = TypeVar('T')
8384

@@ -233,7 +234,7 @@ def __init__(self, errors: Errors, modules: Dict[str, MypyFile], options: Option
233234
self.pass_num = 0
234235
self.current_node_deferred = False
235236
self.is_stub = tree.is_stub
236-
self.is_typeshed_stub = errors.is_typeshed_file(path)
237+
self.is_typeshed_stub = is_typeshed_file(path)
237238
self.inferred_attribute_types = None
238239
if options.strict_optional_whitelist is None:
239240
self.suppress_none_errors = not options.show_none_errors

mypy/errors.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mypy.version import __version__ as mypy_version
1212
from mypy.errorcodes import ErrorCode
1313
from mypy import errorcodes as codes
14-
from mypy.util import DEFAULT_SOURCE_OFFSET
14+
from mypy.util import DEFAULT_SOURCE_OFFSET, is_typeshed_file
1515

1616
T = TypeVar('T')
1717
allowed_duplicates = ['@overload', 'Got:', 'Expected:'] # type: Final
@@ -372,18 +372,14 @@ def clear_errors_in_targets(self, path: str, targets: Set[str]) -> None:
372372

373373
def generate_unused_ignore_errors(self, file: str) -> None:
374374
ignored_lines = self.ignored_lines[file]
375-
if not self.is_typeshed_file(file) and file not in self.ignored_files:
375+
if not is_typeshed_file(file) and file not in self.ignored_files:
376376
for line in set(ignored_lines) - self.used_ignored_lines[file]:
377377
# Don't use report since add_error_info will ignore the error!
378378
info = ErrorInfo(self.import_context(), file, self.current_module(), None,
379379
None, line, -1, 'error', "unused 'type: ignore' comment",
380380
None, False, False)
381381
self._add_error_info(file, info)
382382

383-
def is_typeshed_file(self, file: str) -> bool:
384-
# gross, but no other clear way to tell
385-
return 'typeshed' in os.path.normpath(file).split(os.sep)
386-
387383
def num_messages(self) -> int:
388384
"""Return the number of generated messages."""
389385
return sum(len(x) for x in self.error_info_map.values())

mypy/semanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
Plugin, ClassDefContext, SemanticAnalyzerPluginInterface,
104104
DynamicClassDefContext
105105
)
106-
from mypy.util import correct_relative_import, unmangle, module_prefix
106+
from mypy.util import correct_relative_import, unmangle, module_prefix, is_typeshed_file
107107
from mypy.scope import Scope
108108
from mypy.semanal_shared import (
109109
SemanticAnalyzerInterface, set_callable_name, calculate_tuple_fallback, PRIORITY_FALLBACKS
@@ -481,7 +481,7 @@ def file_context(self,
481481
self.cur_mod_id = file_node.fullname
482482
scope.enter_file(self.cur_mod_id)
483483
self.is_stub_file = file_node.path.lower().endswith('.pyi')
484-
self._is_typeshed_stub_file = self.errors.is_typeshed_file(file_node.path)
484+
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
485485
self.globals = file_node.names
486486
self.tvar_scope = TypeVarScope()
487487

mypy/semanal_main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from mypy.semanal_infer import infer_decorator_signature_if_simple
4545
from mypy.checker import FineGrainedDeferredNode
4646
from mypy.server.aststrip import SavedAttributes
47+
from mypy.util import is_typeshed_file
4748
import mypy.build
4849

4950
if TYPE_CHECKING:
@@ -353,7 +354,7 @@ def check_type_arguments(graph: 'Graph', scc: List[str], errors: Errors) -> None
353354
assert state.tree
354355
analyzer = TypeArgumentAnalyzer(errors,
355356
state.options,
356-
errors.is_typeshed_file(state.path or ''))
357+
is_typeshed_file(state.path or ''))
357358
with state.wrap_context():
358359
with strict_optional_set(state.options.strict_optional):
359360
state.tree.accept(analyzer)
@@ -368,7 +369,7 @@ def check_type_arguments_in_targets(targets: List[FineGrainedDeferredNode], stat
368369
"""
369370
analyzer = TypeArgumentAnalyzer(errors,
370371
state.options,
371-
errors.is_typeshed_file(state.path or ''))
372+
is_typeshed_file(state.path or ''))
372373
with state.wrap_context():
373374
with strict_optional_set(state.options.strict_optional):
374375
for target in targets:

mypy/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Utility functions with no non-trivial dependencies."""
2+
23
import os
34
import pathlib
45
import re
@@ -700,3 +701,8 @@ def format_error(self, n_errors: int, n_files: int, n_sources: int,
700701
if not use_color:
701702
return msg
702703
return self.style(msg, 'red', bold=True)
704+
705+
706+
def is_typeshed_file(file: str) -> bool:
707+
# gross, but no other clear way to tell
708+
return 'typeshed' in os.path.normpath(file).split(os.sep)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy