[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,25 +1,27 @@
# mypy: allow-untyped-defs
from contextlib import contextmanager
from __future__ import annotations
import sys
import warnings
from contextlib import contextmanager
from typing import Generator
from typing import Literal
from typing import Optional
import warnings
import pytest
from _pytest.config import apply_warning_filters
from _pytest.config import Config
from _pytest.config import parse_warning_filter
from _pytest.main import Session
from _pytest.nodes import Item
from _pytest.terminal import TerminalReporter
import pytest
def pytest_configure(config: Config) -> None:
config.addinivalue_line(
"markers",
"filterwarnings(warning): add a warning filter to the given test. "
"see https://docs.pytest.org/en/stable/how-to/capture-warnings.html#pytest-mark-filterwarnings ",
'markers',
'filterwarnings(warning): add a warning filter to the given test. '
'see https://docs.pytest.org/en/stable/how-to/capture-warnings.html#pytest-mark-filterwarnings ',
)
@ -27,8 +29,8 @@ def pytest_configure(config: Config) -> None:
def catch_warnings_for_item(
config: Config,
ihook,
when: Literal["config", "collect", "runtest"],
item: Optional[Item],
when: Literal['config', 'collect', 'runtest'],
item: Item | None,
) -> Generator[None, None, None]:
"""Context manager that catches warnings generated in the contained execution block.
@ -36,7 +38,7 @@ def catch_warnings_for_item(
Each warning captured triggers the ``pytest_warning_recorded`` hook.
"""
config_filters = config.getini("filterwarnings")
config_filters = config.getini('filterwarnings')
cmdline_filters = config.known_args_namespace.pythonwarnings or []
with warnings.catch_warnings(record=True) as log:
# mypy can't infer that record=True means log is not None; help it.
@ -44,8 +46,8 @@ def catch_warnings_for_item(
if not sys.warnoptions:
# If user is not explicitly configuring warning filters, show deprecation warnings by default (#2908).
warnings.filterwarnings("always", category=DeprecationWarning)
warnings.filterwarnings("always", category=PendingDeprecationWarning)
warnings.filterwarnings('always', category=DeprecationWarning)
warnings.filterwarnings('always', category=PendingDeprecationWarning)
# To be enabled in pytest 9.0.0.
# warnings.filterwarnings("error", category=pytest.PytestRemovedIn9Warning)
@ -53,9 +55,9 @@ def catch_warnings_for_item(
apply_warning_filters(config_filters, cmdline_filters)
# apply filters from "filterwarnings" marks
nodeid = "" if item is None else item.nodeid
nodeid = '' if item is None else item.nodeid
if item is not None:
for mark in item.iter_markers(name="filterwarnings"):
for mark in item.iter_markers(name='filterwarnings'):
for arg in mark.args:
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
@ -69,7 +71,7 @@ def catch_warnings_for_item(
nodeid=nodeid,
when=when,
location=None,
)
),
)
@ -91,22 +93,22 @@ def warning_record_to_str(warning_message: warnings.WarningMessage) -> str:
else:
tb = tracemalloc.get_object_traceback(warning_message.source)
if tb is not None:
formatted_tb = "\n".join(tb.format())
formatted_tb = '\n'.join(tb.format())
# Use a leading new line to better separate the (large) output
# from the traceback to the previous warning text.
msg += f"\nObject allocated at:\n{formatted_tb}"
msg += f'\nObject allocated at:\n{formatted_tb}'
else:
# No need for a leading new line.
url = "https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings"
msg += "Enable tracemalloc to get traceback where the object was allocated.\n"
msg += f"See {url} for more info."
url = 'https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings'
msg += 'Enable tracemalloc to get traceback where the object was allocated.\n'
msg += f'See {url} for more info.'
return msg
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
with catch_warnings_for_item(
config=item.config, ihook=item.ihook, when="runtest", item=item
config=item.config, ihook=item.ihook, when='runtest', item=item,
):
return (yield)
@ -115,7 +117,7 @@ def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
def pytest_collection(session: Session) -> Generator[None, object, object]:
config = session.config
with catch_warnings_for_item(
config=config, ihook=config.hook, when="collect", item=None
config=config, ihook=config.hook, when='collect', item=None,
):
return (yield)
@ -126,7 +128,7 @@ def pytest_terminal_summary(
) -> Generator[None, None, None]:
config = terminalreporter.config
with catch_warnings_for_item(
config=config, ihook=config.hook, when="config", item=None
config=config, ihook=config.hook, when='config', item=None,
):
return (yield)
@ -135,16 +137,16 @@ def pytest_terminal_summary(
def pytest_sessionfinish(session: Session) -> Generator[None, None, None]:
config = session.config
with catch_warnings_for_item(
config=config, ihook=config.hook, when="config", item=None
config=config, ihook=config.hook, when='config', item=None,
):
return (yield)
@pytest.hookimpl(wrapper=True)
def pytest_load_initial_conftests(
early_config: "Config",
early_config: Config,
) -> Generator[None, None, None]:
with catch_warnings_for_item(
config=early_config, ihook=early_config.hook, when="config", item=None
config=early_config, ihook=early_config.hook, when='config', item=None,
):
return (yield)