Use `TYPE_CHECKING` instead of `False`

This allows for e.g. Jedi to infer types (it checks the name).

It was only used to support Python 3.5.0/3.5.1, where this is is not
available in the `typing` module.

Ref: https://github.com/davidhalter/jedi/issues/1472

Uses `TYPE_CHECKING = False` in `_pytest.outcomes` to avoid having to
work around circular import.
This commit is contained in:
Daniel Hahler 2020-01-10 19:53:57 +01:00
parent 715f56dfbc
commit 4630e2725e
15 changed files with 37 additions and 15 deletions

View File

@ -24,3 +24,5 @@ exclude_lines =
\#\s*pragma: no cover \#\s*pragma: no cover
^\s*raise NotImplementedError\b ^\s*raise NotImplementedError\b
^\s*return NotImplemented\b ^\s*return NotImplemented\b
^\s*if TYPE_CHECKING:

View File

@ -19,8 +19,9 @@ import os
import sys import sys
from _pytest import __version__ as version from _pytest import __version__ as version
from _pytest.compat import TYPE_CHECKING
if False: # TYPE_CHECKING if TYPE_CHECKING:
import sphinx.application import sphinx.application

View File

@ -32,8 +32,9 @@ import _pytest
from _pytest._io.saferepr import safeformat from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr from _pytest._io.saferepr import saferepr
from _pytest.compat import overload from _pytest.compat import overload
from _pytest.compat import TYPE_CHECKING
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type
from typing_extensions import Literal from typing_extensions import Literal
from weakref import ReferenceType # noqa: F401 from weakref import ReferenceType # noqa: F401

View File

@ -27,7 +27,13 @@ from _pytest._io.saferepr import saferepr
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
if False: # TYPE_CHECKING if sys.version_info < (3, 5, 2):
TYPE_CHECKING = False # type: bool
else:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Type # noqa: F401 (used in type string) from typing import Type # noqa: F401 (used in type string)

View File

@ -37,12 +37,13 @@ from .findpaths import exists
from _pytest._code import ExceptionInfo from _pytest._code import ExceptionInfo
from _pytest._code import filter_traceback from _pytest._code import filter_traceback
from _pytest.compat import importlib_metadata from _pytest.compat import importlib_metadata
from _pytest.compat import TYPE_CHECKING
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
from _pytest.pathlib import Path from _pytest.pathlib import Path
from _pytest.warning_types import PytestConfigWarning from _pytest.warning_types import PytestConfigWarning
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type

View File

@ -5,9 +5,10 @@ from typing import Optional
import py import py
from .exceptions import UsageError from .exceptions import UsageError
from _pytest.compat import TYPE_CHECKING
from _pytest.outcomes import fail from _pytest.outcomes import fail
if False: if TYPE_CHECKING:
from . import Config # noqa: F401 from . import Config # noqa: F401

View File

@ -19,12 +19,13 @@ from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr from _pytest._code.code import TerminalRepr
from _pytest.compat import safe_getattr from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
from _pytest.python_api import approx from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning from _pytest.warning_types import PytestWarning
if False: # TYPE_CHECKING if TYPE_CHECKING:
import doctest import doctest
from typing import Type from typing import Type

View File

@ -27,12 +27,13 @@ from _pytest.compat import getlocation
from _pytest.compat import is_generator from _pytest.compat import is_generator
from _pytest.compat import NOTSET from _pytest.compat import NOTSET
from _pytest.compat import safe_getattr from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS
from _pytest.deprecated import FUNCARGNAMES from _pytest.deprecated import FUNCARGNAMES
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type
from _pytest import nodes from _pytest import nodes

View File

@ -17,6 +17,7 @@ from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprExceptionInfo from _pytest._code.code import ReprExceptionInfo
from _pytest.compat import cached_property from _pytest.compat import cached_property
from _pytest.compat import getfslineno from _pytest.compat import getfslineno
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config from _pytest.config import Config
from _pytest.fixtures import FixtureDef from _pytest.fixtures import FixtureDef
from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureLookupError
@ -26,7 +27,7 @@ from _pytest.mark.structures import MarkDecorator
from _pytest.mark.structures import NodeKeywords from _pytest.mark.structures import NodeKeywords
from _pytest.outcomes import Failed from _pytest.outcomes import Failed
if False: # TYPE_CHECKING if TYPE_CHECKING:
# Imported here due to circular import. # Imported here due to circular import.
from _pytest.main import Session # noqa: F401 from _pytest.main import Session # noqa: F401

View File

@ -8,7 +8,9 @@ from typing import Optional
from packaging.version import Version from packaging.version import Version
if False: # TYPE_CHECKING TYPE_CHECKING = False # avoid circular import through compat
if TYPE_CHECKING:
from typing import NoReturn from typing import NoReturn

View File

@ -28,6 +28,7 @@ from _pytest._code import Source
from _pytest._io.saferepr import saferepr from _pytest._io.saferepr import saferepr
from _pytest.capture import MultiCapture from _pytest.capture import MultiCapture
from _pytest.capture import SysCapture from _pytest.capture import SysCapture
from _pytest.compat import TYPE_CHECKING
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
from _pytest.main import ExitCode from _pytest.main import ExitCode
from _pytest.main import Session from _pytest.main import Session
@ -35,7 +36,7 @@ from _pytest.monkeypatch import MonkeyPatch
from _pytest.pathlib import Path from _pytest.pathlib import Path
from _pytest.reports import TestReport from _pytest.reports import TestReport
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type
@ -189,7 +190,7 @@ class ParsedCall:
del d["_name"] del d["_name"]
return "<ParsedCall {!r}(**{!r})>".format(self._name, d) return "<ParsedCall {!r}(**{!r})>".format(self._name, d)
if False: # TYPE_CHECKING if TYPE_CHECKING:
# The class has undetermined attributes, this tells mypy about it. # The class has undetermined attributes, this tells mypy about it.
def __getattr__(self, key): def __getattr__(self, key):
raise NotImplementedError() raise NotImplementedError()

View File

@ -23,9 +23,10 @@ from more_itertools.more import always_iterable
import _pytest._code import _pytest._code
from _pytest.compat import overload from _pytest.compat import overload
from _pytest.compat import STRING_TYPES from _pytest.compat import STRING_TYPES
from _pytest.compat import TYPE_CHECKING
from _pytest.outcomes import fail from _pytest.outcomes import fail
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type # noqa: F401 (used in type string) from typing import Type # noqa: F401 (used in type string)

View File

@ -12,10 +12,11 @@ from typing import Tuple
from typing import Union from typing import Union
from _pytest.compat import overload from _pytest.compat import overload
from _pytest.compat import TYPE_CHECKING
from _pytest.fixtures import yield_fixture from _pytest.fixtures import yield_fixture
from _pytest.outcomes import fail from _pytest.outcomes import fail
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type

View File

@ -15,12 +15,13 @@ from .reports import CollectErrorRepr
from .reports import CollectReport from .reports import CollectReport
from .reports import TestReport from .reports import TestReport
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
from _pytest.compat import TYPE_CHECKING
from _pytest.nodes import Node from _pytest.nodes import Node
from _pytest.outcomes import Exit from _pytest.outcomes import Exit
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
if False: # TYPE_CHECKING if TYPE_CHECKING:
from typing import Type from typing import Type
# #

View File

@ -4,8 +4,9 @@ from typing import TypeVar
import attr 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) from typing import Type # noqa: F401 (used in type string)