-
-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add type hints #344
RFC: Add type hints #344
Conversation
jwt/api_jws.py
Outdated
def encode(self, payload, key, algorithm='HS256', headers=None, | ||
json_encoder=None): | ||
def encode_bytes(self, | ||
payload, # type: bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we could leave this as .encode()
and make the type for payload Union[bytes, Dict]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible, doing so mypy says:
jwt/api_jwt.py:57: error: Unsupported target for indexed assignment
because the function is using the payload it as a dictionary. But before that there's a check for it and an error being thrown if it's not so I think it's fine to silence it.
The new commit does that and revert encode_bytes
to encode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is definitely a great first step, thank you so much!
@@ -55,7 +66,12 @@ def encode(self, payload, key, algorithm='HS256', headers=None, | |||
json_payload, key, algorithm, headers, json_encoder | |||
) | |||
|
|||
def decode(self, jwt, key='', verify=True, algorithms=None, options=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change jwt -> token, changes public interface and broken my code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't see it as a major change as the argument is positional, turns out I was wrong and there are users with the same problem. It has been reverted here
I decided to use the time my company, Flixbus, dedicates to FOSS projects to contribute to this package.
This PR adds type hints as described in issue #312 and includes
mypy
in Travis configuration. This means that IDEs can automatically detect type errors and results from static check tools can improve.There are a few doubts about which I'd like a feedback and for which I'm ready to work to change/fix:
Mypy complains about
PyJWT.encode
not being a subtype ofPyJWS.encode
, and the same fordecode
. In the case ofdecode
the problem is that one calls the tokenjwt
and the otherjws
, so I just called bothtoken
. As forencode
the problem is thatJWS
expects bytes andJWT
a dictionary, so I changedJWS.encode
toJWS.encode_bytes
. In the documentation there are no examples about using that function, but it is a breaking change for who uses it.The change required to import the
typing
module in atry...catch
, so it can be imported when checking the types but is ignored during normal usage of the library. It's a bit ugly but I couldn't find other ways to have it work on Python 2.7I didn't update the changelog or bump the version, if
JWS.encode
is considered part of the API I guess the major version number is affected