Skip to content

Commit bf451d4

Browse files
authored
Merge pull request #11213 from bluetech/py38-literal
Use typing.Literal without TYPE_CHECKING checks
2 parents 578fbe3 + 04e0db7 commit bf451d4

File tree

13 files changed

+40
-62
lines changed

13 files changed

+40
-62
lines changed

src/_pytest/_py/path.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525
from typing import Any
2626
from typing import Callable
2727
from typing import cast
28+
from typing import Literal
2829
from typing import overload
2930
from typing import TYPE_CHECKING
3031

3132
from . import error
3233

33-
if TYPE_CHECKING:
34-
from typing import Literal
35-
3634
# Moved from local.py.
3735
iswin32 = sys.platform == "win32" or (getattr(os, "_name", False) == "nt")
3836

src/_pytest/config/argparsing.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from typing import Dict
1010
from typing import final
1111
from typing import List
12+
from typing import Literal
1213
from typing import Mapping
1314
from typing import NoReturn
1415
from typing import Optional
1516
from typing import Sequence
1617
from typing import Tuple
17-
from typing import TYPE_CHECKING
1818
from typing import Union
1919

2020
import _pytest._io
@@ -24,9 +24,6 @@
2424
from _pytest.deprecated import ARGUMENT_TYPE_STR_CHOICE
2525
from _pytest.deprecated import check_ispytest
2626

27-
if TYPE_CHECKING:
28-
from typing_extensions import Literal
29-
3027
FILE_OR_DIR = "file_or_dir"
3128

3229

@@ -177,7 +174,7 @@ def addini(
177174
name: str,
178175
help: str,
179176
type: Optional[
180-
"Literal['string', 'paths', 'pathlist', 'args', 'linelist', 'bool']"
177+
Literal["string", "paths", "pathlist", "args", "linelist", "bool"]
181178
] = None,
182179
default: Any = None,
183180
) -> None:

src/_pytest/fixtures.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from _pytest.outcomes import TEST_OUTCOME
6161
from _pytest.pathlib import absolutepath
6262
from _pytest.pathlib import bestrelpath
63+
from _pytest.scope import _ScopeName
6364
from _pytest.scope import HIGH_SCOPES
6465
from _pytest.scope import Scope
6566
from _pytest.stash import StashKey
@@ -68,7 +69,6 @@
6869
if TYPE_CHECKING:
6970
from typing import Deque
7071

71-
from _pytest.scope import _ScopeName
7272
from _pytest.main import Session
7373
from _pytest.python import CallSpec2
7474
from _pytest.python import Function
@@ -444,7 +444,7 @@ def __init__(self, pyfuncitem: "Function", *, _ispytest: bool = False) -> None:
444444
self.param: Any
445445

446446
@property
447-
def scope(self) -> "_ScopeName":
447+
def scope(self) -> _ScopeName:
448448
"""Scope string, one of "function", "class", "module", "package", "session"."""
449449
return self._scope.value
450450

@@ -949,10 +949,10 @@ def _teardown_yield_fixture(fixturefunc, it) -> None:
949949

950950

951951
def _eval_scope_callable(
952-
scope_callable: "Callable[[str, Config], _ScopeName]",
952+
scope_callable: Callable[[str, Config], _ScopeName],
953953
fixture_name: str,
954954
config: Config,
955-
) -> "_ScopeName":
955+
) -> _ScopeName:
956956
try:
957957
# Type ignored because there is no typing mechanism to specify
958958
# keyword arguments, currently.
@@ -987,7 +987,7 @@ def __init__(
987987
baseid: Optional[str],
988988
argname: str,
989989
func: "_FixtureFunc[FixtureValue]",
990-
scope: Union[Scope, "_ScopeName", Callable[[str, Config], "_ScopeName"], None],
990+
scope: Union[Scope, _ScopeName, Callable[[str, Config], _ScopeName], None],
991991
params: Optional[Sequence[object]],
992992
unittest: bool = False,
993993
ids: Optional[
@@ -1048,7 +1048,7 @@ def __init__(
10481048
self._finalizers: Final[List[Callable[[], object]]] = []
10491049

10501050
@property
1051-
def scope(self) -> "_ScopeName":
1051+
def scope(self) -> _ScopeName:
10521052
"""Scope string, one of "function", "class", "module", "package", "session"."""
10531053
return self._scope.value
10541054

src/_pytest/hookspec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
if TYPE_CHECKING:
1919
import pdb
2020
import warnings
21-
from typing_extensions import Literal
21+
from typing import Literal
2222

2323
from _pytest._code.code import ExceptionRepr
2424
from _pytest._code.code import ExceptionInfo

src/_pytest/logging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import final
1717
from typing import Generator
1818
from typing import List
19+
from typing import Literal
1920
from typing import Mapping
2021
from typing import Optional
2122
from typing import Tuple
@@ -41,8 +42,6 @@
4142

4243
if TYPE_CHECKING:
4344
logging_StreamHandler = logging.StreamHandler[StringIO]
44-
45-
from typing_extensions import Literal
4645
else:
4746
logging_StreamHandler = logging.StreamHandler
4847

src/_pytest/main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
from typing import FrozenSet
1414
from typing import Iterator
1515
from typing import List
16+
from typing import Literal
1617
from typing import Optional
1718
from typing import overload
1819
from typing import Sequence
1920
from typing import Set
2021
from typing import Tuple
2122
from typing import Type
22-
from typing import TYPE_CHECKING
2323
from typing import Union
2424

2525
import _pytest._code
@@ -43,10 +43,6 @@
4343
from _pytest.runner import SetupState
4444

4545

46-
if TYPE_CHECKING:
47-
from typing_extensions import Literal
48-
49-
5046
def pytest_addoption(parser: Parser) -> None:
5147
parser.addini(
5248
"norecursedirs",

src/_pytest/python.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from typing import Iterable
2121
from typing import Iterator
2222
from typing import List
23+
from typing import Literal
2324
from typing import Mapping
2425
from typing import Optional
2526
from typing import Pattern
2627
from typing import Sequence
2728
from typing import Set
2829
from typing import Tuple
29-
from typing import TYPE_CHECKING
3030
from typing import Union
3131

3232
import _pytest
@@ -75,16 +75,12 @@
7575
from _pytest.pathlib import ImportPathMismatchError
7676
from _pytest.pathlib import parts
7777
from _pytest.pathlib import visit
78+
from _pytest.scope import _ScopeName
7879
from _pytest.scope import Scope
7980
from _pytest.warning_types import PytestCollectionWarning
8081
from _pytest.warning_types import PytestReturnNotNoneWarning
8182
from _pytest.warning_types import PytestUnhandledCoroutineWarning
8283

83-
if TYPE_CHECKING:
84-
from typing_extensions import Literal
85-
86-
from _pytest.scope import _ScopeName
87-
8884

8985
_PYTEST_DIR = Path(_pytest.__file__).parent
9086

@@ -1236,7 +1232,7 @@ def parametrize(
12361232
ids: Optional[
12371233
Union[Iterable[Optional[object]], Callable[[Any], Optional[object]]]
12381234
] = None,
1239-
scope: "Optional[_ScopeName]" = None,
1235+
scope: Optional[_ScopeName] = None,
12401236
*,
12411237
_param_mark: Optional[Mark] = None,
12421238
) -> None:

src/_pytest/reports.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Iterable
1010
from typing import Iterator
1111
from typing import List
12+
from typing import Literal
1213
from typing import Mapping
1314
from typing import NoReturn
1415
from typing import Optional
@@ -36,8 +37,6 @@
3637
from _pytest.outcomes import skip
3738

3839
if TYPE_CHECKING:
39-
from typing_extensions import Literal
40-
4140
from _pytest.runner import CallInfo
4241

4342

@@ -64,7 +63,7 @@ class BaseReport:
6463
]
6564
sections: List[Tuple[str, str]]
6665
nodeid: str
67-
outcome: "Literal['passed', 'failed', 'skipped']"
66+
outcome: Literal["passed", "failed", "skipped"]
6867

6968
def __init__(self, **kw: Any) -> None:
7069
self.__dict__.update(kw)
@@ -258,11 +257,11 @@ def __init__(
258257
nodeid: str,
259258
location: Tuple[str, Optional[int], str],
260259
keywords: Mapping[str, Any],
261-
outcome: "Literal['passed', 'failed', 'skipped']",
260+
outcome: Literal["passed", "failed", "skipped"],
262261
longrepr: Union[
263262
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
264263
],
265-
when: "Literal['setup', 'call', 'teardown']",
264+
when: Literal["setup", "call", "teardown"],
266265
sections: Iterable[Tuple[str, str]] = (),
267266
duration: float = 0,
268267
start: float = 0,

src/_pytest/runner.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import final
1010
from typing import Generic
1111
from typing import List
12+
from typing import Literal
1213
from typing import Optional
1314
from typing import Tuple
1415
from typing import Type
@@ -38,8 +39,6 @@
3839
from exceptiongroup import BaseExceptionGroup
3940

4041
if TYPE_CHECKING:
41-
from typing_extensions import Literal
42-
4342
from _pytest.main import Session
4443
from _pytest.terminal import TerminalReporter
4544

@@ -184,7 +183,7 @@ def pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) -> None:
184183

185184

186185
def _update_current_test_var(
187-
item: Item, when: Optional["Literal['setup', 'call', 'teardown']"]
186+
item: Item, when: Optional[Literal["setup", "call", "teardown"]]
188187
) -> None:
189188
"""Update :envvar:`PYTEST_CURRENT_TEST` to reflect the current item and stage.
190189
@@ -217,7 +216,7 @@ def pytest_report_teststatus(report: BaseReport) -> Optional[Tuple[str, str, str
217216

218217

219218
def call_and_report(
220-
item: Item, when: "Literal['setup', 'call', 'teardown']", log: bool = True, **kwds
219+
item: Item, when: Literal["setup", "call", "teardown"], log: bool = True, **kwds
221220
) -> TestReport:
222221
call = call_runtest_hook(item, when, **kwds)
223222
hook = item.ihook
@@ -245,7 +244,7 @@ def check_interactive_exception(call: "CallInfo[object]", report: BaseReport) ->
245244

246245

247246
def call_runtest_hook(
248-
item: Item, when: "Literal['setup', 'call', 'teardown']", **kwds
247+
item: Item, when: Literal["setup", "call", "teardown"], **kwds
249248
) -> "CallInfo[None]":
250249
if when == "setup":
251250
ihook: Callable[..., None] = item.ihook.pytest_runtest_setup
@@ -281,7 +280,7 @@ class CallInfo(Generic[TResult]):
281280
#: The call duration, in seconds.
282281
duration: float
283282
#: The context of invocation: "collect", "setup", "call" or "teardown".
284-
when: "Literal['collect', 'setup', 'call', 'teardown']"
283+
when: Literal["collect", "setup", "call", "teardown"]
285284

286285
def __init__(
287286
self,
@@ -290,7 +289,7 @@ def __init__(
290289
start: float,
291290
stop: float,
292291
duration: float,
293-
when: "Literal['collect', 'setup', 'call', 'teardown']",
292+
when: Literal["collect", "setup", "call", "teardown"],
294293
*,
295294
_ispytest: bool = False,
296295
) -> None:
@@ -319,7 +318,7 @@ def result(self) -> TResult:
319318
def from_call(
320319
cls,
321320
func: "Callable[[], TResult]",
322-
when: "Literal['collect', 'setup', 'call', 'teardown']",
321+
when: Literal["collect", "setup", "call", "teardown"],
323322
reraise: Optional[
324323
Union[Type[BaseException], Tuple[Type[BaseException], ...]]
325324
] = None,

src/_pytest/scope.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
"""
1010
from enum import Enum
1111
from functools import total_ordering
12+
from typing import Literal
1213
from typing import Optional
13-
from typing import TYPE_CHECKING
1414

15-
if TYPE_CHECKING:
16-
from typing_extensions import Literal
1715

18-
_ScopeName = Literal["session", "package", "module", "class", "function"]
16+
_ScopeName = Literal["session", "package", "module", "class", "function"]
1917

2018

2119
@total_ordering
@@ -33,11 +31,11 @@ class Scope(Enum):
3331
"""
3432

3533
# Scopes need to be listed from lower to higher.
36-
Function: "_ScopeName" = "function"
37-
Class: "_ScopeName" = "class"
38-
Module: "_ScopeName" = "module"
39-
Package: "_ScopeName" = "package"
40-
Session: "_ScopeName" = "session"
34+
Function: _ScopeName = "function"
35+
Class: _ScopeName = "class"
36+
Module: _ScopeName = "module"
37+
Package: _ScopeName = "package"
38+
Session: _ScopeName = "session"
4139

4240
def next_lower(self) -> "Scope":
4341
"""Return the next lower scope."""
@@ -60,7 +58,7 @@ def __lt__(self, other: "Scope") -> bool:
6058

6159
@classmethod
6260
def from_user(
63-
cls, scope_name: "_ScopeName", descr: str, where: Optional[str] = None
61+
cls, scope_name: _ScopeName, descr: str, where: Optional[str] = None
6462
) -> "Scope":
6563
"""
6664
Given a scope name from the user, return the equivalent Scope enum. Should be used

0 commit comments

Comments
 (0)