title: Why is Python's Hashlib not strongly typed?
body: <p>Python is supposed to be strongly typed. </p>
  
  <p>For instance: <code>'abc'['1']</code> won't work, because you're expected to provide an integer there, not a string. An error wil be raised and you can go on and correct it.</p>
  
  <p>But that's not the case with hashlib. Indeed, try the following:</p>
  
  <pre><code>import hashlib
  hashlib.md5('abc') #Works OK        
  
  hashlib.md5(1) 
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: md5() argument 1 must be string or read-only buffer, not int
  
  hashlib.md5(u'abc') #Works, but shouldn't : this is unicode, not str.
  
  haslib.md5(u'é')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>Of course, it does not fail because of a <code>TypeError</code>, but because of <code>UnicodeEncodeError</code>. <code>UnicodeEncodeError</code> is supposed to be raised when you attempt to encode unicode to a string.</p>
  
  <p>I think I'm not too far from the truth when my guess is that Hashlib silently attempted to convert unicode to a string.</p>
  
  <p>Now. I agree, hashlib indicated that the argument to <code>hashlib.md5()</code> should be a string or a read-only buffer, which a unicode string is.
  But this actually goes to show that it actually is not: <code>hashlib.md5()</code> will work properly with strings and that's about it.</p>
  
  <p>Of course, the main problem this causes is that you will get an exception with some unicode strings, and not with some others.</p>
  
  <p>Which leads me to my questions. First, do you have an explanation as to why hashlib implements this behavior? Second, is it considered an issue? Third, is there a way to fix this without changing the module itself? </p>
  
  <p>Hashlib is basically an example, there are several other modules that behave the same when provided unicode strings - which leads you to an uncomfortable situation where your program will work with ASCII input but completely fail with accents.</p>