TypeError: Strings must be encoded before hashing with example code
Hi, I am trying to learn twisted, and heavily relying on examples to learn. The last example on this page https://docs.twisted.org/en/twisted-18.7.0/core/howto/pb-cred.html doesn't work, is that because of the switch from python 2 to python 3, because of the new distinction between strings in byte strings ? If so, is that a bug or is there a workaround to make the example code work ? Thank you in advance.
On Dec 14, 2025, at 3:07 PM, via Twisted <twisted@python.org> wrote:
Hi, I am trying to learn twisted, and heavily relying on examples to learn. The last example on this page https://docs.twisted.org/en/twisted-18.7.0/core/howto/pb-cred.html doesn't work, is that because of the switch from python 2 to python 3, because of the new distinction between strings in byte strings ? If so, is that a bug or is there a workaround to make the example code work ?
Yes, this is most likely broken because we don't have test coverage, or even type checking, for our documentation. Sorry for the trouble. If I run `mypy docs/core/howto/listings/pb/chatserver.py` in a checkout of current trunk, I see a bunch of errors, but most of them are related to the example itself not being annotated. However, there are many errors like this: docs/core/howto/listings/pb/chatserver.py:70:17: error: Argument 1 to "addUser" of "InMemoryUsernamePasswordDatabaseDontUse" has incompatible type "str"; expected "bytes" [arg-type] docs/core/howto/listings/pb/chatserver.py:70:26: error: Argument 2 to "addUser" of "InMemoryUsernamePasswordDatabaseDontUse" has incompatible type "str"; expected "bytes" [arg-type] which suggests that a couple of `b` prefixes on some of those strings might fix it. The source for this example is here: https://github.com/twisted/twisted/blob/trunk/docs/core/howto/listings/pb/ch... Would you have any interest in submitting a small pull request to fix it? You don't need to add automated test coverage for the example, given that none exists currently, and it might be a hassle to set up a testing environment for that just to fix a couple of small type issues. Thanks, -g
Thank you in advance. _______________________________________________ Twisted mailing list -- twisted@python.org To unsubscribe send an email to twisted-leave@python.org https://mail.python.org/mailman3/lists/twisted.python.org/ Message archived at https://mail.python.org/archives/list/twisted@python.org/message/KER632VC775... Code of Conduct: https://twisted.org/conduct
On 16/12/2025 00:49, glyph@twistedmatrix.com wrote:
On Dec 14, 2025, at 3:07 PM, via Twisted <twisted@python.org> wrote:
The last example on this page https://docs.twisted.org/en/ twisted-18.7.0/core/howto/pb-cred.html doesn't work, is that because
...
Yes, this is most likely broken because we don't have test coverage, or even type checking, for our documentation. Sorry for the trouble.
I've used https://sybil.readthedocs.io/en/latest/ for at least one Twisted-based project, it could probably work well here, but no, I don't have time to work on this :'(
The source for this example is here: https://github.com/twisted/twisted/ blob/trunk/docs/core/howto/listings/pb/chatserver.py <https:// github.com/twisted/twisted/blob/trunk/docs/core/howto/listings/pb/ chatserver.py>
Would you have any interest in submitting a small pull request to fix it? You don't need to add automated test coverage for the example, given that none exists currently, and it might be a hassle to set up a testing environment for that just to fix a couple of small type issues.
...but if you'd like to give Sybil a go for wider impact, please let me know if you hit any problems! I'd use the unittest integration: https://sybil.readthedocs.io/en/latest/use.html#test-runner-integration cheers, Chris
On Dec 15, 2025, at 10:36 PM, Chris Withers <chris@withers.org> wrote:
On 16/12/2025 00:49, glyph@twistedmatrix.com wrote:
On Dec 14, 2025, at 3:07 PM, via Twisted <twisted@python.org> wrote:
The last example on this page https://docs.twisted.org/en/ twisted-18.7.0/core/howto/pb-cred.html doesn't work, is that because ...
Yes, this is most likely broken because we don't have test coverage, or even type checking, for our documentation. Sorry for the trouble.
I've used https://sybil.readthedocs.io/en/latest/ for at least one Twisted-based project, it could probably work well here, but no, I don't have time to work on this :'(
No worries, thanks for the pointer. I also don't have time for this project but hopefully someone else could run with it :) -g
participants (3)
-
Chris Withers -
glyph@twistedmatrix.com -
maxx.gonzalezz1866@gmail.com