diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 19a7da55e5..bc83ccf84b 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -32,11 +32,11 @@ from _pytest._io import TerminalWriter from _pytest._io.saferepr import safeformat from _pytest._io.saferepr import saferepr -from _pytest.compat import overload from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import OutcomeException if TYPE_CHECKING: + from typing import overload from typing import Type from typing_extensions import Literal from weakref import ReferenceType # noqa: F401 @@ -362,15 +362,16 @@ def cut( return Traceback(x._rawentry, self._excinfo) return self - @overload - def __getitem__(self, key: int) -> TracebackEntry: - raise NotImplementedError() + if TYPE_CHECKING: + @overload + def __getitem__(self, key: int) -> TracebackEntry: + ... - @overload # noqa: F811 - def __getitem__(self, key: slice) -> "Traceback": # noqa: F811 - raise NotImplementedError() + @overload + def __getitem__(self, key: slice) -> "Traceback": + ... - def __getitem__( # noqa: F811 + def __getitem__( self, key: Union[int, slice] ) -> Union[TracebackEntry, "Traceback"]: if isinstance(key, slice): diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index 7faf51e272..bee6805e0c 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -19,10 +19,10 @@ import py.path from _pytest.compat import get_real_func -from _pytest.compat import overload from _pytest.compat import TYPE_CHECKING if TYPE_CHECKING: + from typing import overload from typing_extensions import Literal @@ -62,15 +62,16 @@ def __eq__(self, other): # Ignore type because of https://github.com/python/mypy/issues/4266. __hash__ = None # type: ignore - @overload - def __getitem__(self, key: int) -> str: - raise NotImplementedError() + if TYPE_CHECKING: + @overload + def __getitem__(self, key: int) -> str: + ... - @overload # noqa: F811 - def __getitem__(self, key: slice) -> "Source": # noqa: F811 - raise NotImplementedError() + @overload + def __getitem__(self, key: slice) -> "Source": + ... - def __getitem__(self, key: Union[int, slice]) -> Union[str, "Source"]: # noqa: F811 + def __getitem__(self, key: Union[int, slice]) -> Union[str, "Source"]: if isinstance(key, int): return self.lines[key] else: @@ -160,29 +161,30 @@ def isparseable(self, deindent: bool = True) -> bool: def __str__(self) -> str: return "\n".join(self.lines) - @overload - def compile( - self, - filename: Optional[str] = ..., - mode: str = ..., - flag: "Literal[0]" = ..., - dont_inherit: int = ..., - _genframe: Optional[FrameType] = ..., - ) -> CodeType: - raise NotImplementedError() - - @overload # noqa: F811 - def compile( # noqa: F811 - self, - filename: Optional[str] = ..., - mode: str = ..., - flag: int = ..., - dont_inherit: int = ..., - _genframe: Optional[FrameType] = ..., - ) -> Union[CodeType, ast.AST]: - raise NotImplementedError() + if TYPE_CHECKING: + @overload + def compile( + self, + filename: Optional[str] = ..., + mode: str = ..., + flag: "Literal[0]" = ..., + dont_inherit: int = ..., + _genframe: Optional[FrameType] = ..., + ) -> CodeType: + ... + + @overload + def compile( + self, + filename: Optional[str] = ..., + mode: str = ..., + flag: int = ..., + dont_inherit: int = ..., + _genframe: Optional[FrameType] = ..., + ) -> Union[CodeType, ast.AST]: + ... - def compile( # noqa: F811 + def compile( self, filename: Optional[str] = None, mode: str = "exec", @@ -234,29 +236,29 @@ def compile( # noqa: F811 # -@overload -def compile_( - source: Union[str, bytes, ast.mod, ast.AST], - filename: Optional[str] = ..., - mode: str = ..., - flags: "Literal[0]" = ..., - dont_inherit: int = ..., -) -> CodeType: - raise NotImplementedError() - +if TYPE_CHECKING: + @overload + def compile_( + source: Union[str, bytes, ast.mod, ast.AST], + filename: Optional[str] = ..., + mode: str = ..., + flags: "Literal[0]" = ..., + dont_inherit: int = ..., + ) -> CodeType: + ... -@overload # noqa: F811 -def compile_( # noqa: F811 - source: Union[str, bytes, ast.mod, ast.AST], - filename: Optional[str] = ..., - mode: str = ..., - flags: int = ..., - dont_inherit: int = ..., -) -> Union[CodeType, ast.AST]: - raise NotImplementedError() + @overload + def compile_( + source: Union[str, bytes, ast.mod, ast.AST], + filename: Optional[str] = ..., + mode: str = ..., + flags: int = ..., + dont_inherit: int = ..., + ) -> Union[CodeType, ast.AST]: + ... -def compile_( # noqa: F811 +def compile_( source: Union[str, bytes, ast.mod, ast.AST], filename: Optional[str] = None, mode: str = "exec", diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index bd7afe880c..a43b9f1085 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -16,7 +16,6 @@ from typing import Generic from typing import IO from typing import Optional -from typing import overload from typing import Tuple from typing import TypeVar from typing import Union @@ -31,8 +30,9 @@ if TYPE_CHECKING: - from typing import List from types import ModuleType # noqa: F401 (used in type string) + from typing import List + from typing import overload from typing import Type # noqa: F401 (used in type string) @@ -396,12 +396,6 @@ def write(self, s) -> int: return self._other.write(s) -if sys.version_info < (3, 5, 2): - - def overload(f): # noqa: F811 - return f - - if getattr(attr, "__version_info__", ()) >= (19, 2): ATTRS_EQ_FIELD = "eq" else: @@ -425,19 +419,20 @@ def __init__(self, func: Callable[[_S], _T]) -> None: self.func = func self.__doc__ = func.__doc__ - @overload - def __get__( - self, instance: None, owner: Optional["Type[_S]"] = ... - ) -> "cached_property[_S, _T]": - raise NotImplementedError() + if TYPE_CHECKING: + @overload + def __get__( + self, instance: None, owner: Optional["Type[_S]"] = ... + ) -> "cached_property[_S, _T]": + ... - @overload # noqa: F811 - def __get__( # noqa: F811 - self, instance: _S, owner: Optional["Type[_S]"] = ... - ) -> _T: - raise NotImplementedError() + @overload + def __get__( + self, instance: _S, owner: Optional["Type[_S]"] = ... + ) -> _T: + ... - def __get__(self, instance, owner=None): # noqa: F811 + def __get__(self, instance, owner=None): if instance is None: return self value = instance.__dict__[self.func.__name__] = self.func(instance) diff --git a/src/_pytest/pytester/__init__.py b/src/_pytest/pytester/__init__.py index 5097665bc1..6877426e10 100644 --- a/src/_pytest/pytester/__init__.py +++ b/src/_pytest/pytester/__init__.py @@ -36,7 +36,6 @@ from _pytest.capture import CloseStdinType from _pytest.capture import MultiCapture from _pytest.capture import SysCapture -from _pytest.compat import overload from _pytest.compat import TYPE_CHECKING from _pytest.config import _PluggyPlugin from _pytest.config import Config @@ -56,6 +55,7 @@ if TYPE_CHECKING: + from typing import overload from typing import Type from typing_extensions import Literal # noqa: F401 @@ -1282,33 +1282,34 @@ def collect_by_name( return colitem return None - @overload - def popen( - self, - cmdargs, - stdout: Optional[Union[int, IO]] = subprocess.PIPE, - stderr: Optional[Union[int, IO]] = subprocess.PIPE, - stdin: Optional[Union[CloseStdinType, bytes, str, int, IO]] = CLOSE_STDIN, - *, - encoding: None = ..., - **kw - ) -> "subprocess.Popen[bytes]": - ... - - @overload - def popen( # noqa: F811 - self, - cmdargs, - stdout: Optional[Union[int, IO]] = subprocess.PIPE, - stderr: Optional[Union[int, IO]] = subprocess.PIPE, - stdin: Optional[Union[CloseStdinType, bytes, str, int, IO]] = CLOSE_STDIN, - *, - encoding: str, - **kw - ) -> "subprocess.Popen[str]": - ... + if TYPE_CHECKING: + @overload + def popen( + self, + cmdargs, + stdout: Optional[Union[int, IO]] = subprocess.PIPE, + stderr: Optional[Union[int, IO]] = subprocess.PIPE, + stdin: Optional[Union[CloseStdinType, bytes, str, int, IO]] = CLOSE_STDIN, + *, + encoding: None = ..., + **kw + ) -> "subprocess.Popen[bytes]": + ... + + @overload + def popen( + self, + cmdargs, + stdout: Optional[Union[int, IO]] = subprocess.PIPE, + stderr: Optional[Union[int, IO]] = subprocess.PIPE, + stdin: Optional[Union[CloseStdinType, bytes, str, int, IO]] = CLOSE_STDIN, + *, + encoding: str, + **kw + ) -> "subprocess.Popen[str]": + ... - def popen( # noqa: F811 + def popen( self, cmdargs, stdout: Optional[Union[int, IO]] = subprocess.PIPE, diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7f52778b9b..b02ed43b7d 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -21,12 +21,12 @@ from more_itertools.more import always_iterable import _pytest._code -from _pytest.compat import overload from _pytest.compat import STRING_TYPES from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import fail if TYPE_CHECKING: + from typing import overload from typing import Type # noqa: F401 (used in type string) @@ -543,26 +543,27 @@ def _is_numpy_array(obj): _E = TypeVar("_E", bound=BaseException) -@overload -def raises( - expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], - *, - match: "Optional[Union[str, Pattern]]" = ... -) -> "RaisesContext[_E]": - ... # pragma: no cover +if TYPE_CHECKING: + @overload + def raises( + expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], + *, + match: "Optional[Union[str, Pattern]]" = ... + ) -> "RaisesContext[_E]": + ... -@overload # noqa: F811 -def raises( # noqa: F811 - expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], - func: Callable, - *args: Any, - **kwargs: Any -) -> _pytest._code.ExceptionInfo[_E]: - ... # pragma: no cover + @overload + def raises( + expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], + func: Callable, + *args: Any, + **kwargs: Any + ) -> _pytest._code.ExceptionInfo[_E]: + ... -def raises( # noqa: F811 +def raises( expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], *args: Any, **kwargs: Any diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index cb94fc79fc..006b235c9e 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -11,12 +11,12 @@ from typing import Tuple from typing import Union -from _pytest.compat import overload from _pytest.compat import TYPE_CHECKING from _pytest.fixtures import yield_fixture from _pytest.outcomes import fail if TYPE_CHECKING: + from typing import overload from typing import Type @@ -55,27 +55,28 @@ def deprecated_call(func=None, *args, **kwargs): return warns((DeprecationWarning, PendingDeprecationWarning), *args, **kwargs) -@overload -def warns( - expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], - *, - match: "Optional[Union[str, Pattern]]" = ... -) -> "WarningsChecker": - raise NotImplementedError() - - -@overload # noqa: F811 -def warns( # noqa: F811 - expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], - func: Callable, - *args: Any, - match: Optional[Union[str, "Pattern"]] = ..., - **kwargs: Any -) -> Union[Any]: - raise NotImplementedError() +if TYPE_CHECKING: + @overload + def warns( + expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], + *, + match: "Optional[Union[str, Pattern]]" = ... + ) -> "WarningsChecker": + ... + + + @overload + def warns( + expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], + func: Callable, + *args: Any, + match: Optional[Union[str, "Pattern"]] = ..., + **kwargs: Any + ) -> Union[Any]: + ... -def warns( # noqa: F811 +def warns( expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], *args: Any, match: Optional[Union[str, "Pattern"]] = None,