diff --git a/.coveragerc b/.coveragerc index ed1fb97596..fc2321e877 100644 --- a/.coveragerc +++ b/.coveragerc @@ -24,3 +24,5 @@ exclude_lines = \#\s*pragma: no cover ^\s*raise NotImplementedError\b ^\s*return NotImplemented\b + + ^\s*if TYPE_CHECKING: diff --git a/changelog/6435.trivial.rst b/changelog/6435.trivial.rst new file mode 100644 index 0000000000..78a4a3bac8 --- /dev/null +++ b/changelog/6435.trivial.rst @@ -0,0 +1 @@ +Added ``typing_extensions`` as dependency on Python 3.5.1 or lower. diff --git a/doc/en/conf.py b/doc/en/conf.py index 3fb6002bc1..bd2fd9871f 100644 --- a/doc/en/conf.py +++ b/doc/en/conf.py @@ -19,8 +19,9 @@ import sys from _pytest import __version__ as version +from _pytest.compat import TYPE_CHECKING -if False: # TYPE_CHECKING +if TYPE_CHECKING: import sphinx.application diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index d1a8ec2f1e..f0e503f4f1 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -32,8 +32,9 @@ from _pytest._io.saferepr import safeformat from _pytest._io.saferepr import saferepr from _pytest.compat import overload +from _pytest.compat import TYPE_CHECKING -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type from typing_extensions import Literal from weakref import ReferenceType # noqa: F401 diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 9eec0aedaa..117554c448 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -1,12 +1,18 @@ """ python version compatibility code """ +import sys # isort:skip + +if sys.version_info < (3, 5, 2): + TYPE_CHECKING = False # type: bool +else: + from typing import TYPE_CHECKING + import functools import inspect import io import os import re -import sys from contextlib import contextmanager from inspect import Parameter from inspect import signature @@ -28,7 +34,8 @@ from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME -if False: # TYPE_CHECKING + +if TYPE_CHECKING: from typing import Type # noqa: F401 (used in type string) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index fb965b2615..d6a704b1d8 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -37,12 +37,13 @@ from _pytest._code import ExceptionInfo from _pytest._code import filter_traceback from _pytest.compat import importlib_metadata +from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import fail from _pytest.outcomes import Skipped from _pytest.pathlib import Path from _pytest.warning_types import PytestConfigWarning -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type from .argparsing import Argument diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index d0870ed56a..fa9d8f5dc6 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -15,9 +15,10 @@ import py +from _pytest.compat import TYPE_CHECKING from _pytest.config.exceptions import UsageError -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import NoReturn from typing_extensions import Literal # noqa: F401 diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 728246dfcc..d9b5f7543f 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -5,9 +5,10 @@ import py from .exceptions import UsageError +from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import fail -if False: +if TYPE_CHECKING: from . import Config # noqa: F401 diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 3475ac9c2b..30eaba0353 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -19,12 +19,13 @@ from _pytest._code.code import ReprFileLocation from _pytest._code.code import TerminalRepr from _pytest.compat import safe_getattr +from _pytest.compat import TYPE_CHECKING from _pytest.fixtures import FixtureRequest from _pytest.outcomes import Skipped from _pytest.python_api import approx from _pytest.warning_types import PytestWarning -if False: # TYPE_CHECKING +if TYPE_CHECKING: import doctest from typing import Type diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 44802e0009..fe5410c39d 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -27,12 +27,13 @@ from _pytest.compat import is_generator from _pytest.compat import NOTSET from _pytest.compat import safe_getattr +from _pytest.compat import TYPE_CHECKING from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS from _pytest.deprecated import FUNCARGNAMES from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type from _pytest import nodes diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 3eaafa91d5..13707c89a5 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -17,6 +17,7 @@ from _pytest._code.code import ReprExceptionInfo from _pytest.compat import cached_property from _pytest.compat import getfslineno +from _pytest.compat import TYPE_CHECKING from _pytest.config import Config from _pytest.deprecated import NODE_USE_FROM_PARENT from _pytest.fixtures import FixtureDef @@ -27,7 +28,7 @@ from _pytest.mark.structures import NodeKeywords from _pytest.outcomes import Failed -if False: # TYPE_CHECKING +if TYPE_CHECKING: # Imported here due to circular import. from _pytest.main import Session # noqa: F401 diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index 9471366257..6ac97af613 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -8,7 +8,9 @@ from packaging.version import Version -if False: # TYPE_CHECKING +from _pytest.compat import TYPE_CHECKING + +if TYPE_CHECKING: from typing import NoReturn diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index d2313f2808..9c60dc1aaf 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -28,6 +28,7 @@ from _pytest._io.saferepr import saferepr from _pytest.capture import MultiCapture from _pytest.capture import SysCapture +from _pytest.compat import TYPE_CHECKING from _pytest.fixtures import FixtureRequest from _pytest.main import ExitCode from _pytest.main import Session @@ -35,7 +36,7 @@ from _pytest.pathlib import Path from _pytest.reports import TestReport -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type @@ -189,7 +190,7 @@ def __repr__(self): del d["_name"] return "".format(self._name, d) - if False: # TYPE_CHECKING + if TYPE_CHECKING: # The class has undetermined attributes, this tells mypy about it. def __getattr__(self, key): raise NotImplementedError() diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 9f206ce9b4..5e5eddc5b0 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -23,9 +23,10 @@ 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 False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type # noqa: F401 (used in type string) diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index 5cf32c8940..956a907831 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -12,10 +12,11 @@ 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 False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 50e4d4307a..417783b47e 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -15,12 +15,13 @@ from .reports import CollectReport from .reports import TestReport from _pytest._code.code import ExceptionInfo +from _pytest.compat import TYPE_CHECKING from _pytest.nodes import Node from _pytest.outcomes import Exit from _pytest.outcomes import Skipped from _pytest.outcomes import TEST_OUTCOME -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type # diff --git a/src/_pytest/warning_types.py b/src/_pytest/warning_types.py index 22cb17dbae..2e03c578c0 100644 --- a/src/_pytest/warning_types.py +++ b/src/_pytest/warning_types.py @@ -4,8 +4,9 @@ import attr +from _pytest.compat import TYPE_CHECKING -if False: # TYPE_CHECKING +if TYPE_CHECKING: from typing import Type # noqa: F401 (used in type string) diff --git a/tox.ini b/tox.ini index c26b7cf828..9415edc6a1 100644 --- a/tox.ini +++ b/tox.ini @@ -182,6 +182,8 @@ markers = [flake8] max-line-length = 120 extend-ignore = E203 +per-file-ignores = + src/_pytest/compat.py: E402 [isort] ; This config mimics what reorder-python-imports does.