diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 3b4a62a4fa0..dfa6db1e938 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -199,8 +199,8 @@ def __init__( rawentry: TracebackType, repr_style: Optional['Literal["short", "long"]'] = None, ) -> None: - self._rawentry: "Final" = rawentry - self._repr_style: "Final" = repr_style + self._rawentry: Final = rawentry + self._repr_style: Final = repr_style def with_repr_style( self, repr_style: Optional['Literal["short", "long"]'] @@ -454,7 +454,7 @@ def __init__( self, excinfo: Optional[Tuple[Type["E"], "E", TracebackType]], striptext: str = "", - traceback: Optional[Traceback] = None, + traceback: Optional["Traceback"] = None, *, _ispytest: bool = False, ) -> None: @@ -545,33 +545,33 @@ def fill_unfilled(self, exc_info: Tuple[Type[E], E, TracebackType]) -> None: @property def type(self) -> Type[E]: """The exception class.""" - assert ( - self._excinfo is not None - ), ".type can only be used after the context manager exits" + assert self._excinfo is not None, ( + ".type can only be used after the context manager exits" + ) return self._excinfo[0] @property def value(self) -> E: """The exception value.""" - assert ( - self._excinfo is not None - ), ".value can only be used after the context manager exits" + assert self._excinfo is not None, ( + ".value can only be used after the context manager exits" + ) return self._excinfo[1] @property def tb(self) -> TracebackType: """The exception raw traceback.""" - assert ( - self._excinfo is not None - ), ".tb can only be used after the context manager exits" + assert self._excinfo is not None, ( + ".tb can only be used after the context manager exits" + ) return self._excinfo[2] @property def typename(self) -> str: """The type name of the exception.""" - assert ( - self._excinfo is not None - ), ".typename can only be used after the context manager exits" + assert self._excinfo is not None, ( + ".typename can only be used after the context manager exits" + ) return self.type.__name__ @property @@ -942,7 +942,7 @@ def repr_traceback_entry( if short: message = "in %s" % (entry.name) else: - message = excinfo and excinfo.typename or "" + message = (excinfo and excinfo.typename) or "" entry_path = entry.path path = self._makepath(entry_path) reprfileloc = ReprFileLocation(path, entry.lineno + 1, message) @@ -1177,10 +1177,8 @@ def toterminal(self, tw: TerminalWriter) -> None: entry.toterminal(tw) if i < len(self.reprentries) - 1: next_entry = self.reprentries[i + 1] - if ( - entry.style == "long" - or entry.style == "short" - and next_entry.style == "long" + if entry.style == "long" or ( + entry.style == "short" and next_entry.style == "long" ): tw.sep(self.entrysep) @@ -1358,7 +1356,7 @@ def getfslineno(obj: object) -> Tuple[Union[str, Path], int]: except TypeError: return "", -1 - fspath = fn and absolutepath(fn) or "" + fspath = (fn and absolutepath(fn)) or "" lineno = -1 if fspath: try: diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 7ff27643f10..f83ec6bdd9b 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -2,6 +2,7 @@ """Command line options, ini-file and conftest.py processing.""" import argparse +import builtins as _builtins import collections.abc import copy import dataclasses @@ -15,7 +16,6 @@ import re import shlex import sys -from textwrap import dedent import types from types import FunctionType from typing import Any @@ -1484,9 +1484,9 @@ def _get_unknown_ini_keys(self) -> List[str]: def parse(self, args: List[str], addopts: bool = True) -> None: # Parse given cmdline arguments into this config object. - assert ( - self.args == [] - ), "can only parse cmdline args at most once per Config object" + assert self.args == [], ( + "can only parse cmdline args at most once per Config object" + ) self.hook.pytest_addhooks.call_historic( kwargs=dict(pluginmanager=self.pluginmanager) ) @@ -1874,39 +1874,33 @@ def parse_warning_filter( * Raises UsageError so we get nice error messages on failure. """ __tracebackhide__ = True - error_template = dedent( - f"""\ - while parsing the following warning configuration: - - {arg} - - This error occurred: - {{error}} - """ + arg_str = arg + error_template = ( + "while parsing the following warning configuration:\n\n" + f" {arg_str}\n\n" + "This error occurred:\n\n" + "{error}\n" ) parts = arg.split(":") - if len(parts) > 5: + nparts = len(parts) + if nparts > 5: doc_url = ( "https://docs.python.org/3/library/warnings.html#describing-warning-filters" ) - error = dedent( - f"""\ - Too many fields ({len(parts)}), expected at most 5 separated by colons: - - action:message:category:module:line - - For more information please consult: {doc_url} - """ + error = ( + f"Too many fields ({nparts}), expected at most 5 separated by colons:\n\n" + " action:message:category:module:line\n\n" + f"For more information please consult: {doc_url}\n" ) raise UsageError(error_template.format(error=error)) - while len(parts) < 5: - parts.append("") + if nparts < 5: + parts += [""] * (5 - nparts) action_, message, category_, module, lineno_ = (s.strip() for s in parts) try: - action: "warnings._ActionKind" = warnings._getaction(action_) # type: ignore[attr-defined] + action: warnings._ActionKind = warnings._getaction(action_) # type: ignore[attr-defined] except warnings._OptionError as e: raise UsageError(error_template.format(error=str(e))) from None try: @@ -1943,12 +1937,12 @@ def _resolve_warning_category(category: str) -> Type[Warning]: return Warning if "." not in category: - import builtins as m + m = _builtins klass = category else: module, _, klass = category.rpartition(".") - m = __import__(module, None, None, [klass]) + m = __import__(module, None, None, (klass,)) cat = getattr(m, klass) if not issubclass(cat, Warning): raise UsageError(f"{cat} is not a Warning subclass")