-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Typing for get_values_for_type is incorrect #5889
Comments
Actually it's worse; GeneralName values can be the following:
And of course this method also returns a list of OtherName objects as well. |
@mathiasertl do you have any ideas on sane typing here? |
First, let me note that it took me 10 minutes to find out why the function was not just returning a list of GeneralName instances. Of course I'm just one data point, but for me the function code was hard to read. But of course, you're right about the bug. The situation is worse yet, because off course the same is also true for I see two possibilities right now (but will try to think of others too): Option ONE: We typehint the return value to A side note here: I don't yet get why Option TWO: We could try to overload the function (sorry @alex ;-)) to something like that (not tried it out yet, just pseudo code):
NOTE-1: Maybe we can just give x509.GeneralName here, we have to try that out. I'm also not sure how/if overloading works if we give one of the stubs |
I think the overload approach is probably best. I'm pretty sure you'll need
to use `typing.Type[x509.IPAddress]` though.
…On Sat, Mar 6, 2021 at 2:51 PM Mathias Ertl ***@***.***> wrote:
First, let me note that it took me 10 minutes to find out *why* the
function was not just returning a list of GeneralName instances. Of course
I'm just one data point, but for me the function code was hard to read.
But of course, you're right about the bug. The situation is worse yet,
because off course the same is also true for SubjectAlternativeName,
IssuerAlternativeName and CertificateIssuer. What I found out in my
projects as well, is that functions that take/return lot's of different
types are all great until you try to typehint them...
I see two possibilities right now (but will try to think of others too):
Option ONE: We typehint the return value to List[Union[str, Name, ...]].
The absolute downside is of course that mypy wont know about
get_values_for_type(DNSName) returning only strings.
A side note here: I don't yet get why Typing.Union[typing.List[str],
typing.List[OtherName]] causes troubles. We had that same issue in #5897
<#5897>. I will investigate that
further.
Option TWO: We could try to overload the function (sorry @alex
<https://github.com/alex> ;-)) to something like that (not tried it out
yet, just pseudo code):
@overload
def get_values_for_type(self, type: x509.IPAddress) -> Union[ipaddress.IPv4Address, ...]:
...
@overload
def get_values_for_type(self, type: x509.RegisteredID) -> x509.ObjectIdentifier:
...
...
def get_values_for_type(self, type: NOTE-1-BELOW) -> Union[ipaddress.IPv4Address, ...]:
....
NOTE-1: Maybe we can just give x509.GeneralName here, we have to try that
out.
I'm also not sure how/if overloading works if we give one of the stubs type:
GeneralName) -> str as a "catch all" overload. We'll have to try that out
two.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5889 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAAGBBM2MDAJAFLHW6QN2TTCKBTPANCNFSM4YQQZBRA>
.
--
All that is necessary for evil to succeed is for good people to do nothing.
|
There's also more underlying issues here, some we already discovered in other recent PRs: mypy does not update the type of re-assigned variables. If I
The solution here could be to simply return directly, instead. Update: That's why mypy didn't complain about return-typehint either. I already tried a first (incomplete) |
Alright, I have some good news and some bad news. The good news: Using overloading as described above (of course with Type, as @alex mentioned), works well. With this python code (comments are line numbers):
I get what I think we should get (right?):
The bad news: overloading is not inheritable, it seems. I tried moving this to a dedicated interface class, and it doesn't seem to work. I only found python/mypy#5146 which is going in that direction. In any case, I couldn't find a variant that does what it's supposed to do. That means that the function declaration for You can view my changes here: https://github.com/mathiasertl/cryptography/tree/bugfix/issue-5889-typehint-get-values-for-types |
So bottom line: If you're fine with those changes (in general), I'll make a PR of that branch to start the detailed review process. |
I don't really see that we have any alternative. Please go ahead and
send that PR. Thanks for working on this!
…On Sun, Mar 7, 2021 at 6:00 AM Mathias Ertl ***@***.***> wrote:
So bottom line: If you're fine with those changes (in general), I'll make a PR of that branch to start the detailed review process.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
--
All that is necessary for evil to succeed is for good people to do nothing.
|
cryptography/src/cryptography/x509/extensions.py
Lines 1392 to 1401 in 032a7f3
This is currently defined as returning a list of GeneralName objects, but it actually returns a list of strings or a list of OtherName objects (which is a type of GeneralName). However,
typing.Union[typing.List[str], typing.List[OtherName]]
causes mypy to get confused as well. We should figure out how to make this (and the other instances of this same signature inextensions.py
) correct.The text was updated successfully, but these errors were encountered: