Skip to content

Fix blake2 binding #1663

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 2 commits into from
Nov 9, 2017
Merged

Conversation

quantsini
Copy link
Contributor

Addresses #1651. Also a continuation of #1609, thanks @i80and for the initial pr!

In addition to blake2*, the sha3* and shake* algorithms return types in hashlib,
e.g.

In [1]: import hashlib

In [2]: type(hashlib.md5)
Out[2]: builtin_function_or_method

In [3]: type(hashlib.sha3_512)
Out[3]: type

This means the following would fail in mypy

import hashlib
def f(in: type) -> None:
    assert type(in) == type
f(hashlib.sha3_512)

with <>:3: error: Argument 1 to "f2" has incompatible type "Callable[[Union[bytes, bytearray, memoryview]], _Hash]"; expected "type"

i80and and others added 2 commits October 10, 2017 10:16
Currently calling `hashlib.blake2b` results in the following type errors:

Cannot instantiate abstract class '_BlakeHash' with abstract attributes 'copy', 'digest', 'hexdigest' and 'update'
Missing positional arguments "data", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node" in call to "_BlakeHash"
Modifies the type signatures of:
 * blake2b
 * blake2s
 * sha3_224
 * sha3_256
 * sha3_384
 * sha3_512
 * shake_128
 * shake_256
To reflect the types that are implemented in the standard library.
These should be exposed as `type`s instead of `builtin_function_or_method`s.
e.g.
In [40]: type(hashlib.blake2b)
Out[40]: type

In [41]: type(hashlib.md5)
Out[41]: builtin_function_or_method
@JelleZijlstra JelleZijlstra merged commit 44f7869 into python:master Nov 9, 2017
@JelleZijlstra
Copy link
Member

Thanks! Sorry for overlooking this PR for a while.

saifelse added a commit to saifelse/python-rsa that referenced this pull request Feb 17, 2021
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
sybrenstuvel pushed a commit to sybrenstuvel/python-rsa that referenced this pull request Mar 24, 2021
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 #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
sybrenstuvel pushed a commit to sybrenstuvel/python-rsa that referenced this pull request Mar 24, 2021
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 #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
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