question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

encode()/decode() key parameter type should not be str

See original GitHub issue

The jwt.encode() and jwt.decode() functions declare parameter key to be str. This seems to have been the case for a while, but with new release mypy will now complain that Argument "key" has incompatible type "bytes"; expected "str" [arg-type] when this parameter is given a bytes object.

Giving it a string does not seem to make sense though, the key is inherently binary (and indeed the first thing HMAC does is throw the key into force_bytes: https://github.com/jpadilla/pyjwt/blob/fdfd6871/jwt/algorithms.py#L173-174).

Everything works as intended when you pass in bytes, it’s just the type declaration.

Expected Result

Parameter should accept bytes (and possibly not accept str)

Actual Result

mypy reports a type error when using bytes. When using str it is impossible to pass random bits (since it will be run through .encode('utf-8'))

Reproduction Steps

import secrets
import jwt

secret = secrets.token_bytes(32)
token = jwt.encode(payload={"aud": "foobar"}, key=secret, algorithm="HS256")
result = jwt.decode(
    token,
    key=secret,
    audience="foobar",
    verify=True,
    algorithms=["HS256"],
    options={"require": ["aud"]},
)

Runs fine, but mypy will report errors.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

2reactions
ps-georgecommented, Jun 16, 2022

This still seems to be an issue

1reaction
WolfgangFellgercommented, Feb 24, 2021

Yeah, was discussed over in the PR. Seems like the only practical solution at this time is going to Any.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'str' object has no attribute 'decode'. Python 3 error?
You are trying to decode an object that is already decoded. You have a str , there is no need to decode from...
Read more >
Python String encode() decode()
Python string encode() function is used to encode the string using the provided encoding. This function returns the bytes object.
Read more >
How to Encode and Decode a String in Python
Decode() function is used to convert the encoded string back into the original form. It takes the encoded string as an input and...
Read more >
Built-in Types — Python 3.11.1 documentation
The principal built-in types are numerics, sequences, mappings, classes, ... a string (with the repr() function or the slightly different str() function).
Read more >
Python Strings decode() method
decode() is a method specified in Strings in Python 2. This method is used to convert from one encoding scheme, in which argument...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found