You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ mypy tt.py
tt.py:11: error: Module has no attribute "_hashlib"
tt.py:12: error: Revealed type is 'hashlib._Hash'
$ ./tt.py
Traceback (most recent call last):
File "./tt.py.save", line 17, in <module>
def check_hash(hash_obj: hashlib._Hash) -> hashlib._Hash:
AttributeError: module 'hashlib' has no attribute '_Hash'
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
hashlib._Hash is a stub-only construct. To use it in live code, either quote it, or use from __future__ import annotations to prevent processing at runtime.
Reacted by vadella, Douglas Thor, Jakub Kaczmarzyk and Alex Suzen
python 3.7
how do you realize that have to use _Hash in hashlib._Hash and how use annotations in this context, I am trying to be able use code completion in Pycharm. It is my code:
mypy doesn't show any error. I hope use code completion in pycharm i.e. with these functions : my_var_hash.update(b'lmao') or my_var_hash.hexdigest() . what are not taking into account?, thanks in advance.
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 @type_check_only in typeshed.
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.
Reacted by Ferdinand Bachmann, Sebastian Rittau and jnicoulaud-ledger
Activity
srittau commentedon Apr 30, 2019
hashlib._Hashis a stub-only construct. To use it in live code, either quote it, or usefrom __future__ import annotationsto prevent processing at runtime.christianbueno1 commentedon Nov 19, 2019
python 3.7
how do you realize that have to use
_Hashinhashlib._Hashand how use annotations in this context, I am trying to be able use code completion in Pycharm. It is my code:mypydoesn't show any error. I hope use code completion in pycharm i.e. with these functions :my_var_hash.update(b'lmao')ormy_var_hash.hexdigest(). what are not taking into account?, thanks in advance.l0b0 commentedon Jun 14, 2022
This code passes both
mypy --strictandpylintfor dealing with an arbitrary hashing algorithm:Ferdi265 commentedon Jun 25, 2024
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?
JelleZijlstra commentedon Jun 25, 2024
There are a few similar instances; you can find some of them by grepping for
@type_check_onlyin typeshed.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.