mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 13:24:18 +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,17 +1,16 @@
|
|||
# mypy: allow-untyped-defs
|
||||
"""Python version compatibility code."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import enum
|
||||
import functools
|
||||
import inspect
|
||||
import os
|
||||
import sys
|
||||
from inspect import Parameter
|
||||
from inspect import signature
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import Final
|
||||
|
|
@ -57,7 +56,7 @@ def iscoroutinefunction(func: object) -> bool:
|
|||
importing asyncio directly, which in turns also initializes the "logging"
|
||||
module as a side-effect (see issue #8).
|
||||
"""
|
||||
return inspect.iscoroutinefunction(func) or getattr(func, "_is_coroutine", False)
|
||||
return inspect.iscoroutinefunction(func) or getattr(func, '_is_coroutine', False)
|
||||
|
||||
|
||||
def is_async_function(func: object) -> bool:
|
||||
|
|
@ -76,33 +75,33 @@ def getlocation(function, curdir: str | os.PathLike[str] | None = None) -> str:
|
|||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
return "%s:%d" % (relfn, lineno + 1)
|
||||
return "%s:%d" % (fn, lineno + 1)
|
||||
return '%s:%d' % (relfn, lineno + 1)
|
||||
return '%s:%d' % (fn, lineno + 1)
|
||||
|
||||
|
||||
def num_mock_patch_args(function) -> int:
|
||||
"""Return number of arguments used up by mock arguments (if any)."""
|
||||
patchings = getattr(function, "patchings", None)
|
||||
patchings = getattr(function, 'patchings', None)
|
||||
if not patchings:
|
||||
return 0
|
||||
|
||||
mock_sentinel = getattr(sys.modules.get("mock"), "DEFAULT", object())
|
||||
ut_mock_sentinel = getattr(sys.modules.get("unittest.mock"), "DEFAULT", object())
|
||||
mock_sentinel = getattr(sys.modules.get('mock'), 'DEFAULT', object())
|
||||
ut_mock_sentinel = getattr(sys.modules.get('unittest.mock'), 'DEFAULT', object())
|
||||
|
||||
return len(
|
||||
[
|
||||
p
|
||||
for p in patchings
|
||||
if not p.attribute_name
|
||||
and (p.new is mock_sentinel or p.new is ut_mock_sentinel)
|
||||
]
|
||||
if not p.attribute_name and
|
||||
(p.new is mock_sentinel or p.new is ut_mock_sentinel)
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def getfuncargnames(
|
||||
function: Callable[..., object],
|
||||
*,
|
||||
name: str = "",
|
||||
name: str = '',
|
||||
is_method: bool = False,
|
||||
cls: type | None = None,
|
||||
) -> tuple[str, ...]:
|
||||
|
|
@ -135,7 +134,7 @@ def getfuncargnames(
|
|||
from _pytest.outcomes import fail
|
||||
|
||||
fail(
|
||||
f"Could not determine arguments of {function!r}: {e}",
|
||||
f'Could not determine arguments of {function!r}: {e}',
|
||||
pytrace=False,
|
||||
)
|
||||
|
||||
|
|
@ -143,10 +142,10 @@ def getfuncargnames(
|
|||
p.name
|
||||
for p in parameters.values()
|
||||
if (
|
||||
p.kind is Parameter.POSITIONAL_OR_KEYWORD
|
||||
or p.kind is Parameter.KEYWORD_ONLY
|
||||
)
|
||||
and p.default is Parameter.empty
|
||||
p.kind is Parameter.POSITIONAL_OR_KEYWORD or
|
||||
p.kind is Parameter.KEYWORD_ONLY
|
||||
) and
|
||||
p.default is Parameter.empty
|
||||
)
|
||||
if not name:
|
||||
name = function.__name__
|
||||
|
|
@ -157,15 +156,15 @@ def getfuncargnames(
|
|||
if is_method or (
|
||||
# Not using `getattr` because we don't want to resolve the staticmethod.
|
||||
# Not using `cls.__dict__` because we want to check the entire MRO.
|
||||
cls
|
||||
and not isinstance(
|
||||
inspect.getattr_static(cls, name, default=None), staticmethod
|
||||
cls and
|
||||
not isinstance(
|
||||
inspect.getattr_static(cls, name, default=None), staticmethod,
|
||||
)
|
||||
):
|
||||
arg_names = arg_names[1:]
|
||||
# Remove any names that will be replaced with mocks.
|
||||
if hasattr(function, "__wrapped__"):
|
||||
arg_names = arg_names[num_mock_patch_args(function) :]
|
||||
if hasattr(function, '__wrapped__'):
|
||||
arg_names = arg_names[num_mock_patch_args(function):]
|
||||
return arg_names
|
||||
|
||||
|
||||
|
|
@ -176,16 +175,16 @@ def get_default_arg_names(function: Callable[..., Any]) -> tuple[str, ...]:
|
|||
return tuple(
|
||||
p.name
|
||||
for p in signature(function).parameters.values()
|
||||
if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)
|
||||
and p.default is not Parameter.empty
|
||||
if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY) and
|
||||
p.default is not Parameter.empty
|
||||
)
|
||||
|
||||
|
||||
_non_printable_ascii_translate_table = {
|
||||
i: f"\\x{i:02x}" for i in range(128) if i not in range(32, 127)
|
||||
i: f'\\x{i:02x}' for i in range(128) if i not in range(32, 127)
|
||||
}
|
||||
_non_printable_ascii_translate_table.update(
|
||||
{ord("\t"): "\\t", ord("\r"): "\\r", ord("\n"): "\\n"}
|
||||
{ord('\t'): '\\t', ord('\r'): '\\r', ord('\n'): '\\n'},
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -206,9 +205,9 @@ def ascii_escaped(val: bytes | str) -> str:
|
|||
a UTF-8 string.
|
||||
"""
|
||||
if isinstance(val, bytes):
|
||||
ret = val.decode("ascii", "backslashreplace")
|
||||
ret = val.decode('ascii', 'backslashreplace')
|
||||
else:
|
||||
ret = val.encode("unicode_escape").decode("ascii")
|
||||
ret = val.encode('unicode_escape').decode('ascii')
|
||||
return ret.translate(_non_printable_ascii_translate_table)
|
||||
|
||||
|
||||
|
|
@ -232,11 +231,11 @@ def get_real_func(obj):
|
|||
# __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function
|
||||
# to trigger a warning if it gets called directly instead of by pytest: we don't
|
||||
# want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774)
|
||||
new_obj = getattr(obj, "__pytest_wrapped__", None)
|
||||
new_obj = getattr(obj, '__pytest_wrapped__', None)
|
||||
if isinstance(new_obj, _PytestWrapper):
|
||||
obj = new_obj.obj
|
||||
break
|
||||
new_obj = getattr(obj, "__wrapped__", None)
|
||||
new_obj = getattr(obj, '__wrapped__', None)
|
||||
if new_obj is None:
|
||||
break
|
||||
obj = new_obj
|
||||
|
|
@ -244,7 +243,7 @@ def get_real_func(obj):
|
|||
from _pytest._io.saferepr import saferepr
|
||||
|
||||
raise ValueError(
|
||||
f"could not find real function of {saferepr(start_obj)}\nstopped at {saferepr(obj)}"
|
||||
f'could not find real function of {saferepr(start_obj)}\nstopped at {saferepr(obj)}',
|
||||
)
|
||||
if isinstance(obj, functools.partial):
|
||||
obj = obj.func
|
||||
|
|
@ -256,11 +255,11 @@ def get_real_method(obj, holder):
|
|||
``obj``, while at the same time returning a bound method to ``holder`` if
|
||||
the original object was a bound method."""
|
||||
try:
|
||||
is_method = hasattr(obj, "__func__")
|
||||
is_method = hasattr(obj, '__func__')
|
||||
obj = get_real_func(obj)
|
||||
except Exception: # pragma: no cover
|
||||
return obj
|
||||
if is_method and hasattr(obj, "__get__") and callable(obj.__get__):
|
||||
if is_method and hasattr(obj, '__get__') and callable(obj.__get__):
|
||||
obj = obj.__get__(holder)
|
||||
return obj
|
||||
|
||||
|
|
@ -306,7 +305,7 @@ def get_user_id() -> int | None:
|
|||
# mypy follows the version and platform checking expectation of PEP 484:
|
||||
# https://mypy.readthedocs.io/en/stable/common_issues.html?highlight=platform#python-version-and-system-platform-checks
|
||||
# Containment checks are too complex for mypy v1.5.0 and cause failure.
|
||||
if sys.platform == "win32" or sys.platform == "emscripten": # noqa: PLR1714
|
||||
if sys.platform == 'win32' or sys.platform == 'emscripten': # noqa: PLR1714
|
||||
# win32 does not have a getuid() function.
|
||||
# Emscripten has a return 0 stub.
|
||||
return None
|
||||
|
|
@ -350,4 +349,4 @@ def get_user_id() -> int | None:
|
|||
#
|
||||
# This also work for Enums (if you use `is` to compare) and Literals.
|
||||
def assert_never(value: NoReturn) -> NoReturn:
|
||||
assert False, f"Unhandled value: {value} ({type(value).__name__})"
|
||||
assert False, f'Unhandled value: {value} ({type(value).__name__})'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue