Skip to content

Commit f93656d

Browse files
authored
Make ScopeName public to enable downstream type checking (#14325)
Closes #14137
1 parent 80f5492 commit f93656d

File tree

9 files changed

+33
-19
lines changed

9 files changed

+33
-19
lines changed

AUTHORS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Aron Curzon
5757
Arthur Richard
5858
Ashish Kurmi
5959
Ashley Whetter
60+
Ava Birtwistle
6061
Aviral Verma
6162
Aviv Palivoda
6263
Babak Keyvani
@@ -239,6 +240,7 @@ Joseph Hunkeler
239240
Joseph Sawaya
240241
Josh Karpel
241242
Joshua Bronson
243+
Julia Knaak
242244
Julian Valentin
243245
Junhao Liao
244246
Jurko Gospodnetić
@@ -300,6 +302,7 @@ Martijn Faassen
300302
Martin Altmayer
301303
Martin K. Scherer
302304
Martin Prusse
305+
Mateya Berezowsky
303306
Mathieu Clabaut
304307
Matt Bachmann
305308
Matt Duck

changelog/14137.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`pytest.ScopeName` is now public to allow using it in function signatures.

doc/en/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
("py:class", "_pytest.runner.TResult"),
111111
("py:obj", "_pytest.fixtures.FixtureValue"),
112112
("py:obj", "_pytest.stash.T"),
113-
("py:class", "_ScopeName"),
113+
("py:class", "ScopeName"),
114114
("py:class", "BaseExcT_1"),
115115
("py:class", "ExcT_1"),
116116
]

src/_pytest/fixtures.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
from _pytest.outcomes import TEST_OUTCOME
6666
from _pytest.pathlib import absolutepath
6767
from _pytest.pathlib import bestrelpath
68-
from _pytest.scope import _ScopeName
6968
from _pytest.scope import HIGH_SCOPES
7069
from _pytest.scope import Scope
70+
from _pytest.scope import ScopeName
7171
from _pytest.warning_types import PytestWarning
7272

7373

@@ -401,7 +401,7 @@ def _scope(self) -> Scope:
401401
raise NotImplementedError()
402402

403403
@property
404-
def scope(self) -> _ScopeName:
404+
def scope(self) -> ScopeName:
405405
"""Scope string, one of "function", "class", "module", "package", "session"."""
406406
return self._scope.value
407407

@@ -942,10 +942,10 @@ def _teardown_yield_fixture(fixturefunc, it) -> None:
942942

943943

944944
def _eval_scope_callable(
945-
scope_callable: Callable[[str, Config], _ScopeName],
945+
scope_callable: Callable[[str, Config], ScopeName],
946946
fixture_name: str,
947947
config: Config,
948-
) -> _ScopeName:
948+
) -> ScopeName:
949949
try:
950950
# Type ignored because there is no typing mechanism to specify
951951
# keyword arguments, currently.
@@ -977,7 +977,7 @@ def __init__(
977977
baseid: str | None,
978978
argname: str,
979979
func: _FixtureFunc[FixtureValue],
980-
scope: Scope | _ScopeName | Callable[[str, Config], _ScopeName] | None,
980+
scope: Scope | ScopeName | Callable[[str, Config], ScopeName] | None,
981981
params: Sequence[object] | None,
982982
ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None,
983983
*,
@@ -1034,7 +1034,7 @@ def __init__(
10341034
self._autouse = _autouse
10351035

10361036
@property
1037-
def scope(self) -> _ScopeName:
1037+
def scope(self) -> ScopeName:
10381038
"""Scope string, one of "function", "class", "module", "package", "session"."""
10391039
return self._scope.value
10401040

@@ -1228,7 +1228,7 @@ def pytest_fixture_setup(
12281228
@final
12291229
@dataclasses.dataclass(frozen=True)
12301230
class FixtureFunctionMarker:
1231-
scope: _ScopeName | Callable[[str, Config], _ScopeName]
1231+
scope: ScopeName | Callable[[str, Config], ScopeName]
12321232
params: tuple[object, ...] | None
12331233
autouse: bool = False
12341234
ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None
@@ -1322,7 +1322,7 @@ def _get_wrapped_function(self) -> Callable[..., Any]:
13221322
def fixture(
13231323
fixture_function: Callable[..., object],
13241324
*,
1325-
scope: _ScopeName | Callable[[str, Config], _ScopeName] = ...,
1325+
scope: ScopeName | Callable[[str, Config], ScopeName] = ...,
13261326
params: Iterable[object] | None = ...,
13271327
autouse: bool = ...,
13281328
ids: Sequence[object | None] | Callable[[Any], object | None] | None = ...,
@@ -1334,7 +1334,7 @@ def fixture(
13341334
def fixture(
13351335
fixture_function: None = ...,
13361336
*,
1337-
scope: _ScopeName | Callable[[str, Config], _ScopeName] = ...,
1337+
scope: ScopeName | Callable[[str, Config], ScopeName] = ...,
13381338
params: Iterable[object] | None = ...,
13391339
autouse: bool = ...,
13401340
ids: Sequence[object | None] | Callable[[Any], object | None] | None = ...,
@@ -1345,7 +1345,7 @@ def fixture(
13451345
def fixture(
13461346
fixture_function: FixtureFunction | None = None,
13471347
*,
1348-
scope: _ScopeName | Callable[[str, Config], _ScopeName] = "function",
1348+
scope: ScopeName | Callable[[str, Config], ScopeName] = "function",
13491349
params: Iterable[object] | None = None,
13501350
autouse: bool = False,
13511351
ids: Sequence[object | None] | Callable[[Any], object | None] | None = None,
@@ -1799,7 +1799,7 @@ def _register_fixture(
17991799
name: str,
18001800
func: _FixtureFunc[object],
18011801
nodeid: str | None,
1802-
scope: Scope | _ScopeName | Callable[[str, Config], _ScopeName] = "function",
1802+
scope: Scope | ScopeName | Callable[[str, Config], ScopeName] = "function",
18031803
params: Sequence[object] | None = None,
18041804
ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None,
18051805
autouse: bool = False,

src/_pytest/mark/structures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from _pytest.deprecated import PARAMETRIZE_NON_COLLECTION_ITERABLE
3030
from _pytest.outcomes import fail
3131
from _pytest.raises import AbstractRaises
32-
from _pytest.scope import _ScopeName
32+
from _pytest.scope import ScopeName
3333
from _pytest.warning_types import PytestUnknownMarkWarning
3434

3535

@@ -535,7 +535,7 @@ def __call__(
535535
ids: Iterable[None | str | float | int | bool]
536536
| Callable[[Any], object | None]
537537
| None = ...,
538-
scope: _ScopeName | None = ...,
538+
scope: ScopeName | None = ...,
539539
) -> MarkDecorator: ...
540540

541541
@overload
@@ -552,7 +552,7 @@ def __call__(
552552
ids: Iterable[None | str | float | int | bool]
553553
| Callable[[Any], object | None]
554554
| None = ...,
555-
scope: _ScopeName | None = ...,
555+
scope: ScopeName | None = ...,
556556
) -> MarkDecorator: ...
557557

558558
class _UsefixturesMarkDecorator(MarkDecorator):

src/_pytest/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
from _pytest.pathlib import import_path
7272
from _pytest.pathlib import ImportPathMismatchError
7373
from _pytest.pathlib import scandir
74-
from _pytest.scope import _ScopeName
7574
from _pytest.scope import Scope
75+
from _pytest.scope import ScopeName
7676
from _pytest.stash import StashKey
7777
from _pytest.warning_types import PytestCollectionWarning
7878
from _pytest.warning_types import PytestReturnNotNoneWarning
@@ -1209,7 +1209,7 @@ def parametrize(
12091209
argvalues: Iterable[ParameterSet | Sequence[object] | object],
12101210
indirect: bool | Sequence[str] = False,
12111211
ids: Iterable[object | None] | Callable[[Any], object | None] | None = None,
1212-
scope: _ScopeName | None = None,
1212+
scope: ScopeName | None = None,
12131213
*,
12141214
_param_mark: Mark | None = None,
12151215
) -> None:

src/_pytest/scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Literal
1616

1717

18-
_ScopeName = Literal["session", "package", "module", "class", "function"]
18+
ScopeName = Literal["session", "package", "module", "class", "function"]
1919

2020

2121
@total_ordering
@@ -60,7 +60,7 @@ def __lt__(self, other: Scope) -> bool:
6060

6161
@classmethod
6262
def from_user(
63-
cls, scope_name: _ScopeName, descr: str, where: str | None = None
63+
cls, scope_name: ScopeName, descr: str, where: str | None = None
6464
) -> Scope:
6565
"""
6666
Given a scope name from the user, return the equivalent Scope enum. Should be used

src/pytest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from _pytest.reports import CollectReport
7070
from _pytest.reports import TestReport
7171
from _pytest.runner import CallInfo
72+
from _pytest.scope import ScopeName
7273
from _pytest.stash import Stash
7374
from _pytest.stash import StashKey
7475
from _pytest.subtests import SubtestReport
@@ -145,6 +146,7 @@
145146
"RaisesGroup",
146147
"RecordedHookCall",
147148
"RunResult",
149+
"ScopeName",
148150
"Session",
149151
"Stash",
150152
"StashKey",

testing/typing_checks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import pytest
1616
from pytest import MonkeyPatch
17+
from pytest import ScopeName
1718
from pytest import TestReport
1819

1920

@@ -65,3 +66,10 @@ def check_testreport_attributes(report: TestReport) -> None:
6566
@pytest.mark.parametrize("x", iter(range(10))) # type: ignore[deprecated]
6667
def test_it(x: int) -> None:
6768
pass
69+
70+
71+
# Issue #14137.
72+
def check_scope_typing() -> None:
73+
74+
custom_scope: ScopeName = "function"
75+
assert_type(custom_scope, ScopeName)

0 commit comments

Comments
 (0)