Skip to content

added type references to pager.py, mypy produced no errors #936

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 5 commits into from
Nov 9, 2021
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
2 changes: 1 addition & 1 deletion bpython/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .pager import page

# Ugly monkeypatching
pydoc.pager = page
pydoc.pager = page # type: ignore


class _Helper:
Expand Down
14 changes: 9 additions & 5 deletions bpython/pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# mypy: disallow_untyped_defs=True
# mypy: disallow_untyped_calls=True

import curses
import errno
Expand All @@ -28,31 +30,33 @@
import subprocess
import sys
import shlex
from typing import List


def get_pager_command(default="less -rf"):
def get_pager_command(default: str = "less -rf") -> List[str]:
command = shlex.split(os.environ.get("PAGER", default))
return command


def page_internal(data):
def page_internal(data: str) -> None:
"""A more than dumb pager function."""
if hasattr(pydoc, "ttypager"):
pydoc.ttypager(data)
else:
sys.stdout.write(data)


def page(data, use_internal=False):
def page(data: str, use_internal: bool = False) -> None:
command = get_pager_command()
if not command or use_internal:
page_internal(data)
else:
curses.endwin()
try:
popen = subprocess.Popen(command, stdin=subprocess.PIPE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this needs a

Suggested change
popen = subprocess.Popen(command, stdin=subprocess.PIPE)
assert popen.stdin is not None
popen = subprocess.Popen(command, stdin=subprocess.PIPE)

to silence mypy errors about stdin maybe being None.

data = data.encode(sys.__stdout__.encoding, "replace")
popen.stdin.write(data)
assert popen.stdin is not None
data_bytes = data.encode(sys.__stdout__.encoding, "replace")
popen.stdin.write(data_bytes)
popen.stdin.close()
except OSError as e:
if e.errno == errno.ENOENT:
Expand Down