-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add PyJWT type annotations #1281
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 6 commits
9e14364
493044b
3c36782
9f3ac44
4060191
38d932c
3e20c57
c093029
0d5790d
a609aae
c805fcb
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from typing import Mapping, Any, Optional, Union | ||
|
|
||
| from . import algorithms | ||
|
|
||
| def decode(jwt: Union[str, bytes], key: Union[str, bytes] = ..., | ||
| verify: bool = ..., algorithms: Optional[Any] = ..., | ||
| options: Optional[Mapping[Any, Any]] = ..., | ||
| **kwargs: Any) -> Mapping[str, Any]: ... | ||
|
|
||
| def encode(payload: Mapping[str, Any], key: Union[str, bytes], | ||
| algorithm: str = ..., headers: Optional[Mapping[str, Any]] = ..., | ||
| json_encoder: Optional[Any] = ...) -> bytes: ... | ||
|
|
||
| def register_algorithm(alg_id: str, | ||
| alg_obj: algorithms.Algorithm) -> None: ... | ||
|
|
||
| def unregister_algorithm(alg_id: str) -> None: ... | ||
|
|
||
| class InvalidTokenError(Exception): pass | ||
|
|
||
| class DecodeError(InvalidTokenError): pass | ||
|
|
||
| class ExpiredSignatureError(InvalidTokenError): pass | ||
|
|
||
| class InvalidAudienceError(InvalidTokenError): pass | ||
|
|
||
| class InvalidIssuerError(InvalidTokenError): pass | ||
|
|
||
| class InvalidIssuedAtError(InvalidTokenError): pass | ||
|
|
||
| class ImmatureSignatureError(InvalidTokenError): pass | ||
|
|
||
| class InvalidKeyError(Exception): pass | ||
|
|
||
| class InvalidAlgorithmError(InvalidTokenError): pass | ||
|
|
||
| class MissingRequiredClaimError(InvalidTokenError): ... | ||
|
|
||
| # Compatibility aliases (deprecated) | ||
| ExpiredSignature = ExpiredSignatureError | ||
| InvalidAudience = InvalidAudienceError | ||
| InvalidIssuer = InvalidIssuerError | ||
|
|
||
| # These aren't actually documented, but the package | ||
| # exports them in __init__.py, so we should at least | ||
| # make sure that mypy doesn't raise spurious errors | ||
| # if they're used. | ||
| get_unverified_header = ... # type: Any | ||
| PyJWT = ... # type: Any | ||
| PyJWS = ... # type: Any | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from typing import Any | ||
|
|
||
| class Algorithm: Any # type: ignore | ||
|
Member
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. This just makes the class body be |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from typing import Callable, Any, Union | ||
| from hashlib import _DataType | ||
|
|
||
| # In reality, _HashAlg is a function of the type | ||
| # that hashlib.sha256/384/512 are, but there doesn't | ||
| # seem to be a consistent exportable name that we can reference | ||
| # for that, so we'll just say it's a Callable here. | ||
|
Member
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. I think you can just say
Contributor
Author
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. Hmm, so this is what I'd tried originally, but I get the error I noticed that in the Python 2 stubs, this type is called Any ideas?
Member
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. These stubs are in stdlib/3 so they should only use the Python 3 version. When you switch to what I suggested does it still fail?
Member
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. Oh. You need to merge/rebase. The situation in hashlib apparently changed since you last pulled from upstream.
Contributor
Author
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. Hey, I got an email notification where you said you were able to reproduce my bug and were debugging it, but visiting this page now, I'm not seeing it--did you manage to make any progress? I'll try to test my stub in the context of the Will try to get this working on my end soon! Sorry for the delay.
Member
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. Sorry, I found out I was wrong and deleted that comment, replacing it instead with "You need to merge/rebase", but I never sent that comment out. :-( So the upshot is that yes, I debugged it, and the solution is to merge from upstream. |
||
| _HashAlg = Callable[[_DataType], Any] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from typing import Any | ||
| from jwt.algorithms import Algorithm | ||
|
|
||
| from . import _HashAlg | ||
|
|
||
| class ECAlgorithm(Algorithm): | ||
| SHA256 = ... # type: _HashAlg | ||
| SHA384 = ... # type: _HashAlg | ||
| SHA512 = ... # type: _HashAlg | ||
| def __init__(self, hash_alg: _HashAlg) -> None: ... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from typing import Any | ||
| from jwt.algorithms import Algorithm | ||
|
|
||
| from . import _HashAlg | ||
|
|
||
| class RSAAlgorithm(Algorithm): | ||
| SHA256 = ... # type: _HashAlg | ||
| SHA384 = ... # type: _HashAlg | ||
| SHA512 = ... # type: _HashAlg | ||
| def __init__(self, hash_alg: _HashAlg) -> 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.
I'd remove the blank lines between the various exception classes too (but keep them between this block and the functions and non-exception definitions).