Skip to content

Typecheck examples. #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Type Checking
run: |
uvx --with . mypy src/ptpython
uvx --with . mypy src/ptpython/
uvx --with . mypy examples/
- name: Code formatting
if: ${{ matrix.python-version == '3.13' }}
run: |
Expand Down
6 changes: 4 additions & 2 deletions examples/ssh-and-telnet-embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
https://gist.github.com/vxgmichel/7685685b3e5ead04ada4a3ba75a48eef
"""

from __future__ import annotations

import asyncio
import pathlib

Expand All @@ -15,7 +17,7 @@
PromptToolkitSSHServer,
PromptToolkitSSHSession,
)
from prompt_toolkit.contrib.telnet.server import TelnetServer
from prompt_toolkit.contrib.telnet.server import TelnetConnection, TelnetServer

from ptpython.repl import embed

Expand All @@ -28,7 +30,7 @@ def ensure_key(filename: str = "ssh_host_key") -> str:
return str(path)


async def interact(connection: PromptToolkitSSHSession) -> None:
async def interact(connection: PromptToolkitSSHSession | TelnetConnection) -> None:
global_dict = {**globals(), "print": print_formatted_text}
await embed(return_asyncio_coroutine=True, globals=global_dict)

Expand Down
43 changes: 41 additions & 2 deletions src/ptpython/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@
import warnings
from dis import COMPILER_FLAG_NAMES
from pathlib import Path
from typing import Any, Callable, ContextManager, Iterable, NoReturn, Sequence
from typing import (
Any,
Callable,
ContextManager,
Coroutine,
Iterable,
Literal,
NoReturn,
Sequence,
overload,
)

from prompt_toolkit.formatted_text import OneStyleAndTextTuple
from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context
Expand Down Expand Up @@ -505,6 +515,34 @@ class ReplExit(Exception):
"""


@overload
def embed(
globals: dict[str, Any] | None = ...,
locals: dict[str, Any] | None = ...,
configure: Callable[[PythonRepl], None] | None = ...,
vi_mode: bool = ...,
history_filename: str | None = ...,
title: str | None = ...,
startup_paths: Sequence[str | Path] | None = ...,
patch_stdout: bool = ...,
return_asyncio_coroutine: Literal[False] = ...,
) -> None: ...


@overload
def embed(
globals: dict[str, Any] | None = ...,
locals: dict[str, Any] | None = ...,
configure: Callable[[PythonRepl], None] | None = ...,
vi_mode: bool = ...,
history_filename: str | None = ...,
title: str | None = ...,
startup_paths: Sequence[str | Path] | None = ...,
patch_stdout: bool = ...,
return_asyncio_coroutine: Literal[True] = ...,
) -> Coroutine[Any, Any, None]: ...


def embed(
globals: dict[str, Any] | None = None,
locals: dict[str, Any] | None = None,
Expand All @@ -515,7 +553,7 @@ def embed(
startup_paths: Sequence[str | Path] | None = None,
patch_stdout: bool = False,
return_asyncio_coroutine: bool = False,
) -> None:
) -> None | Coroutine[Any, Any, None]:
"""
Call this to embed Python shell at the current point in your program.
It's similar to `IPython.embed` and `bpython.embed`. ::
Expand Down Expand Up @@ -577,3 +615,4 @@ async def coroutine() -> None:
else:
with patch_context:
repl.run()
return None
Loading