Skip to content

Unable to use typehint for hashlib.sha256() #2928

Closed

Description

@JohnVillalovos
  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,

#!/usr/bin/python3 -ttu
import hashlib
import sys

def main() -> int:
    m = hashlib.sha256()
    print("type:", type(m.digest()))
    print("len:", len(m.digest()))
    print("sizeof:", sys.getsizeof(m))
    print("type:", type(m))
    print(hashlib._hashlib.HASH)
    reveal_type(m)

    return 0

def check_hash(hash_obj: hashlib._Hash) -> hashlib._Hash:
    hash_obj.update(b"0")
    return hash_obj


if '__main__' == __name__:
    sys.exit(main())
  • What is the actual behavior/output?
$ 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

Activity

  1. srittau commented on Apr 30, 2019

    @srittau
    Collaborator

    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.

  2. christianbueno1 commented on Nov 19, 2019

    @christianbueno1

    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:

    from __ future__ import annotations
    import hashlib
    
    my_var_hash: hashlib._Hash
    my_var_hash = hashlib.sha256()

    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.

  3. l0b0 commented on Jun 14, 2022

    @l0b0

    This code passes both mypy --strict and pylint for dealing with an arbitrary hashing algorithm:

    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])]()
  4. Ferdi265 commented on Jun 25, 2024

    @Ferdi265

    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?

  5. JelleZijlstra commented on Jun 25, 2024

    @JelleZijlstra
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions