Skip to content

Fixed: Argument for Werkzeug's Headers.pop method. #5335

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

Merged
merged 3 commits into from
May 5, 2021

Conversation

01-vyom
Copy link
Contributor

@01-vyom 01-vyom commented May 4, 2021

Closes #5147

@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2021

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@@ -191,7 +191,7 @@ class Headers(object):
def extend(self, iterable): ...
def __delitem__(self, key: Any) -> None: ...
def remove(self, key): ...
def pop(self, **kwargs): ...
def pop(self, key: str, default: Optional[str] = ...): ...
Copy link
Member

Choose a reason for hiding this comment

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

From the implementation, looks like the key can also be an int or None.

Also, default can really be of any type. For a precise return type you'll probably need two overloads: one that doesn't take default as an argument and returns str, and one that does and returns the Union of the default's type and the normal return type. get() already had some similar overloads, though it's more complicated because of other parameters.

Copy link
Contributor Author

@01-vyom 01-vyom May 4, 2021

Choose a reason for hiding this comment

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

@overload
def pop(self, key: str, default: Optional[str] = ...) -> str: ...
@overload
def pop(self, key: Optional[int], default: Optional[Tuple[str, str]] = ...) -> Tuple[str, str]: ...

What if I do something like this ? This is similar to the types present in the original code. Also, I will need to handle this for the ImmutableHeadersMixin class's pop method as well.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, didn't realize the return type changed if the key is an int. Yes, let's just go with the same types the library itself provides.

We'll eventually drop the Werkzeug stubs in typeshed once there's a release with its own types (#5337).

@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2021

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@overload
def pop(self, key: str, default: Optional[str] = ...) -> str: ...
@overload
def pop(self, key: Optional[int], default: Optional[Tuple[str, str]] = ...) -> Tuple[str, str]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method seems to be something completely different than the other pop. It is defined as:

    def pop(self, index=-1):
        is_immutable(self)

While the other pop is like dict.pop, accepting key and default, this is more like list.pop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok got that. I only added that method as they handled that in their original code here. But, I guess what you said makes more sense.

@01-vyom
Copy link
Contributor Author

01-vyom commented May 5, 2021

I made all the changes as suggested in the comments. Learned a lot today. Still a noob in typing, but will keep learning and improving.
Thank you @Akuli

@github-actions
Copy link
Contributor

github-actions bot commented May 5, 2021

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Akuli Akuli merged commit 00f2301 into python:master May 5, 2021
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.

Werkzeug Headers.pop has wrong arguments
3 participants