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

TypeConversionDict.pop with type #2888

Closed
wants to merge 3 commits into from

Conversation

MarcinKonowalczyk
Copy link

Proposed implementation of pop with type parameter on TypeConversionDict. Address #2883.

@MarcinKonowalczyk
Copy link
Author

Typing is slightly messy, but I think correct. mypy certainly seems to be happy with it. I've had to battle a bit with ImmutableTypeConversionDict typing, but won in the end.

@davidism davidism modified the milestones: 3.0.3, 3.1.0 May 4, 2024
@pallets pallets deleted a comment from depinsutohap May 7, 2024
@MarcinKonowalczyk
Copy link
Author

@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)

@MarcinKonowalczyk
Copy link
Author

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 get and pop signatures are compatible with dict and werzeug.datastructures.TypeConversionDict.

In that implementation i've also added a required kwarg which I find very useful but i don't propose to add it here ( at least not in this PR/Issue. Obv feel free to add it too if you like it :)) )

rv = type(rv)
except (ValueError, TypeError):
if default is _missing:
return None
Copy link
Member

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.

Copy link
Member

@davidism davidism Oct 25, 2024

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.

Copy link
Member

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: ...
Copy link
Member

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.

@davidism davidism force-pushed the tcdict-pop branch 3 times, most recently from 2433bc5 to f407e3c Compare October 27, 2024 16:36
@davidism
Copy link
Member

Hmm, I'm starting to wonder if this is worth it. There's also popitem, and for MultiDict there's poplist and popitemlist, and there's also OrderedMultiDict.

Nothing in Werkzeug uses MultiDict destructively, it's either immutable, or additive-only during testing. Long term, I would like to simplify our datastructures implementation, as there is a lot of stuff we don't actually use in Werkzeug that adds complexity.

It seems like a general MultiDict/immutable implementation would be more appropriate as a separate library. Most users probably aren't reaching for an entire WSGI library just for MultiDict. That could also do away with Werkzeug-specific stuff like BadRequestKeyError.

@davidism davidism closed this Oct 27, 2024
@davidism davidism reopened this Oct 27, 2024
@davidism
Copy link
Member

Pushed implementations of MultiDict pop, popitem, poplist, and popitemlist, but no tests or type annotations. I mostly did it as an exercise, I'm still not convinced this should go in.

@davidism
Copy link
Member

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.

@davidism davidism closed this Oct 27, 2024
@davidism davidism removed this from the 3.1.0 milestone Oct 27, 2024
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.

2 participants