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

Add mypy to CI #693

Merged
merged 11 commits into from
Nov 2, 2021
Merged

Add mypy to CI #693

merged 11 commits into from
Nov 2, 2021

Conversation

DMRobertson
Copy link
Contributor

Following the strategy outlined in #692 (comment), I've

  • Configured an initial, gentle version of mypy
  • Added this to the CI in the meta step
  • Fixed up mypy's initial complaints.

placeholder for now, to get mypy passing
Sadly I can't import `SupportBytes` from typing_extensions, so for 3.8
and below I need to define it myself.
@DMRobertson DMRobertson marked this pull request as ready for review October 21, 2021 19:04
@DMRobertson
Copy link
Contributor Author

Not sure why the pypy CI build failed. Looks like it couldn't build typed-ast from source because codecs.h wasn't on the C include path?

@Dreamsorcerer
Copy link
Contributor

Not sure why the pypy CI build failed. Looks like it couldn't build typed-ast from source because codecs.h wasn't on the C include path?

Looks like there was a related issue on aiohttp, but I don't know the details: aio-libs/aiohttp#5496

@DMRobertson
Copy link
Contributor Author

Looks like there was a related issue on aiohttp, but I don't know the details: aio-libs/aiohttp#5496

Seems like the underlying cause is python/typed_ast#111 . One of the linked PRs was nolar/kopf#845 , which decided to only run mypy under CPython. So maybe it's best to pull mypy out from the meta job and into its own job, and then configure GHA to only run that under CPython.

@reaperhulk
Copy link
Member

I would prefer that this be structured like the mypy job on pyca/cryptography (which adds a new mypy toxenv and only installs the deps there). This avoids the pypy issue as well and gives us some consistency across the projects (which is useful for me!).

Other than that this looks good, although I'd also like to get the tests repo into the mypy job as quickly as we can since obviously this doesn't do much yet. Subsequent PRs can take care of that as we land chunks of the larger work!

Thanks for all the work you're doing here; this will be a great addition for the next release.

@DMRobertson
Copy link
Contributor Author

DMRobertson commented Oct 26, 2021

@reaperhulk Many thanks, I'll try to get that done soon. But to double check:

I would prefer that this be structured like the mypy job on pyca/cryptography (which adds a new mypy toxenv and only installs the deps there). This avoids the pypy issue as well and gives us some consistency across the projects (which is useful for me!).

Were you thinking of pyca/bcrypt here? AFAICS the pyca/cryptography tox configuration lumps mypy in to the flake env.

setup.py Outdated Show resolved Hide resolved
src/nacl/types.py Outdated Show resolved Hide resolved
@reaperhulk reaperhulk merged commit ea0db5f into pyca:main Nov 2, 2021
@reaperhulk
Copy link
Member

Thanks for working with me on this! I look forward to the next PR 😄 If at all possible it'd be great to split this into as many PRs as is reasonably feasible.

DMRobertson added a commit to DMRobertson/pynacl that referenced this pull request Nov 3, 2021
Following on from pyca#693, I've pulled out the changes in pyca#692 which relate
to `nacl.exceptions` (and applied a suggestion of @Dreamsorcerer
in the process). I picked this module because it was a leaf: it doesn't
import any other things from `nacl`.

The code changes are small; instead, I think we'll have more to say
about the mypy config changes. I've turned on a bunch of extra checks
for `nacl.exceptions` only. I'd hoped that I could enable strict mode on
a module-by-module basis, but that doesn't seem to be possible. So
instead, I went through the
[mypy documentation](https://mypy.readthedocs.io/en/stable/config_file.html)
and turned on every check that sounded good for type safety.

(At work, I've found disallowing the propagation of Any, and
`disallow_untyped_defs` the most useful: they help ensure that mypy is
actually able to do type checking in the first place!)

If you're happy with this list of checks, my plan would be to take on
other modules and add them one-by-one to the "strict" section in
`pyproject.toml`. In the long run I'd like to have everything in
`pynacl` under this list, at which point we can drop the per-module
override and have a single set of package-wide configuration for mypy.
@DMRobertson
Copy link
Contributor Author

Thanks for working with me on this! I look forward to the next PR smile If at all possible it'd be great to split this into as many PRs as is reasonably feasible.

My pleasure: thank you for having me. My plan is to tackle each module in a PR by itself, starting with #694.

reaperhulk pushed a commit that referenced this pull request Nov 3, 2021
Following on from #693, I've pulled out the changes in #692 which relate
to `nacl.exceptions` (and applied a suggestion of @Dreamsorcerer
in the process). I picked this module because it was a leaf: it doesn't
import any other things from `nacl`.

The code changes are small; instead, I think we'll have more to say
about the mypy config changes. I've turned on a bunch of extra checks
for `nacl.exceptions` only. I'd hoped that I could enable strict mode on
a module-by-module basis, but that doesn't seem to be possible. So
instead, I went through the
[mypy documentation](https://mypy.readthedocs.io/en/stable/config_file.html)
and turned on every check that sounded good for type safety.

(At work, I've found disallowing the propagation of Any, and
`disallow_untyped_defs` the most useful: they help ensure that mypy is
actually able to do type checking in the first place!)

If you're happy with this list of checks, my plan would be to take on
other modules and add them one-by-one to the "strict" section in
`pyproject.toml`. In the long run I'd like to have everything in
`pynacl` under this list, at which point we can drop the per-module
override and have a single set of package-wide configuration for mypy.
DMRobertson added a commit to DMRobertson/pynacl that referenced this pull request Nov 3, 2021
Pulled out from pyca#692.

After we realised in pyca#693 that we don't need `typing_extensions` to use
`SupportsBytes`, I didn't want to pull in `typing_extensions` again for
just one use of `Protocol`. (I have no other plans to use `Protocol` in
other annotations.)

My understanding: doing so means that we can't run `mypy` under Python
3.6 or 3.7 to fully typecheck the package. Maybe that's fine? I guess
the downside is that someone using `nacl` in a 3.6 or 3.7 project can't
fully benefit from these annotations. What're your thoughts here?

(As a compromise, I could do a `try-except` dance to use from
`typing_extensions.Protocol`, if it happens to be available.)
reaperhulk pushed a commit that referenced this pull request Nov 7, 2021
* Type annotations for `nacl.encoding`

Pulled out from #692.

After we realised in #693 that we don't need `typing_extensions` to use
`SupportsBytes`, I didn't want to pull in `typing_extensions` again for
just one use of `Protocol`. (I have no other plans to use `Protocol` in
other annotations.)

My understanding: doing so means that we can't run `mypy` under Python
3.6 or 3.7 to fully typecheck the package. Maybe that's fine? I guess
the downside is that someone using `nacl` in a 3.6 or 3.7 project can't
fully benefit from these annotations. What're your thoughts here?

(As a compromise, I could do a `try-except` dance to use from
`typing_extensions.Protocol`, if it happens to be available.)

* Use an ABC instead of a protocol

* Format with black

* Docstrings to appease the `coverage` check
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants