Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.

Fix hashlib mypy types for Python 3.x #176

Merged
merged 1 commit into from
Mar 24, 2021

Conversation

saifelse
Copy link
Contributor

As captured in python/typeshed#1663, the types for
SHA-1 and SHA-2 family of functions are callables that return a Hash instance,
whilst the SHA-3 family of functions are Hash types (at least in Python 3.6).
Mixing the two kinds of functions together in a dictionary confuses mypy's type
inference as noted in #153, so we instead add an annotation as a hint.

Also, update test_my.py to match the python version set by tox.ini in CI
instead of always targeting Python 3.7 (as configured in setup.cfg) to
validate the types in all supported Python 3.x versions.

This fix also avoids the issue with the older mypy releases for
Python 3.6 / Python 3.7 found in distro repos...

... for Ubuntu:

docker run \
  -v $(pwd):/tmp/rsa \
  -w /tmp/rsa ubuntu:18.04 \
  /bin/bash -c 'apt-get update -qqy \
                  && apt-get install -qqy python3-pyasn1 python3-setuptools python3-mypy \
                  && python3 setup.py test'

... and for Fedora:

docker run \
  -v $(pwd):/tmp/rsa \
  -w /tmp/rsa docker.io/fedora \
  /bin/bash -c 'dnf -y install wget python3-devel python3-pyasn1 python3-setuptools python3-mypy \
                  && python3 setup.py test'

Fixes #153

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.09%) to 91.534% when pulling 56a0f36 on saifelse:test-mypy into 758562f on sybrenstuvel:main.

@coveralls
Copy link

coveralls commented Feb 17, 2021

Coverage Status

Coverage increased (+0.01%) to 91.636% when pulling 1b8a31e on saifelse:test-mypy into 758562f on sybrenstuvel:main.

As captured in python/typeshed#1663, the types for
SHA-1 and SHA-2 family of functions are callables that return a Hash instance,
whilst the SHA-3 family of functions are Hash `type`s (at least in Python 3.6).
Mixing the two kinds of functions together in a dictionary confuses mypy's type
inference as noted in sybrenstuvel#153, so we instead add an annotation as a hint.

Also, update test_my.py to match the python version set by tox.ini in CI
instead of always targeting Python 3.7 (as configured in setup.cfg) to
validate the types in all supported Python 3.x versions.

This fix also avoids the issue with the older mypy releases for
Python 3.6 / Python 3.7 found in distro repos...

... for Ubuntu:
```
docker run \
  -v $(pwd):/tmp/rsa \
  -w /tmp/rsa ubuntu:18.04 \
  /bin/bash -c 'apt-get update -qqy \
                  && apt-get install -qqy python3-pyasn1 python3-setuptools python3-mypy \
                  && python3 setup.py test'
```
... and for Fedora:
```
docker run \
  -v $(pwd):/tmp/rsa \
  -w /tmp/rsa docker.io/fedora \
  /bin/bash -c 'dnf -y install wget python3-devel python3-pyasn1 python3-setuptools python3-mypy \
                  && python3 setup.py test'
```

Fixes sybrenstuvel#153
@saifelse
Copy link
Contributor Author

@sybrenstuvel : just checking in to see if there's anything I can clarify / fix with this PR to get it merged. Thanks!

Comment on lines +4 to +5
# Re-enable the standard pragma
pragma: no cover
Copy link
Owner

Choose a reason for hiding this comment

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

What does this do? What is "the standard pragma", and why does it need to be re-enabled?

Copy link
Contributor Author

@saifelse saifelse Feb 25, 2021

Choose a reason for hiding this comment

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

image

Note that when using the exclude_lines option in a configuration file, you are taking control of the entire list of regexes, so you need to re-specify the default “pragma: no cover” match if you still want it to apply.

from https://coverage.readthedocs.io/en/coverage-5.0/excluding.html#excluding

It makes sense you aren't currently using it anywhere since this line was missing, but for someone trying to ignore coverage, this is usually the first thing one might reach for, and can be confusing to the developer why # pragma: no cover ends up not working if we don't add it back

@@ -34,6 +34,11 @@

from . import common, transform, core, key

if typing.TYPE_CHECKING:
HashType = hashlib._Hash
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this only valid while type-checking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sybrenstuvel
Copy link
Owner

Thanks for the PR, appreciate it. I just added a few inline questions.

@saifelse
Copy link
Contributor Author

@sybrenstuvel : gentle nudge 🙂

@sybrenstuvel
Copy link
Owner

Gentleness appreciated :)
I'll probably be able to give this a final read-through & merge tomorrow.

@sybrenstuvel sybrenstuvel merged commit 7bdbdaa into sybrenstuvel:main Mar 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problems with tests on Fedora
3 participants