Skip to content

Commit

Permalink
typing: use typing.overload directly
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Nov 22, 2020
1 parent ffb11a5 commit 579e756
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 141 deletions.
17 changes: 9 additions & 8 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
102 changes: 52 additions & 50 deletions src/_pytest/_code/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
33 changes: 14 additions & 19 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
55 changes: 28 additions & 27 deletions src/_pytest/pytester/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,6 +55,7 @@


if TYPE_CHECKING:
from typing import overload
from typing import Type
from typing_extensions import Literal # noqa: F401

Expand Down Expand Up @@ -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,
Expand Down
35 changes: 18 additions & 17 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 579e756

Please sign in to comment.