Skip to content

Commit 75615b1

Browse files
authored
Add @overload to contrib.regular_languages.compiler.Variables.get
- Improves type hints for the `Variables` class. - Fixes ruff pipeline check. - Fixes several mypy issues.
1 parent 6695411 commit 75615b1

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

Diff for: .github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
pip list
3232
- name: Ruff
3333
run: |
34-
ruff .
34+
ruff check .
3535
ruff format --check .
3636
typos .
3737
- name: Tests

Diff for: src/prompt_toolkit/contrib/regular_languages/compiler.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from __future__ import annotations
4343

4444
import re
45-
from typing import Callable, Dict, Iterable, Iterator, Pattern
45+
from typing import Callable, Dict, Iterable, Iterator, Pattern, TypeVar, overload
4646
from typing import Match as RegexMatch
4747

4848
from .regex_parser import (
@@ -57,9 +57,7 @@
5757
tokenize_regex,
5858
)
5959

60-
__all__ = [
61-
"compile",
62-
]
60+
__all__ = ["compile", "Match", "Variables"]
6361

6462

6563
# Name of the named group in the regex, matching trailing input.
@@ -491,6 +489,9 @@ def end_nodes(self) -> Iterable[MatchVariable]:
491489
yield MatchVariable(varname, value, (reg[0], reg[1]))
492490

493491

492+
_T = TypeVar("_T")
493+
494+
494495
class Variables:
495496
def __init__(self, tuples: list[tuple[str, str, tuple[int, int]]]) -> None:
496497
#: List of (varname, value, slice) tuples.
@@ -502,7 +503,13 @@ def __repr__(self) -> str:
502503
", ".join(f"{k}={v!r}" for k, v, _ in self._tuples),
503504
)
504505

505-
def get(self, key: str, default: str | None = None) -> str | None:
506+
@overload
507+
def get(self, key: str) -> str | None: ...
508+
509+
@overload
510+
def get(self, key: str, default: str | _T) -> str | _T: ...
511+
512+
def get(self, key: str, default: str | _T | None = None) -> str | _T | None:
506513
items = self.getall(key)
507514
return items[0] if items else default
508515

Diff for: src/prompt_toolkit/output/defaults.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from typing import TextIO, cast
4+
from typing import TYPE_CHECKING, TextIO, cast
55

66
from prompt_toolkit.utils import (
77
get_bell_environment_variable,
@@ -13,13 +13,17 @@
1313
from .color_depth import ColorDepth
1414
from .plain_text import PlainTextOutput
1515

16+
if TYPE_CHECKING:
17+
from prompt_toolkit.patch_stdout import StdoutProxy
18+
19+
1620
__all__ = [
1721
"create_output",
1822
]
1923

2024

2125
def create_output(
22-
stdout: TextIO | None = None, always_prefer_tty: bool = False
26+
stdout: TextIO | StdoutProxy | None = None, always_prefer_tty: bool = False
2327
) -> Output:
2428
"""
2529
Return an :class:`~prompt_toolkit.output.Output` instance for the command
@@ -54,13 +58,6 @@ def create_output(
5458
stdout = io
5559
break
5660

57-
# If the output is still `None`, use a DummyOutput.
58-
# This happens for instance on Windows, when running the application under
59-
# `pythonw.exe`. In that case, there won't be a terminal Window, and
60-
# stdin/stdout/stderr are `None`.
61-
if stdout is None:
62-
return DummyOutput()
63-
6461
# If the patch_stdout context manager has been used, then sys.stdout is
6562
# replaced by this proxy. For prompt_toolkit applications, we want to use
6663
# the real stdout.
@@ -69,6 +66,13 @@ def create_output(
6966
while isinstance(stdout, StdoutProxy):
7067
stdout = stdout.original_stdout
7168

69+
# If the output is still `None`, use a DummyOutput.
70+
# This happens for instance on Windows, when running the application under
71+
# `pythonw.exe`. In that case, there won't be a terminal Window, and
72+
# stdin/stdout/stderr are `None`.
73+
if stdout is None:
74+
return DummyOutput()
75+
7276
if sys.platform == "win32":
7377
from .conemu import ConEmuOutput
7478
from .win32 import Win32Output

Diff for: src/prompt_toolkit/patch_stdout.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def flush(self) -> None:
273273
self._flush()
274274

275275
@property
276-
def original_stdout(self) -> TextIO:
276+
def original_stdout(self) -> TextIO | None:
277277
return self._output.stdout or sys.__stdout__
278278

279279
# Attributes for compatibility with sys.__stdout__:

0 commit comments

Comments
 (0)