Skip to content

Commit

Permalink
Merge pull request #7840 from asottile/py36_typing_Type
Browse files Browse the repository at this point in the history
py36+: from typing import Type: no longer need guard
  • Loading branch information
asottile committed Oct 3, 2020
2 parents aa077ab + bfadd40 commit 6ed07a1
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 93 deletions.
12 changes: 6 additions & 6 deletions src/_pytest/_code/code.py
Expand Up @@ -21,6 +21,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
Expand All @@ -44,7 +45,6 @@
from _pytest.pathlib import Path

if TYPE_CHECKING:
from typing import Type
from typing_extensions import Literal
from weakref import ReferenceType

Expand Down Expand Up @@ -421,14 +421,14 @@ class ExceptionInfo(Generic[_E]):

_assert_start_repr = "AssertionError('assert "

_excinfo = attr.ib(type=Optional[Tuple["Type[_E]", "_E", TracebackType]])
_excinfo = attr.ib(type=Optional[Tuple[Type["_E"], "_E", TracebackType]])
_striptext = attr.ib(type=str, default="")
_traceback = attr.ib(type=Optional[Traceback], default=None)

@classmethod
def from_exc_info(
cls,
exc_info: Tuple["Type[_E]", "_E", TracebackType],
exc_info: Tuple[Type[_E], _E, TracebackType],
exprinfo: Optional[str] = None,
) -> "ExceptionInfo[_E]":
"""Return an ExceptionInfo for an existing exc_info tuple.
Expand Down Expand Up @@ -479,13 +479,13 @@ def for_later(cls) -> "ExceptionInfo[_E]":
"""Return an unfilled ExceptionInfo."""
return cls(None)

def fill_unfilled(self, exc_info: Tuple["Type[_E]", _E, TracebackType]) -> None:
def fill_unfilled(self, exc_info: Tuple[Type[_E], _E, TracebackType]) -> None:
"""Fill an unfilled ExceptionInfo created with ``for_later()``."""
assert self._excinfo is None, "ExceptionInfo was already filled"
self._excinfo = exc_info

@property
def type(self) -> "Type[_E]":
def type(self) -> Type[_E]:
"""The exception class."""
assert (
self._excinfo is not None
Expand Down Expand Up @@ -551,7 +551,7 @@ def exconly(self, tryshort: bool = False) -> str:
return text

def errisinstance(
self, exc: Union["Type[BaseException]", Tuple["Type[BaseException]", ...]]
self, exc: Union[Type[BaseException], Tuple[Type[BaseException], ...]]
) -> bool:
"""Return True if the exception is an instance of exc.
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/compat.py
Expand Up @@ -24,7 +24,6 @@

if TYPE_CHECKING:
from typing import NoReturn
from typing import Type
from typing_extensions import Final


Expand Down Expand Up @@ -349,6 +348,7 @@ def final(f): # noqa: F811
if sys.version_info >= (3, 8):
from functools import cached_property as cached_property
else:
from typing import Type

class cached_property(Generic[_S, _T]):
__slots__ = ("func", "__doc__")
Expand All @@ -359,13 +359,13 @@ def __init__(self, func: Callable[[_S], _T]) -> None:

@overload
def __get__(
self, instance: None, owner: Optional["Type[_S]"] = ...
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]"] = ...
self, instance: _S, owner: Optional[Type[_S]] = ...
) -> _T:
...

Expand Down
10 changes: 4 additions & 6 deletions src/_pytest/config/__init__.py
Expand Up @@ -26,6 +26,7 @@
from typing import Set
from typing import TextIO
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union

Expand Down Expand Up @@ -56,7 +57,6 @@
from _pytest.warning_types import PytestConfigWarning

if TYPE_CHECKING:
from typing import Type

from _pytest._code.code import _TracebackStyle
from _pytest.terminal import TerminalReporter
Expand Down Expand Up @@ -104,7 +104,7 @@ class ConftestImportFailure(Exception):
def __init__(
self,
path: py.path.local,
excinfo: Tuple["Type[Exception]", Exception, TracebackType],
excinfo: Tuple[Type[Exception], Exception, TracebackType],
) -> None:
super().__init__(path, excinfo)
self.path = path
Expand Down Expand Up @@ -1560,7 +1560,7 @@ def _strtobool(val: str) -> bool:
@lru_cache(maxsize=50)
def parse_warning_filter(
arg: str, *, escape: bool
) -> "Tuple[str, str, Type[Warning], str, int]":
) -> Tuple[str, str, Type[Warning], str, int]:
"""Parse a warnings filter string.
This is copied from warnings._setoption, but does not apply the filter,
Expand All @@ -1573,9 +1573,7 @@ def parse_warning_filter(
parts.append("")
action_, message, category_, module, lineno_ = [s.strip() for s in parts]
action = warnings._getaction(action_) # type: str # type: ignore[attr-defined]
category = warnings._getcategory(
category_
) # type: Type[Warning] # type: ignore[attr-defined]
category: Type[Warning] = warnings._getcategory(category_) # type: ignore[attr-defined]
if message and escape:
message = re.escape(message)
if module and escape:
Expand Down
3 changes: 1 addition & 2 deletions src/_pytest/debugging.py
Expand Up @@ -9,6 +9,7 @@
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union

Expand All @@ -24,8 +25,6 @@
from _pytest.reports import BaseReport

if TYPE_CHECKING:
from typing import Type

from _pytest.capture import CaptureManager
from _pytest.runner import CallInfo

Expand Down
8 changes: 4 additions & 4 deletions src/_pytest/doctest.py
Expand Up @@ -17,6 +17,7 @@
from typing import Pattern
from typing import Sequence
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union

Expand All @@ -40,7 +41,6 @@

if TYPE_CHECKING:
import doctest
from typing import Type

DOCTEST_REPORT_CHOICE_NONE = "none"
DOCTEST_REPORT_CHOICE_CDIFF = "cdiff"
Expand Down Expand Up @@ -168,7 +168,7 @@ def __init__(self, failures: "Sequence[doctest.DocTestFailure]") -> None:
self.failures = failures


def _init_runner_class() -> "Type[doctest.DocTestRunner]":
def _init_runner_class() -> Type["doctest.DocTestRunner"]:
import doctest

class PytestDoctestRunner(doctest.DebugRunner):
Expand Down Expand Up @@ -204,7 +204,7 @@ def report_unexpected_exception(
out,
test: "doctest.DocTest",
example: "doctest.Example",
exc_info: "Tuple[Type[BaseException], BaseException, types.TracebackType]",
exc_info: Tuple[Type[BaseException], BaseException, types.TracebackType],
) -> None:
if isinstance(exc_info[1], OutcomeException):
raise exc_info[1]
Expand Down Expand Up @@ -568,7 +568,7 @@ def func() -> None:
return fixture_request


def _init_checker_class() -> "Type[doctest.OutputChecker]":
def _init_checker_class() -> Type["doctest.OutputChecker"]:
import doctest
import re

Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/fixtures.py
Expand Up @@ -19,6 +19,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
Expand Down Expand Up @@ -57,7 +58,6 @@
if TYPE_CHECKING:
from typing import Deque
from typing import NoReturn
from typing import Type
from typing_extensions import Literal

from _pytest import nodes
Expand Down Expand Up @@ -91,7 +91,7 @@
# Cache key.
object,
# Exc info if raised.
Tuple["Type[BaseException]", BaseException, TracebackType],
Tuple[Type[BaseException], BaseException, TracebackType],
],
]

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/main.py
Expand Up @@ -14,6 +14,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union

Expand Down Expand Up @@ -44,7 +45,6 @@


if TYPE_CHECKING:
from typing import Type
from typing_extensions import Literal


Expand Down
7 changes: 2 additions & 5 deletions src/_pytest/mark/structures.py
Expand Up @@ -14,6 +14,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
Expand All @@ -31,8 +32,6 @@
from _pytest.warning_types import PytestUnknownMarkWarning

if TYPE_CHECKING:
from typing import Type

from ..nodes import Node


Expand Down Expand Up @@ -417,9 +416,7 @@ def __call__( # noqa: F811
*conditions: Union[str, bool],
reason: str = ...,
run: bool = ...,
raises: Union[
"Type[BaseException]", Tuple["Type[BaseException]", ...]
] = ...,
raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = ...,
strict: bool = ...
) -> MarkDecorator:
...
Expand Down
5 changes: 2 additions & 3 deletions src/_pytest/nodes.py
Expand Up @@ -10,6 +10,7 @@
from typing import Optional
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
Expand All @@ -36,8 +37,6 @@
from _pytest.store import Store

if TYPE_CHECKING:
from typing import Type

# Imported here due to circular import.
from _pytest.main import Session
from _pytest.warning_types import PytestWarning
Expand Down Expand Up @@ -350,7 +349,7 @@ def addfinalizer(self, fin: Callable[[], object]) -> None:
"""
self.session._setupstate.addfinalizer(fin, self)

def getparent(self, cls: "Type[_NodeType]") -> Optional[_NodeType]:
def getparent(self, cls: Type[_NodeType]) -> Optional[_NodeType]:
"""Get the next parent node (including self) which is an instance of
the given class."""
current = self # type: Optional[Node]
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/outcomes.py
Expand Up @@ -5,13 +5,13 @@
from typing import Callable
from typing import cast
from typing import Optional
from typing import Type
from typing import TypeVar

TYPE_CHECKING = False # Avoid circular import through compat.

if TYPE_CHECKING:
from typing import NoReturn
from typing import Type # noqa: F401 (used in type string)
from typing_extensions import Protocol
else:
# typing.Protocol is only available starting from Python 3.8. It is also
Expand Down Expand Up @@ -84,7 +84,7 @@ def __init__(
# Ideally would just be `exit.Exception = Exit` etc.

_F = TypeVar("_F", bound=Callable[..., object])
_ET = TypeVar("_ET", bound="Type[BaseException]")
_ET = TypeVar("_ET", bound=Type[BaseException])


class _WithException(Protocol[_F, _ET]):
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/pytester.py
Expand Up @@ -18,6 +18,7 @@
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union
from weakref import WeakKeyDictionary
Expand Down Expand Up @@ -49,7 +50,6 @@
from _pytest.tmpdir import TempdirFactory

if TYPE_CHECKING:
from typing import Type
from typing_extensions import Literal

import pexpect
Expand Down Expand Up @@ -420,7 +420,7 @@ def linecomp() -> "LineComp":


@pytest.fixture(name="LineMatcher")
def LineMatcher_fixture(request: FixtureRequest) -> "Type[LineMatcher]":
def LineMatcher_fixture(request: FixtureRequest) -> Type["LineMatcher"]:
"""A reference to the :class: `LineMatcher`.
This is instantiable with a list of lines (without their trailing newlines).
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/python.py
Expand Up @@ -22,6 +22,7 @@
from typing import Sequence
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import Union

Expand Down Expand Up @@ -72,7 +73,6 @@
from _pytest.warning_types import PytestUnhandledCoroutineWarning

if TYPE_CHECKING:
from typing import Type
from typing_extensions import Literal
from _pytest.fixtures import _Scope

Expand Down

0 comments on commit 6ed07a1

Please sign in to comment.