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

PEP 484 type annotations for Twine #231

Closed
ygingras opened this issue Feb 10, 2017 · 33 comments · Fixed by #631
Closed

PEP 484 type annotations for Twine #231

ygingras opened this issue Feb 10, 2017 · 33 comments · Fixed by #631
Assignees
Labels
enhancement testing Test frameworks, tests, etc.

Comments

@ygingras
Copy link

ygingras commented Feb 10, 2017

Update 4/25/2020: See #231 (comment) below for the current roadmap to completion.

I am considering adding type annotations to Twine to help my team do static analysis of code that depends on it. Would a pull request containing type annotations be merge back?

More info on type annotations:
PEP 484: https://www.python.org/dev/peps/pep-0484/
mypy, the type-checker of choice these days: http://mypy-lang.org/

Given the need to support Python 2.x, the preferred format for annotations would be as Type Comments, but I would be open to do .pyi stubs if that makes it easier for everyone.

Please provide guidance and requirements and I will take it from there.

@sigmavirus24
Copy link
Member

@ygingras Where to .pyi stubs tend to live? Would it be acceptable to store them in a separate directory from the source, e.g., _stubs?

@ygingras
Copy link
Author

Ian, the stubs can be anywhere and a _stubs dir would be perfect for them, indeed.

@sigmavirus24
Copy link
Member

Let's do that then. =) I've been interested in them for a while, and this wouldn't be a bad project to experiment with them.

@sigmavirus24
Copy link
Member

Hey @ygingras how has this been going?

@brainwane
Copy link
Contributor

Tim Abbott of Zulip has written up a guide to adding static type annotations to a pre-existing codebase that folks might find helpful if they want to start working on adding these annotations to twine:

how mypy works, the benefits and pain points we’ve seen in using mypy, and share a detailed guide for adopting mypy in a large production codebase (including how to find and fix dozens of issues in a large project in the first few days of using mypy!).

@ygingras
Copy link
Author

Hi everyone. It was probably obvious from my radio silence, but I have move on to different projects and I won't be doing this work myself. I still think the annotations would be useful, but since this was mostly an idea to scratch one of my itches, I think we can safely close this issue unless someone else is particularly excited about this work.

@brainwane brainwane added enhancement testing Test frameworks, tests, etc. labels Feb 28, 2018
@brainwane
Copy link
Contributor

@waseem18 would you be interested in working on this with me? It's a bit of a big project but it takes place in several steps and over several PRs I think we could make progress.

@waseem18
Copy link
Contributor

I would absolutely love to work on this @brainwane
I'll start going through the Zulip content, Project documentation and PEP 484.

@fhocutt
Copy link
Contributor

fhocutt commented May 14, 2018

A handful of us at PyCon are interested in working on this. @sigmavirus24 @waseem18 did you have strong preferences for using stub files instead of python2-compatible annotations in the code itself? Personally I prefer not having to switch between files to get the type information but can go with the stubs.

@sigmavirus24
Copy link
Member

@fhocutt I would prefer that they be in separate stub files. I would also prefer them to live along side the files they're annotating. I think @waseem18 has started working on that

@waseem18
Copy link
Contributor

waseem18 commented May 14, 2018

@fhocutt Good to hear about folks being interested in this. I have started working on this and have put up an initial pull request #334.

Looking forward to working with you in type hinting Twine. :)

@fhocutt
Copy link
Contributor

fhocutt commented May 16, 2018

Awesome! I see the PR. I'm recovering from PyCon right now and probably won't have time to come back to this till next week sometime. I started annotating utils.py to start getting a feel for mypy. I think that at least to start, we should turn the strictness down from what you have there and ratchet that up once it's starting to come together.

So far I have figured out (among other things) that running the --py2 checks is going to be a little hairy, since it doesn't play nicely with unicode_literals.

@crwilcox
Copy link

crwilcox commented May 7, 2019

The existing PR is #344

@bhrutledge
Copy link
Contributor

The existing PR is #344

I'm building on this PR in #466.

@carljm
Copy link

carljm commented Jun 11, 2019

It's worth noting that annotations in stub files are purely for the benefit of external users of twine as a library (are there many of those?), and bring zero type safety benefit to twine itself. If there are stub files, mypy won't even look at any of the actual twine code. If anything, stub files bring some maintenance burden as they can easily get out of date with the code, and need to be kept updated with all API changes in twine. This maintenance burden could be worth it if there are lots of people using twine's API as a library who will benefit, but I thought twine was mostly a CLI tool?

Doing annotations in the code itself (as comments, for Py2 support) is really a totally different thing from stub files, as it allows twine's own code to be type checked, and this also prevents the possibility of them getting out of date.

@bhrutledge
Copy link
Contributor

Thanks for the feedback, @carljm. There's a proposal for a "real Twine API" in #361, which I'm guessing is some of the motivation for stub files, but maybe @sigmavirus24 can elaborate.

As a newbie, it does seem easier to start bringing the value of typing to Twine by doing inline annotation. I'm inclined to take #466 in that direction, most likely using the Python 3 syntax since #437 seems likely to be merged.

@bhrutledge
Copy link
Contributor

@theacodes and @jaraco: Would you like me to keep working on this? If so, I'd be inclined to continue with #469 (which depends on the merge of #437). In the case, #466 and #344 could be closed.

@theacodes
Copy link
Member

theacodes commented Jul 8, 2019 via email

@bhrutledge bhrutledge self-assigned this Oct 3, 2019
@bhrutledge
Copy link
Contributor

With the merge of #469, we have a passing mypy check via tox -e lint-mypy. I'm planning to add more type coverage following a similar process as that PR (i.e., using MonkeyType).

I'm ambitiously aiming for a strict check, ala pypa/packaging.

@deveshks
Copy link
Contributor

Are PRs still welcome to add type annotations to different twine modules?

I see there are some of them like twine.commands.check and twine.wheel don't have type annotations added

@sigmavirus24
Copy link
Member

Yes @deveshks

@bhrutledge
Copy link
Contributor

@deveshks Yes, though if you don't mind waiting a bit, I've got a process that has worked well for me, that I think will save you time and make PR's easier to submit/review. I'll prioritize running through it again, and document it here.

@deveshks
Copy link
Contributor

Sure thing,

What I was planning to do is to add type annotations one by one, and run mypy <file_name>.py and tox -e typing to check that nothing broke. But I will wait for the process before tackling the PRs

@bhrutledge
Copy link
Contributor

Thanks @deveshks. As a preview, my process has been to use MonkeyType to generate the annotations based on the test suite. As mentioned in that #469, this involves some cleanup, but I found it more straightforward and educational than attempting to do it by hand.

However, with that in mind, working on #7 to add test coverage would benefit this process.

@bhrutledge
Copy link
Contributor

bhrutledge commented Apr 25, 2020

Roadmap to completion

i.e., when the Twine codebase passes with the mypy --strict configuration.

TODO (using the recipe below)

Open questions

  • Should we add add minimal stubs for packages that are missing them? Add stubs for some missing imports #617
  • Should we use if TYPE_CHECKING: around from typing import ... (discussion) ? @di
  • Should we prefer more generic annotations? For example:
    • Iterable[str] instead of List[str]
    • Sequence[str] instead of Tuple[str, ...]

References

Recipe for adding annotations

Run the test suite under MonkeyType tracing:

$ tox -e monkeytype
  ...
====================== 109 passed, 9 deselected in 3.52s =======================

Apply the inferred annotations to a module, format the changes, and commit:

$ tox -e monkeytype apply twine.wheel
$ tox -e format
$ git commit -am "Apply monkeytype to twine.wheel"

Add the module to the strict section of mypy.ini:

[mypy-twine.auth,twine.cli,twine.package,twine.repository,twine.utils,twine.wheel]

Review the changes and cleanup as necessary. Look out for:

  • Unused imports
  • Complex Unions
  • pretend.stub instead of a real type
  • Errors from tox -e typing
  • Linting and test failures
$ git commit -am "Clean up after monkeytype"

Open a PR.

@deveshks
Copy link
Contributor

deveshks commented Apr 25, 2020

I am done with wheel and wininst and will raise a PR for the same

I can perhaps take up annotating commands.register as well, along with _main__ and __init__.

@bhrutledge
Copy link
Contributor

bhrutledge commented Apr 25, 2020

That's great @deveshks, but let's do one PR at a time (i.e., justwheel and wininst).

@deveshks
Copy link
Contributor

That's great @deveshks, but let's do one PR at a time (i.e., justwheel and wininst).

Agreed. PR #607 adds type annotations for just wheel and wininst

@deveshks
Copy link
Contributor

Hi @bhrutledge

Please let me know if I can take up some of the remaining 5 tasks in the list to help out with this effort.

@bhrutledge
Copy link
Contributor

@deveshks Thanks again for your enthusiasm, but I don't think there's much parallel work to be done at this point. I've got the changes ready to go for the tasks with my name on them. I think the missing imports and coverage reports are related, and I've already put some work into that, and am keen to see it through.

@bhrutledge
Copy link
Contributor

Just checked off the last item in the roadmap: #231 (comment). So, I think this is done! Any enhancements to or issues with Twine's type checking should be reported as new issues.

Thanks to everyone who contributed along the way, and especially to @deveshks who, in addition to adding type coverage, gave me the nudge I needed to finish this.

@bhrutledge
Copy link
Contributor

bhrutledge commented May 23, 2020

Oh, and how could I forget: huge thanks to @brainwane for getting me started on this in the first place, and helping me put together the roadmap to completion.

@bhrutledge bhrutledge linked a pull request May 23, 2020 that will close this issue
@brainwane
Copy link
Contributor

Congratulations and thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement testing Test frameworks, tests, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants