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 could pop with type #2883

Closed
MarcinKonowalczyk opened this issue Apr 23, 2024 · 3 comments
Closed

TypeConversionDict.pop could pop with type #2883

MarcinKonowalczyk opened this issue Apr 23, 2024 · 3 comments

Comments

@MarcinKonowalczyk
Copy link

MarcinKonowalczyk commented Apr 23, 2024

TypeConversionDict provides a type argument for the get method, but does not do the same for pop. Currently this is the way to do a typed pop:

d = TypeConversionDict(foo='99', bar='baz')
v = d.get('foo', type=int)
if v is not None:
     d.pop('foo')
reveal_type(v)  # int | None

I propose:

d = TypeConversionDict(foo='99', bar='baz')
v = d.pop('foo', type=int)  # currently this does not work
reveal_type(v)  # int | None

Implementation suggestion:

  def pop(self, key, default=_missing, type=None):
      value = self.get(key, default, type)
      if value is _missing:
          raise KeyError(key)
      del self[key]
      return value

I'm using _missing to correct reproduce the behaviour of pop, and re-using self.get sice we don't want the item to be pop-ed if the type-conversion fails (yes??. i guess that's the tricky bit, no?)

@MarcinKonowalczyk MarcinKonowalczyk changed the title Proposal: TypeConversionDict should also have a pop method Proposal: Should TypeConversionDict also have a pop method? Apr 23, 2024
@davidism davidism changed the title Proposal: Should TypeConversionDict also have a pop method? TypeConversionDict.pop could pop with type Apr 23, 2024
@MarcinKonowalczyk
Copy link
Author

I'm taking the change of title from a question to a statement as a tentative approval of the idea. Hence, see PR for code. The actual implementation ended up being a bit more messy than proposed but its roughly the same idea.

@davidism davidism modified the milestones: 3.0.3, 3.1.0 May 4, 2024
@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 removed this from the 3.1.0 milestone Oct 27, 2024
@MarcinKonowalczyk
Copy link
Author

Cool. No worries! Thanks for considering! :))

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants