mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 21:04:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,8 +1,6 @@
|
|||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
||||
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||
|
||||
"""Determining whether files are being measured/reported or not."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
|
|
@ -14,20 +12,31 @@ import re
|
|||
import sys
|
||||
import sysconfig
|
||||
import traceback
|
||||
|
||||
from types import FrameType, ModuleType
|
||||
from typing import (
|
||||
cast, Any, Iterable, TYPE_CHECKING,
|
||||
)
|
||||
from types import FrameType
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
from typing import cast
|
||||
from typing import Iterable
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from coverage import env
|
||||
from coverage.disposition import FileDisposition, disposition_init
|
||||
from coverage.exceptions import CoverageException, PluginError
|
||||
from coverage.files import TreeMatcher, GlobMatcher, ModuleMatcher
|
||||
from coverage.files import prep_patterns, find_python_files, canonical_filename
|
||||
from coverage.disposition import disposition_init
|
||||
from coverage.disposition import FileDisposition
|
||||
from coverage.exceptions import CoverageException
|
||||
from coverage.exceptions import PluginError
|
||||
from coverage.files import canonical_filename
|
||||
from coverage.files import find_python_files
|
||||
from coverage.files import GlobMatcher
|
||||
from coverage.files import ModuleMatcher
|
||||
from coverage.files import prep_patterns
|
||||
from coverage.files import TreeMatcher
|
||||
from coverage.misc import sys_modules_saved
|
||||
from coverage.python import source_for_file, source_for_morf
|
||||
from coverage.types import TFileDisposition, TMorf, TWarnFn, TDebugCtl
|
||||
from coverage.python import source_for_file
|
||||
from coverage.python import source_for_morf
|
||||
from coverage.types import TDebugCtl
|
||||
from coverage.types import TFileDisposition
|
||||
from coverage.types import TMorf
|
||||
from coverage.types import TWarnFn
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from coverage.config import CoverageConfig
|
||||
|
|
@ -65,7 +74,7 @@ def canonical_path(morf: TMorf, directory: bool = False) -> str:
|
|||
|
||||
"""
|
||||
morf_path = canonical_filename(source_for_morf(morf))
|
||||
if morf_path.endswith("__init__.py") or directory:
|
||||
if morf_path.endswith('__init__.py') or directory:
|
||||
morf_path = os.path.split(morf_path)[0]
|
||||
return morf_path
|
||||
|
||||
|
|
@ -83,16 +92,16 @@ def name_for_module(filename: str, frame: FrameType | None) -> str:
|
|||
|
||||
"""
|
||||
module_globals = frame.f_globals if frame is not None else {}
|
||||
dunder_name: str = module_globals.get("__name__", None)
|
||||
dunder_name: str = module_globals.get('__name__', None)
|
||||
|
||||
if isinstance(dunder_name, str) and dunder_name != "__main__":
|
||||
if isinstance(dunder_name, str) and dunder_name != '__main__':
|
||||
# This is the usual case: an imported module.
|
||||
return dunder_name
|
||||
|
||||
spec = module_globals.get("__spec__", None)
|
||||
spec = module_globals.get('__spec__', None)
|
||||
if spec:
|
||||
fullname = spec.name
|
||||
if isinstance(fullname, str) and fullname != "__main__":
|
||||
if isinstance(fullname, str) and fullname != '__main__':
|
||||
# Module loaded via: runpy -m
|
||||
return fullname
|
||||
|
||||
|
|
@ -106,12 +115,12 @@ def name_for_module(filename: str, frame: FrameType | None) -> str:
|
|||
|
||||
def module_is_namespace(mod: ModuleType) -> bool:
|
||||
"""Is the module object `mod` a PEP420 namespace module?"""
|
||||
return hasattr(mod, "__path__") and getattr(mod, "__file__", None) is None
|
||||
return hasattr(mod, '__path__') and getattr(mod, '__file__', None) is None
|
||||
|
||||
|
||||
def module_has_file(mod: ModuleType) -> bool:
|
||||
"""Does the module object `mod` have an existing __file__ ?"""
|
||||
mod__file__ = getattr(mod, "__file__", None)
|
||||
mod__file__ = getattr(mod, '__file__', None)
|
||||
if mod__file__ is None:
|
||||
return False
|
||||
return os.path.exists(mod__file__)
|
||||
|
|
@ -146,7 +155,7 @@ def add_stdlib_paths(paths: set[str]) -> None:
|
|||
# spread across a few locations. Look at all the candidate modules
|
||||
# we've imported, and take all the different ones.
|
||||
for m in modules_we_happen_to_have:
|
||||
if hasattr(m, "__file__"):
|
||||
if hasattr(m, '__file__'):
|
||||
paths.add(canonical_path(m, directory=True))
|
||||
|
||||
|
||||
|
|
@ -157,10 +166,10 @@ def add_third_party_paths(paths: set[str]) -> None:
|
|||
|
||||
for scheme in scheme_names:
|
||||
# https://foss.heptapod.net/pypy/pypy/-/issues/3433
|
||||
better_scheme = "pypy_posix" if scheme == "pypy" else scheme
|
||||
if os.name in better_scheme.split("_"):
|
||||
better_scheme = 'pypy_posix' if scheme == 'pypy' else scheme
|
||||
if os.name in better_scheme.split('_'):
|
||||
config_paths = sysconfig.get_paths(scheme)
|
||||
for path_name in ["platlib", "purelib", "scripts"]:
|
||||
for path_name in ['platlib', 'purelib', 'scripts']:
|
||||
paths.add(config_paths[path_name])
|
||||
|
||||
|
||||
|
|
@ -170,7 +179,7 @@ def add_coverage_paths(paths: set[str]) -> None:
|
|||
paths.add(cover_path)
|
||||
if env.TESTING:
|
||||
# Don't include our own test code.
|
||||
paths.add(os.path.join(cover_path, "tests"))
|
||||
paths.add(os.path.join(cover_path, 'tests'))
|
||||
|
||||
|
||||
class InOrOut:
|
||||
|
|
@ -221,7 +230,7 @@ class InOrOut:
|
|||
# The matchers for should_trace.
|
||||
|
||||
# Generally useful information
|
||||
_debug("sys.path:" + "".join(f"\n {p}" for p in sys.path))
|
||||
_debug('sys.path:' + ''.join(f'\n {p}' for p in sys.path))
|
||||
|
||||
# Create the matchers we need for should_trace
|
||||
self.source_match = None
|
||||
|
|
@ -232,28 +241,28 @@ class InOrOut:
|
|||
if self.source or self.source_pkgs:
|
||||
against = []
|
||||
if self.source:
|
||||
self.source_match = TreeMatcher(self.source, "source")
|
||||
against.append(f"trees {self.source_match!r}")
|
||||
self.source_match = TreeMatcher(self.source, 'source')
|
||||
against.append(f'trees {self.source_match!r}')
|
||||
if self.source_pkgs:
|
||||
self.source_pkgs_match = ModuleMatcher(self.source_pkgs, "source_pkgs")
|
||||
against.append(f"modules {self.source_pkgs_match!r}")
|
||||
_debug("Source matching against " + " and ".join(against))
|
||||
self.source_pkgs_match = ModuleMatcher(self.source_pkgs, 'source_pkgs')
|
||||
against.append(f'modules {self.source_pkgs_match!r}')
|
||||
_debug('Source matching against ' + ' and '.join(against))
|
||||
else:
|
||||
if self.pylib_paths:
|
||||
self.pylib_match = TreeMatcher(self.pylib_paths, "pylib")
|
||||
_debug(f"Python stdlib matching: {self.pylib_match!r}")
|
||||
self.pylib_match = TreeMatcher(self.pylib_paths, 'pylib')
|
||||
_debug(f'Python stdlib matching: {self.pylib_match!r}')
|
||||
if self.include:
|
||||
self.include_match = GlobMatcher(self.include, "include")
|
||||
_debug(f"Include matching: {self.include_match!r}")
|
||||
self.include_match = GlobMatcher(self.include, 'include')
|
||||
_debug(f'Include matching: {self.include_match!r}')
|
||||
if self.omit:
|
||||
self.omit_match = GlobMatcher(self.omit, "omit")
|
||||
_debug(f"Omit matching: {self.omit_match!r}")
|
||||
self.omit_match = GlobMatcher(self.omit, 'omit')
|
||||
_debug(f'Omit matching: {self.omit_match!r}')
|
||||
|
||||
self.cover_match = TreeMatcher(self.cover_paths, "coverage")
|
||||
_debug(f"Coverage code matching: {self.cover_match!r}")
|
||||
self.cover_match = TreeMatcher(self.cover_paths, 'coverage')
|
||||
_debug(f'Coverage code matching: {self.cover_match!r}')
|
||||
|
||||
self.third_match = TreeMatcher(self.third_paths, "third")
|
||||
_debug(f"Third-party lib matching: {self.third_match!r}")
|
||||
self.third_match = TreeMatcher(self.third_paths, 'third')
|
||||
_debug(f'Third-party lib matching: {self.third_match!r}')
|
||||
|
||||
# Check if the source we want to measure has been installed as a
|
||||
# third-party package.
|
||||
|
|
@ -263,30 +272,30 @@ class InOrOut:
|
|||
for pkg in self.source_pkgs:
|
||||
try:
|
||||
modfile, path = file_and_path_for_module(pkg)
|
||||
_debug(f"Imported source package {pkg!r} as {modfile!r}")
|
||||
_debug(f'Imported source package {pkg!r} as {modfile!r}')
|
||||
except CoverageException as exc:
|
||||
_debug(f"Couldn't import source package {pkg!r}: {exc}")
|
||||
continue
|
||||
if modfile:
|
||||
if self.third_match.match(modfile):
|
||||
_debug(
|
||||
f"Source in third-party: source_pkg {pkg!r} at {modfile!r}",
|
||||
f'Source in third-party: source_pkg {pkg!r} at {modfile!r}',
|
||||
)
|
||||
self.source_in_third_paths.add(canonical_path(source_for_file(modfile)))
|
||||
else:
|
||||
for pathdir in path:
|
||||
if self.third_match.match(pathdir):
|
||||
_debug(
|
||||
f"Source in third-party: {pkg!r} path directory at {pathdir!r}",
|
||||
f'Source in third-party: {pkg!r} path directory at {pathdir!r}',
|
||||
)
|
||||
self.source_in_third_paths.add(pathdir)
|
||||
|
||||
for src in self.source:
|
||||
if self.third_match.match(src):
|
||||
_debug(f"Source in third-party: source directory {src!r}")
|
||||
_debug(f'Source in third-party: source directory {src!r}')
|
||||
self.source_in_third_paths.add(src)
|
||||
self.source_in_third_match = TreeMatcher(self.source_in_third_paths, "source_in_third")
|
||||
_debug(f"Source in third-party matching: {self.source_in_third_match}")
|
||||
self.source_in_third_match = TreeMatcher(self.source_in_third_paths, 'source_in_third')
|
||||
_debug(f'Source in third-party matching: {self.source_in_third_match}')
|
||||
|
||||
self.plugins: Plugins
|
||||
self.disp_class: type[TFileDisposition] = FileDisposition
|
||||
|
|
@ -309,8 +318,8 @@ class InOrOut:
|
|||
disp.reason = reason
|
||||
return disp
|
||||
|
||||
if original_filename.startswith("<"):
|
||||
return nope(disp, "original file name is not real")
|
||||
if original_filename.startswith('<'):
|
||||
return nope(disp, 'original file name is not real')
|
||||
|
||||
if frame is not None:
|
||||
# Compiled Python files have two file names: frame.f_code.co_filename is
|
||||
|
|
@ -319,10 +328,10 @@ class InOrOut:
|
|||
# .pyc files can be moved after compilation (for example, by being
|
||||
# installed), we look for __file__ in the frame and prefer it to the
|
||||
# co_filename value.
|
||||
dunder_file = frame.f_globals and frame.f_globals.get("__file__")
|
||||
dunder_file = frame.f_globals and frame.f_globals.get('__file__')
|
||||
if dunder_file:
|
||||
filename = source_for_file(dunder_file)
|
||||
if original_filename and not original_filename.startswith("<"):
|
||||
if original_filename and not original_filename.startswith('<'):
|
||||
orig = os.path.basename(original_filename)
|
||||
if orig != os.path.basename(filename):
|
||||
# Files shouldn't be renamed when moved. This happens when
|
||||
|
|
@ -334,15 +343,15 @@ class InOrOut:
|
|||
# Empty string is pretty useless.
|
||||
return nope(disp, "empty string isn't a file name")
|
||||
|
||||
if filename.startswith("memory:"):
|
||||
if filename.startswith('memory:'):
|
||||
return nope(disp, "memory isn't traceable")
|
||||
|
||||
if filename.startswith("<"):
|
||||
if filename.startswith('<'):
|
||||
# Lots of non-file execution is represented with artificial
|
||||
# file names like "<string>", "<doctest readme.txt[0]>", or
|
||||
# "<exec_function>". Don't ever trace these executions, since we
|
||||
# can't do anything with the data later anyway.
|
||||
return nope(disp, "file name is not real")
|
||||
return nope(disp, 'file name is not real')
|
||||
|
||||
canonical = canonical_filename(filename)
|
||||
disp.canonical_filename = canonical
|
||||
|
|
@ -369,7 +378,7 @@ class InOrOut:
|
|||
except Exception:
|
||||
plugin_name = plugin._coverage_plugin_name
|
||||
tb = traceback.format_exc()
|
||||
self.warn(f"Disabling plug-in {plugin_name!r} due to an exception:\n{tb}")
|
||||
self.warn(f'Disabling plug-in {plugin_name!r} due to an exception:\n{tb}')
|
||||
plugin._coverage_enabled = False
|
||||
continue
|
||||
else:
|
||||
|
|
@ -402,7 +411,7 @@ class InOrOut:
|
|||
# any canned exclusions. If they didn't, then we have to exclude the
|
||||
# stdlib and coverage.py directories.
|
||||
if self.source_match or self.source_pkgs_match:
|
||||
extra = ""
|
||||
extra = ''
|
||||
ok = False
|
||||
if self.source_pkgs_match:
|
||||
if self.source_pkgs_match.match(modulename):
|
||||
|
|
@ -410,41 +419,41 @@ class InOrOut:
|
|||
if modulename in self.source_pkgs_unmatched:
|
||||
self.source_pkgs_unmatched.remove(modulename)
|
||||
else:
|
||||
extra = f"module {modulename!r} "
|
||||
extra = f'module {modulename!r} '
|
||||
if not ok and self.source_match:
|
||||
if self.source_match.match(filename):
|
||||
ok = True
|
||||
if not ok:
|
||||
return extra + "falls outside the --source spec"
|
||||
return extra + 'falls outside the --source spec'
|
||||
if self.third_match.match(filename) and not self.source_in_third_match.match(filename):
|
||||
return "inside --source, but is third-party"
|
||||
return 'inside --source, but is third-party'
|
||||
elif self.include_match:
|
||||
if not self.include_match.match(filename):
|
||||
return "falls outside the --include trees"
|
||||
return 'falls outside the --include trees'
|
||||
else:
|
||||
# We exclude the coverage.py code itself, since a little of it
|
||||
# will be measured otherwise.
|
||||
if self.cover_match.match(filename):
|
||||
return "is part of coverage.py"
|
||||
return 'is part of coverage.py'
|
||||
|
||||
# If we aren't supposed to trace installed code, then check if this
|
||||
# is near the Python standard library and skip it if so.
|
||||
if self.pylib_match and self.pylib_match.match(filename):
|
||||
return "is in the stdlib"
|
||||
return 'is in the stdlib'
|
||||
|
||||
# Exclude anything in the third-party installation areas.
|
||||
if self.third_match.match(filename):
|
||||
return "is a third-party module"
|
||||
return 'is a third-party module'
|
||||
|
||||
# Check the file against the omit pattern.
|
||||
if self.omit_match and self.omit_match.match(filename):
|
||||
return "is inside an --omit pattern"
|
||||
return 'is inside an --omit pattern'
|
||||
|
||||
# No point tracing a file we can't later write to SQLite.
|
||||
try:
|
||||
filename.encode("utf-8")
|
||||
filename.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
return "non-encodable filename"
|
||||
return 'non-encodable filename'
|
||||
|
||||
# No reason found to skip this file.
|
||||
return None
|
||||
|
|
@ -453,20 +462,20 @@ class InOrOut:
|
|||
"""Warn if there are settings that conflict."""
|
||||
if self.include:
|
||||
if self.source or self.source_pkgs:
|
||||
self.warn("--include is ignored because --source is set", slug="include-ignored")
|
||||
self.warn('--include is ignored because --source is set', slug='include-ignored')
|
||||
|
||||
def warn_already_imported_files(self) -> None:
|
||||
"""Warn if files have already been imported that we will be measuring."""
|
||||
if self.include or self.source or self.source_pkgs:
|
||||
warned = set()
|
||||
for mod in list(sys.modules.values()):
|
||||
filename = getattr(mod, "__file__", None)
|
||||
filename = getattr(mod, '__file__', None)
|
||||
if filename is None:
|
||||
continue
|
||||
if filename in warned:
|
||||
continue
|
||||
|
||||
if len(getattr(mod, "__path__", ())) > 1:
|
||||
if len(getattr(mod, '__path__', ())) > 1:
|
||||
# A namespace package, which confuses this code, so ignore it.
|
||||
continue
|
||||
|
||||
|
|
@ -477,10 +486,10 @@ class InOrOut:
|
|||
# of tracing anyway.
|
||||
continue
|
||||
if disp.trace:
|
||||
msg = f"Already imported a file that will be measured: {filename}"
|
||||
self.warn(msg, slug="already-imported")
|
||||
msg = f'Already imported a file that will be measured: {filename}'
|
||||
self.warn(msg, slug='already-imported')
|
||||
warned.add(filename)
|
||||
elif self.debug and self.debug.should("trace"):
|
||||
elif self.debug and self.debug.should('trace'):
|
||||
self.debug.write(
|
||||
"Didn't trace already imported file {!r}: {}".format(
|
||||
disp.original_filename, disp.reason,
|
||||
|
|
@ -500,7 +509,7 @@ class InOrOut:
|
|||
"""
|
||||
mod = sys.modules.get(pkg)
|
||||
if mod is None:
|
||||
self.warn(f"Module {pkg} was never imported.", slug="module-not-imported")
|
||||
self.warn(f'Module {pkg} was never imported.', slug='module-not-imported')
|
||||
return
|
||||
|
||||
if module_is_namespace(mod):
|
||||
|
|
@ -509,14 +518,14 @@ class InOrOut:
|
|||
return
|
||||
|
||||
if not module_has_file(mod):
|
||||
self.warn(f"Module {pkg} has no Python source.", slug="module-not-python")
|
||||
self.warn(f'Module {pkg} has no Python source.', slug='module-not-python')
|
||||
return
|
||||
|
||||
# The module was in sys.modules, and seems like a module with code, but
|
||||
# we never measured it. I guess that means it was imported before
|
||||
# coverage even started.
|
||||
msg = f"Module {pkg} was previously imported, but not measured"
|
||||
self.warn(msg, slug="module-not-measured")
|
||||
msg = f'Module {pkg} was previously imported, but not measured'
|
||||
self.warn(msg, slug='module-not-measured')
|
||||
|
||||
def find_possibly_unexecuted_files(self) -> Iterable[tuple[str, str | None]]:
|
||||
"""Find files in the areas of interest that might be untraced.
|
||||
|
|
@ -524,8 +533,10 @@ class InOrOut:
|
|||
Yields pairs: file path, and responsible plug-in name.
|
||||
"""
|
||||
for pkg in self.source_pkgs:
|
||||
if (pkg not in sys.modules or
|
||||
not module_has_file(sys.modules[pkg])):
|
||||
if (
|
||||
pkg not in sys.modules or
|
||||
not module_has_file(sys.modules[pkg])
|
||||
):
|
||||
continue
|
||||
pkg_file = source_for_file(cast(str, sys.modules[pkg].__file__))
|
||||
yield from self._find_executable_files(canonical_path(pkg_file))
|
||||
|
|
@ -569,16 +580,16 @@ class InOrOut:
|
|||
Returns a list of (key, value) pairs.
|
||||
"""
|
||||
info = [
|
||||
("coverage_paths", self.cover_paths),
|
||||
("stdlib_paths", self.pylib_paths),
|
||||
("third_party_paths", self.third_paths),
|
||||
("source_in_third_party_paths", self.source_in_third_paths),
|
||||
('coverage_paths', self.cover_paths),
|
||||
('stdlib_paths', self.pylib_paths),
|
||||
('third_party_paths', self.third_paths),
|
||||
('source_in_third_party_paths', self.source_in_third_paths),
|
||||
]
|
||||
|
||||
matcher_names = [
|
||||
"source_match", "source_pkgs_match",
|
||||
"include_match", "omit_match",
|
||||
"cover_match", "pylib_match", "third_match", "source_in_third_match",
|
||||
'source_match', 'source_pkgs_match',
|
||||
'include_match', 'omit_match',
|
||||
'cover_match', 'pylib_match', 'third_match', 'source_in_third_match',
|
||||
]
|
||||
|
||||
for matcher_name in matcher_names:
|
||||
|
|
@ -586,7 +597,7 @@ class InOrOut:
|
|||
if matcher:
|
||||
matcher_info = matcher.info()
|
||||
else:
|
||||
matcher_info = "-none-"
|
||||
matcher_info = '-none-'
|
||||
info.append((matcher_name, matcher_info))
|
||||
|
||||
return info
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue