-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
TypeConversionDict.pop with type #2888
Conversation
38d436d
to
3c8cc63
Compare
Typing is slightly messy, but I think correct. mypy certainly seems to be happy with it. I've had to battle a bit with |
@davidism anything else which needs to happen here? (all good if just waiting / no-one gotten to it. just wanna make sure i'm not sitting idle on something i'm supposed to be doing here) |
Since I often use this in a non-werkzeug context, I've extracted the TypeConversionDict into a single-file micro-package: https://github.com/MarcinKonowalczyk/type_conversion_dict. I thought it relevant to post here since i've done quite a bit of test + typing work there. The In that implementation i've also added a |
rv = type(rv) | ||
except (ValueError, TypeError): | ||
if default is _missing: | ||
return 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.
Above, we raise the error if the default is missing. Seems like it should be raised here too, not silently succeed.
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 pushed an updated version with this change. Leaving this open for discussion. Didn't fix the tests yet.
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.
After thinking a bit more, I still think raising the error makes sense. dict.pop(key)
will raise an error if the key doesn't exist, dict.pop(key, None)
can be used to silence that error. Extending that to "key exists but conversion fails" makes sense.
@overload | ||
def pop(self, key: K, default: D) -> V | D: ... | ||
@overload | ||
def pop(self, key: K, default: D, type: Callable[[V], T]) -> D | T: ... |
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 isn't valid unless the implementation of MultiDict.pop
is changed to have a type
parameter.
Similarly, poplist
should probably have a type
parameter.
2433bc5
to
f407e3c
Compare
Hmm, I'm starting to wonder if this is worth it. There's also Nothing in Werkzeug uses It seems like a general |
Pushed implementations of |
After discussing it with other maintainers, we don't want to accept the additional complexity right now, when Werkzeug itself won't use it. Thanks for working on this. |
Proposed implementation of pop with type parameter on
TypeConversionDict
. Address #2883.