-
-
Notifications
You must be signed in to change notification settings - Fork 719
Split PyJWT/PyJWS classes to tighten type interfaces #559
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,27 @@ def test_decodes_valid_jws(self, jws, payload): | |
|
|
||
| assert decoded_payload == payload | ||
|
|
||
| def test_decodes_complete_valid_jws(self, jws, payload): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you start enforcing Mypy on CI, I'd suggest adding some annotations to the tests (and adding tests/ to the Mypy run), as they can help catch API mistakes, as this is the only place in the code that actually uses much of the library. We've done this recently on aiohttp-jinja2, where some annotations were incorrect and only getting noticed by users previously. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Certainly would be useful, but it seems a much bigger issue to tackle and so I think is outside the scope for this particular PR. Help there would be welcome. P.S. I'm not the maintainer of this project, just a contributor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, just giving some ideas for the future. If you want to tackle it, I'd start by adding a That may be a little too strict for this project, so play around with the options (but, you'll want to change Then, adding CI support can be done with something as simple as: Let me know if you need any other help. |
||
| example_secret = "secret" | ||
| example_jws = ( | ||
| b"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." | ||
| b"aGVsbG8gd29ybGQ." | ||
| b"gEW0pdU4kxPthjtehYdhxB9mMOGajt1xCKlGGXDJ8PM" | ||
| ) | ||
|
|
||
| decoded = jws.decode_complete( | ||
| example_jws, example_secret, algorithms=["HS256"] | ||
| ) | ||
|
|
||
| assert decoded == { | ||
| "header": {"alg": "HS256", "typ": "JWT"}, | ||
| "payload": payload, | ||
| "signature": ( | ||
| b"\x80E\xb4\xa5\xd58\x93\x13\xed\x86;^\x85\x87a\xc4" | ||
| b"\x1ff0\xe1\x9a\x8e\xddq\x08\xa9F\x19p\xc9\xf0\xf3" | ||
| ), | ||
| } | ||
|
|
||
| # 'Control' Elliptic Curve jws created by another library. | ||
| # Used to test for regressions that could affect both | ||
| # encoding / decoding operations equally (causing tests | ||
|
|
||
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.
This is a perfectly fine solution, but if you wanted to maintain backwards-compatibility, this could remain the same, but use
@overload.It would look something like:
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.
We're prepping for version 2.0, so this is the chance to break backward compatibility if we must. IMO, we should aim for the desired interface rather than force support for something we can drop.
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 played around with the
overloadidea a bit. Unfortunately,Literalwas only introduced in Python 3.8.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.
Yes, that's right. Not sure you can do the overload without it.
If you wanted to go that route, you could import from
typing_extensionsfor backwards compatibility (adding that to the dependencies). Alternatively, only do the overload in Python 3.8+ (if sys.version_info >= (3, 8), and users of older versions will have to put up with theUnion(though I suspect most Mypy users are already using 3.8+).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.
Your current proposal is probably the easier option though.