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

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

merged 5 commits into from
Nov 9, 2021

Conversation

Frostmaine
Copy link
Contributor

First time adding to a project on github, started off small. Would really appreciate feedback.

Copy link
Member

@thomasballinger thomasballinger left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

It looks like some builds are failing because list can't be subscripted, I added a note about why.

When I run mypy, I get a few errors

$ python -m mypy
bpython/pager.py:57: error: Item "None" of "Optional[IO[bytes]]" has no attribute "write"
bpython/pager.py:58: error: Item "None" of "Optional[IO[bytes]]" has no attribute "close"
bpython/_internal.py:7: error: Incompatible types in assignment (expression has type "Callable[[str, bool], None]", variable has type "Callable[[str], None]")
Found 3 errors in 2 files (checked 64 source files)

If you figure out how our environments are different let me know and maybe we can add it to some developer docs.

The bpython/_internal.py file is getting more strictly checked now because it uses page defined in pager.py. Apparently the real pydoc.pager doesn't take that second argument. I'd be fine with a type: ignore for that.

pydoc.pager = page  # type: ignore

bpython/pager.py Outdated
@@ -30,29 +32,29 @@
import shlex


def get_pager_command(default="less -rf"):
def get_pager_command(default: str = "less -rf") -> list[str]:
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately using list and dict in type annotations instead of typing.List and typing.Dict was added in 3.9 and bpython still supports 3.8 and 3.7, so we can't use these yet; you'll need to import List from typing and use that.

"""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.

@thomasballinger
Copy link
Member

thomasballinger commented Nov 2, 2021

If you aren't able to get mypy to produce these errors locally, you can see them in the CI runs on this page.
image

@Frostmaine
Copy link
Contributor Author

Linter workflow did fail, however it seemed to only be because I modified _internal.py by adding the suggested # type: ignore to pydoc.pager = page.

@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2021

Codecov Report

Merging #936 (72a126b) into main (b4578ba) will increase coverage by 0.53%.
The diff coverage is 62.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #936      +/-   ##
==========================================
+ Coverage   68.11%   68.65%   +0.53%     
==========================================
  Files          61       62       +1     
  Lines        9244     9434     +190     
==========================================
+ Hits         6297     6477     +180     
- Misses       2947     2957      +10     
Impacted Files Coverage Δ
bpython/pager.py 26.82% <57.14%> (+1.18%) ⬆️
bpython/_internal.py 78.57% <100.00%> (ø)
bpython/line.py 95.86% <0.00%> (-0.33%) ⬇️
bpython/test/test_brackets_completion.py 99.13% <0.00%> (ø)
bpython/config.py 87.83% <0.00%> (+0.08%) ⬆️
bpython/curtsiesfrontend/manual_readline.py 90.15% <0.00%> (+0.20%) ⬆️
bpython/curtsiesfrontend/repl.py 65.83% <0.00%> (+0.72%) ⬆️
bpython/autocomplete.py 86.47% <0.00%> (+0.88%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b4578ba...72a126b. Read the comment docs.

@sebastinas
Copy link
Contributor

Indeed, the black check failed. So please run black on bpython/_internal.py and commit the fix.

@thomasballinger thomasballinger merged commit adf19b1 into bpython:main Nov 9, 2021
@thomasballinger
Copy link
Member

Thanks for adding these types and fixing these CI issues, this looks good to me. The pypy3 CI failure is failing on main right now too.

@Frostmaine
Copy link
Contributor Author

Frostmaine commented Nov 10, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants