Skip to content

Commit 80c2234

Browse files
authored
Type annotation polishing for symbols around Pytester.run (#8298)
* Type annotation polishing for symbols around Pytester.run Hopefully these will help document readers understand pertinent methods and constants better. Following up #8294 * Use NOTSET instead of object
1 parent bebb695 commit 80c2234

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/_pytest/pytester.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Callable
2121
from typing import Dict
2222
from typing import Generator
23+
from typing import IO
2324
from typing import Iterable
2425
from typing import List
2526
from typing import Optional
@@ -41,6 +42,8 @@
4142
from _pytest._code import Source
4243
from _pytest.capture import _get_multicapture
4344
from _pytest.compat import final
45+
from _pytest.compat import NOTSET
46+
from _pytest.compat import NotSetType
4447
from _pytest.config import _PluggyPlugin
4548
from _pytest.config import Config
4649
from _pytest.config import ExitCode
@@ -66,6 +69,7 @@
6669

6770

6871
if TYPE_CHECKING:
72+
from typing_extensions import Final
6973
from typing_extensions import Literal
7074

7175
import pexpect
@@ -651,7 +655,7 @@ class Pytester:
651655

652656
__test__ = False
653657

654-
CLOSE_STDIN = object
658+
CLOSE_STDIN: "Final" = NOTSET
655659

656660
class TimeoutExpired(Exception):
657661
pass
@@ -1297,13 +1301,13 @@ def popen(
12971301
cmdargs: Sequence[Union[str, "os.PathLike[str]"]],
12981302
stdout: Union[int, TextIO] = subprocess.PIPE,
12991303
stderr: Union[int, TextIO] = subprocess.PIPE,
1300-
stdin=CLOSE_STDIN,
1304+
stdin: Union[NotSetType, bytes, IO[Any], int] = CLOSE_STDIN,
13011305
**kw,
13021306
):
13031307
"""Invoke :py:class:`subprocess.Popen`.
13041308
13051309
Calls :py:class:`subprocess.Popen` making sure the current working
1306-
directory is in the ``PYTHONPATH``.
1310+
directory is in ``PYTHONPATH``.
13071311
13081312
You probably want to use :py:meth:`run` instead.
13091313
"""
@@ -1334,7 +1338,7 @@ def run(
13341338
self,
13351339
*cmdargs: Union[str, "os.PathLike[str]"],
13361340
timeout: Optional[float] = None,
1337-
stdin=CLOSE_STDIN,
1341+
stdin: Union[NotSetType, bytes, IO[Any], int] = CLOSE_STDIN,
13381342
) -> RunResult:
13391343
"""Run a command with arguments.
13401344
@@ -1426,21 +1430,17 @@ def _dump_lines(self, lines, fp):
14261430
def _getpytestargs(self) -> Tuple[str, ...]:
14271431
return sys.executable, "-mpytest"
14281432

1429-
def runpython(self, script) -> RunResult:
1430-
"""Run a python script using sys.executable as interpreter.
1431-
1432-
:rtype: RunResult
1433-
"""
1433+
def runpython(self, script: "os.PathLike[str]") -> RunResult:
1434+
"""Run a python script using sys.executable as interpreter."""
14341435
return self.run(sys.executable, script)
14351436

1436-
def runpython_c(self, command):
1437-
"""Run python -c "command".
1438-
1439-
:rtype: RunResult
1440-
"""
1437+
def runpython_c(self, command: str) -> RunResult:
1438+
"""Run ``python -c "command"``."""
14411439
return self.run(sys.executable, "-c", command)
14421440

1443-
def runpytest_subprocess(self, *args, timeout: Optional[float] = None) -> RunResult:
1441+
def runpytest_subprocess(
1442+
self, *args: Union[str, "os.PathLike[str]"], timeout: Optional[float] = None
1443+
) -> RunResult:
14441444
"""Run pytest as a subprocess with given arguments.
14451445
14461446
Any plugins added to the :py:attr:`plugins` list will be added using the
@@ -1454,8 +1454,6 @@ def runpytest_subprocess(self, *args, timeout: Optional[float] = None) -> RunRes
14541454
:param timeout:
14551455
The period in seconds after which to timeout and raise
14561456
:py:class:`Pytester.TimeoutExpired`.
1457-
1458-
:rtype: RunResult
14591457
"""
14601458
__tracebackhide__ = True
14611459
p = make_numbered_dir(root=self.path, prefix="runpytest-")
@@ -1529,9 +1527,9 @@ class Testdir:
15291527

15301528
__test__ = False
15311529

1532-
CLOSE_STDIN = Pytester.CLOSE_STDIN
1533-
TimeoutExpired = Pytester.TimeoutExpired
1534-
Session = Pytester.Session
1530+
CLOSE_STDIN: "Final" = Pytester.CLOSE_STDIN
1531+
TimeoutExpired: "Final" = Pytester.TimeoutExpired
1532+
Session: "Final" = Pytester.Session
15351533

15361534
def __init__(self, pytester: Pytester, *, _ispytest: bool = False) -> None:
15371535
check_ispytest(_ispytest)
@@ -1695,8 +1693,8 @@ def collect_by_name(
16951693
def popen(
16961694
self,
16971695
cmdargs,
1698-
stdout: Union[int, TextIO] = subprocess.PIPE,
1699-
stderr: Union[int, TextIO] = subprocess.PIPE,
1696+
stdout=subprocess.PIPE,
1697+
stderr=subprocess.PIPE,
17001698
stdin=CLOSE_STDIN,
17011699
**kw,
17021700
):

0 commit comments

Comments
 (0)