Skip to content

Commit

Permalink
Cache information about whether file is typeshed file (#16351)
Browse files Browse the repository at this point in the history
We used to check if a file is in typeshed a lot. This seems to speed up
self-check by about 2%, and this should also speed up tests a bit.
  • Loading branch information
JukkaL authored Oct 28, 2023
1 parent f33c9a3 commit c4ab46e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
from mypy.types_utils import is_overlapping_none, remove_optional, store_argument_type, strip_type
from mypy.typetraverser import TypeTraverserVisitor
from mypy.typevars import fill_typevars, fill_typevars_with_any, has_no_typevars
from mypy.util import is_dunder, is_sunder, is_typeshed_file
from mypy.util import is_dunder, is_sunder
from mypy.visitor import NodeVisitor

T = TypeVar("T")
Expand Down Expand Up @@ -400,7 +400,7 @@ def __init__(
self.pass_num = 0
self.current_node_deferred = False
self.is_stub = tree.is_stub
self.is_typeshed_stub = is_typeshed_file(options.abs_custom_typeshed_dir, path)
self.is_typeshed_stub = tree.is_typeshed_file(options)
self.inferred_attribute_types = None

# If True, process function definitions. If False, don't. This is used
Expand Down
11 changes: 10 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import mypy.strconv
from mypy.options import Options
from mypy.util import short_type
from mypy.util import is_typeshed_file, short_type
from mypy.visitor import ExpressionVisitor, NodeVisitor, StatementVisitor

if TYPE_CHECKING:
Expand Down Expand Up @@ -283,6 +283,7 @@ class MypyFile(SymbolNode):
"is_partial_stub_package",
"plugin_deps",
"future_import_flags",
"_is_typeshed_file",
)

__match_args__ = ("name", "path", "defs")
Expand Down Expand Up @@ -319,6 +320,7 @@ class MypyFile(SymbolNode):
plugin_deps: dict[str, set[str]]
# Future imports defined in this file. Populated during semantic analysis.
future_import_flags: set[str]
_is_typeshed_file: bool | None

def __init__(
self,
Expand Down Expand Up @@ -346,6 +348,7 @@ def __init__(
self.is_cache_skeleton = False
self.is_partial_stub_package = False
self.future_import_flags = set()
self._is_typeshed_file = None

def local_definitions(self) -> Iterator[Definition]:
"""Return all definitions within the module (including nested).
Expand All @@ -371,6 +374,12 @@ def is_package_init_file(self) -> bool:
def is_future_flag_set(self, flag: str) -> bool:
return flag in self.future_import_flags

def is_typeshed_file(self, options: Options) -> bool:
# Cache result since this is called a lot
if self._is_typeshed_file is None:
self._is_typeshed_file = is_typeshed_file(options.abs_custom_typeshed_dir, self.path)
return self._is_typeshed_file

def serialize(self) -> JsonDict:
return {
".class": "MypyFile",
Expand Down
13 changes: 2 additions & 11 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,7 @@
)
from mypy.types_utils import is_invalid_recursive_alias, store_argument_type
from mypy.typevars import fill_typevars
from mypy.util import (
correct_relative_import,
is_dunder,
is_typeshed_file,
module_prefix,
unmangle,
unnamed_function,
)
from mypy.util import correct_relative_import, is_dunder, module_prefix, unmangle, unnamed_function
from mypy.visitor import NodeVisitor

T = TypeVar("T")
Expand Down Expand Up @@ -777,9 +770,7 @@ def file_context(
self.cur_mod_id = file_node.fullname
with scope.module_scope(self.cur_mod_id):
self._is_stub_file = file_node.path.lower().endswith(".pyi")
self._is_typeshed_stub_file = is_typeshed_file(
options.abs_custom_typeshed_dir, file_node.path
)
self._is_typeshed_stub_file = file_node.is_typeshed_file(options)
self.globals = file_node.names
self.tvar_scope = TypeVarLikeScope()

Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def check_type_arguments(graph: Graph, scc: list[str], errors: Errors) -> None:
analyzer = TypeArgumentAnalyzer(
errors,
state.options,
is_typeshed_file(state.options.abs_custom_typeshed_dir, state.path or ""),
state.tree.is_typeshed_file(state.options),
state.manager.semantic_analyzer.named_type,
)
with state.wrap_context():
Expand Down

0 comments on commit c4ab46e

Please sign in to comment.