-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Unable to use typehint for hashlib.sha256() #2928
Comments
|
python 3.7 from __ future__ import annotations
import hashlib
my_var_hash: hashlib._Hash
my_var_hash = hashlib.sha256()
|
This code passes both from random import choice
from typing import TYPE_CHECKING
from multihash import FUNCS, SHA2_256, SHA2_512
if TYPE_CHECKING:
from hashlib import _Hash
hash_object: "_Hash" = FUNCS[choice([SHA2_256, SHA2_512])]() |
I know this bug report is really old and already closed, but having the typing information for a library type be an expression that can't be used in regular Python (because the name doesn't actually exist in the module) is really ugly. Is there precedence for behaviour like this in other Python modules? Is there a PEP that describes the rationale for this? |
There are a few similar instances; you can find some of them by grepping for There is no PEP explicitly discussing this case. In typeshed we do the best we do to type Python interfaces that often weren't originally designed with typing in mind. If you have a concrete proposal for how to make it better, you can share it and we can potentially change either CPython or typeshed. |
Are you reporting a bug, or opening a feature request?
Bug
Please insert below the code you are checking with mypy,
What is the behavior/output you expect?
That mypy would understand hashlib._hashlib.HASH as a valid type OR that when running the code Python would not complain that there is no 'hashlib._Hash' attribute.
What are the versions of mypy and Python you are using?
mypy 0.701
Python 3.7.3 on Fedora 29
What are the mypy flags you are using? (For example --strict-optional)
No flags used
When trying to use a type annotation in Python 3.7 for an object created with hashlib.sha256() I can not seem to find a combination that will work with both running it with Python and running mypy to validate the code. Please see above my results.
Note that mypy complains that it does not know about: hashlib._hashlib
And Python has an error as it can't find: hashlib._Hash
The text was updated successfully, but these errors were encountered: