Skip to content
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

Add more stubs for cryptography #3307

Merged
merged 4 commits into from
Nov 4, 2019
Merged

Add more stubs for cryptography #3307

merged 4 commits into from
Nov 4, 2019

Conversation

jlaine
Copy link
Contributor

@jlaine jlaine commented Oct 5, 2019

No description provided.

@jlaine
Copy link
Contributor Author

jlaine commented Oct 5, 2019

The x509 stub still needs some love, I haven't had a chance to properly represent the "Name" class.

@@ -0,0 +1,13 @@
class CMACBackend: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably these things have some methods? If you don't have time to stub them all out, we should add a def __getattr__(self, name: str) -> Any to avoid false positives for people using these classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backends themselves are private API.. so it's debatable whether they should be declared at all.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are documented, though? Also, they should all have a metaclass=ABCMeta base. Same is true for several of the interfaces in other files.

@jlaine
Copy link
Contributor Author

jlaine commented Oct 5, 2019

One thing I noticed is that the signature for load_pem_private_key looks wrong, I believe the password argument is bytes, not str.

@jlaine
Copy link
Contributor Author

jlaine commented Oct 5, 2019

CI seems to be failing because Python 2 lacks ipaddress, see #3309.

It's a bit annoying having to deal with Python 2 less than 3 months before EOL..

Edit: this is no longer an issue, ipaddress is now in third_party/2

@JelleZijlstra
Copy link
Member

I think the EOL of Python 2 in typeshed is probably going to be later than December 31.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for these stubs! I left some remarks below, not a full review yet.

third_party/2and3/cryptography/exceptions.pyi Show resolved Hide resolved
@@ -0,0 +1,13 @@
class CMACBackend: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are documented, though? Also, they should all have a metaclass=ABCMeta base. Same is true for several of the interfaces in other files.

@jlaine jlaine force-pushed the cryptography branch 12 times, most recently from 6645405 to dde4a57 Compare October 18, 2019 15:22
@jlaine jlaine force-pushed the cryptography branch 3 times, most recently from 4fe102a to 353ba30 Compare October 18, 2019 19:20
@jlaine
Copy link
Contributor Author

jlaine commented Oct 18, 2019

@srittau I have added a lot more stubs, feel free to review.

I have extensively read the cryptography documentation, and AFAICT the only module which is not yet fully typed is x509.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very helpful, thank you for the stubs! I am sorry for the delay in reviewing, other things came up. For now I just managed to review up to hazmat/primitives/asymmetric, but I hope to review the rest soon.

A few notes below, none of them showstoppers. Most of them are just repeats of the same issue, but I pointed them out in each place to (hopefully) make it easier to fix.

third_party/2and3/cryptography/fernet.pyi Outdated Show resolved Hide resolved
third_party/2and3/cryptography/exceptions.pyi Show resolved Hide resolved
third_party/2and3/cryptography/fernet.pyi Outdated Show resolved Hide resolved
third_party/2and3/cryptography/fernet.pyi Outdated Show resolved Hide resolved
third_party/2and3/cryptography/fernet.pyi Outdated Show resolved Hide resolved
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very helpful, thank you for the stubs! I am sorry for the delay in reviewing, other things came up. For now I just managed to review up to hazmat/primitives/asymmetric, but I hope to review the rest soon.

A few notes below, none of them showstoppers. Most of them are just repeats of the same issue, but I pointed them out in each place to (hopefully) make it easier to fix.

@jlaine
Copy link
Contributor Author

jlaine commented Oct 30, 2019

Thanks so much for taking the time for this first review, I've addressed all the issues you pointed out. The changes related to abstract properties seem to have broken CI though, it looks as though I need to go over all the subclasses to add the corresponding concrete properties.

)

class CipherBackend(metaclass=ABCMeta):
@abstractmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not bother including the various backend API methods -- basically no user should ever be invoking them, so it's a lot of code for relatively little benefit (that's my opinion at least, maybe the approach of typeshed is different)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my opinion too, but as @srittau pointed out in #3307 (comment) these methods are part of the cryptography documentation.. so public API?

@jlaine
Copy link
Contributor Author

jlaine commented Oct 30, 2019

Since @alex is weighing in on this PR, it might be a good time to decide whether these type hints should live in cryptography itself or it typeshed.

I'm happy with both approaches, as long as it lets me typecheck my code :)

@alex
Copy link
Member

alex commented Oct 30, 2019

We're unlikely to be convinced to bring these in-tree for cryptography until we're python3 only and can use the native syntax.

@srittau
Copy link
Collaborator

srittau commented Nov 3, 2019

@alex I'd just like to point out that the syntax of stubs is independent of the supported Python version. Even when checking Python 2.7 code, type checkers will support stub files using newer features.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished the review now. Just one more note below. Please note that due to the size of the PR, I didn't compare the stubs against implementation or documentation like I usually do, I just looked for obvious issues.

@alex
Copy link
Member

alex commented Nov 3, 2019

Yes, I understand the stubs syntax works in both Python2/3. We don't plan to adopt type hints until they can be done directly inline in the source code.

@srittau
Copy link
Collaborator

srittau commented Nov 3, 2019

Seems sensible. We are happy to have them in typeshed as long as required.

@srittau
Copy link
Collaborator

srittau commented Nov 4, 2019

Looks good, I will merge when the build failure is fixed.

@jlaine
Copy link
Contributor Author

jlaine commented Nov 4, 2019

Let's see if CI completes this time. I caught a couple of other places in hazmat.ciphers.algorithms where abstract properties were needed instead of members.

Going forward, I don't plan to do a "hit and run", please mention me if any issues crop up related to these new stubs. I also plan to expand the x509 module as time allows.

@srittau srittau merged commit 047caa9 into python:master Nov 4, 2019
@jlaine
Copy link
Contributor Author

jlaine commented Nov 4, 2019

Thanks for all your guidance @srittau, this is one friendly project!

@jlaine jlaine deleted the cryptography branch November 6, 2019 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants