Skip to content

Refactor: move is_typeshed_file() to mypy.util #8404

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.

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
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Refactor: move is_typeshed_file() to mypy.util
This way we can call it in contexts that don't have access to
an `Errors` instance.
  • Loading branch information
JukkaL committed Feb 14, 2020
commit d6f2a701c1bc84a1853bdd208e2013f202338ca6
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
from mypy import state, errorcodes as codes
from mypy.traverser import has_return_statement, all_return_statements
from mypy.errorcodes import ErrorCode
from mypy.util import is_typeshed_file

T = TypeVar('T')

Expand Down Expand Up @@ -233,7 +234,7 @@ def __init__(self, errors: Errors, modules: Dict[str, MypyFile], options: Option
self.pass_num = 0
self.current_node_deferred = False
self.is_stub = tree.is_stub
self.is_typeshed_stub = errors.is_typeshed_file(path)
self.is_typeshed_stub = is_typeshed_file(path)
self.inferred_attribute_types = None
if options.strict_optional_whitelist is None:
self.suppress_none_errors = not options.show_none_errors
Expand Down
8 changes: 2 additions & 6 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mypy.version import __version__ as mypy_version
from mypy.errorcodes import ErrorCode
from mypy import errorcodes as codes
from mypy.util import DEFAULT_SOURCE_OFFSET
from mypy.util import DEFAULT_SOURCE_OFFSET, is_typeshed_file

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

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

def is_typeshed_file(self, file: str) -> bool:
# gross, but no other clear way to tell
return 'typeshed' in os.path.normpath(file).split(os.sep)

def num_messages(self) -> int:
"""Return the number of generated messages."""
return sum(len(x) for x in self.error_info_map.values())
Expand Down
4 changes: 2 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
Plugin, ClassDefContext, SemanticAnalyzerPluginInterface,
DynamicClassDefContext
)
from mypy.util import correct_relative_import, unmangle, module_prefix
from mypy.util import correct_relative_import, unmangle, module_prefix, is_typeshed_file
from mypy.scope import Scope
from mypy.semanal_shared import (
SemanticAnalyzerInterface, set_callable_name, calculate_tuple_fallback, PRIORITY_FALLBACKS
Expand Down Expand Up @@ -481,7 +481,7 @@ def file_context(self,
self.cur_mod_id = file_node.fullname
scope.enter_file(self.cur_mod_id)
self.is_stub_file = file_node.path.lower().endswith('.pyi')
self._is_typeshed_stub_file = self.errors.is_typeshed_file(file_node.path)
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
self.globals = file_node.names
self.tvar_scope = TypeVarScope()

Expand Down
5 changes: 3 additions & 2 deletions mypy/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from mypy.semanal_infer import infer_decorator_signature_if_simple
from mypy.checker import FineGrainedDeferredNode
from mypy.server.aststrip import SavedAttributes
from mypy.util import is_typeshed_file
import mypy.build

if TYPE_CHECKING:
Expand Down Expand Up @@ -353,7 +354,7 @@ def check_type_arguments(graph: 'Graph', scc: List[str], errors: Errors) -> None
assert state.tree
analyzer = TypeArgumentAnalyzer(errors,
state.options,
errors.is_typeshed_file(state.path or ''))
is_typeshed_file(state.path or ''))
with state.wrap_context():
with strict_optional_set(state.options.strict_optional):
state.tree.accept(analyzer)
Expand All @@ -368,7 +369,7 @@ def check_type_arguments_in_targets(targets: List[FineGrainedDeferredNode], stat
"""
analyzer = TypeArgumentAnalyzer(errors,
state.options,
errors.is_typeshed_file(state.path or ''))
is_typeshed_file(state.path or ''))
with state.wrap_context():
with strict_optional_set(state.options.strict_optional):
for target in targets:
Expand Down
6 changes: 6 additions & 0 deletions mypy/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions with no non-trivial dependencies."""

import os
import pathlib
import re
Expand Down Expand Up @@ -700,3 +701,8 @@ def format_error(self, n_errors: int, n_files: int, n_sources: int,
if not use_color:
return msg
return self.style(msg, 'red', bold=True)


def is_typeshed_file(file: str) -> bool:
# gross, but no other clear way to tell
return 'typeshed' in os.path.normpath(file).split(os.sep)
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