Skip to content
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

type annotate tests #592

Merged
merged 2 commits into from
Jun 27, 2020
Merged

type annotate tests #592

merged 2 commits into from
Jun 27, 2020

Conversation

rkm
Copy link
Contributor

@rkm rkm commented Jun 26, 2020

hi!

This PR contains my work towards closing #15 . All the main tests now pass mypy, except for the plugin tests which I will continue to work on. As discussed in #15, there are some # type: ignores that need to be worked through at some point once pytest supports typing better.

@codecov
Copy link

codecov bot commented Jun 26, 2020

Codecov Report

Merging #592 into master will decrease coverage by 0.03%.
The diff coverage is 68.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #592      +/-   ##
==========================================
- Coverage   79.08%   79.05%   -0.04%     
==========================================
  Files          13       13              
  Lines        2056     2053       -3     
  Branches      293      293              
==========================================
- Hits         1626     1623       -3     
  Misses        322      322              
  Partials      108      108              
Impacted Files Coverage Δ
src/bandersnatch/mirror.py 85.89% <66.66%> (-1.91%) ⬇️
src/bandersnatch/package.py 81.09% <100.00%> (+1.39%) ⬆️
src/bandersnatch/utils.py 93.97% <100.00%> (ø)

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 183af68...25535f2. Read the comment docs.

@rkm rkm marked this pull request as ready for review June 26, 2020 09:44
Copy link
Contributor

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

LGTM - surprising the small coverage drop but this is a huge step forward into getting complete type coverage. Many thanks!

I look forward to a more type annotated pytest!

config_path.write_text("\n".join(config_lines), encoding="utf-8")
sys.argv = ["bandersnatch", "-c", str(config_path), "mirror"]
assert config_path.exists()
main(asyncio.new_event_loop())
(homedir, master), kwargs = mirror_mock.call_args_list[0]

assert "/srv/pypi" == homedir
assert Path("/srv/pypi") == homedir
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this change on Mirror - just hope non fikeaystem objects can handle it - cc: @techalchemy

monkeypatch.chdir(tmpdir)
with open("sample", "w") as f:
f.write("bsdf")
with rewrite("sample") as f:
with rewrite("sample") as f: # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the type error here ... weird. I’ll look after merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error is:

src/bandersnatch/tests/test_utils.py: note: In function "test_rewrite":
src/bandersnatch/tests/test_utils.py:74: error: Incompatible types in
assignment (expression has type "IO[Any]", variable has type "TextIO")
        with rewrite("sample") as f:

Copy link
Member

@ichard26 ichard26 Jun 26, 2020

Choose a reason for hiding this comment

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

The problem is that the open() call above has the return type typing.TextIO because mypy sees "w" and returns a more specific type. The rewrite function can take any mode and then pass it along to tempfile.NamedTemporaryFile. Since the mode is defined by a variable, mypy just sets the return type of tempfile.NamedTemporaryFile and subsequently rewrite to typing.IO[Any]

A solution that comes to mind is just adding a f: IO[Any] or f: IO type annotation before the first with statement so f isn't inferred as a subclass (typing.TextIO) of typing.IO.


¹. Quoting from Guido van Rossum python/typeshed#1229 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't trust that guy, but that's what I would have tried too. Thanks @ichard26

@cooperlees cooperlees merged commit 86fc2a8 into pypa:master Jun 27, 2020
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.

3 participants