-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Issue with urlencode typeshed annotation starting in mypy 0.780 #4234
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
Comments
There don't look to have been any relevant typeshed changes. git bisect tells me the issue was introduced by python/mypy#8167 (the commit message mentions it expects some things to break), so possibly be worth opening on mypy. The usage of AnyStr type var looks reasonable to me (using Union or something would be looser than we want), but maybe something needs to be marked contravariant. I'll take a look when I'm more awake and let you know whether I think that commit introduced a mypy bug or if the stub was slightly wrong all along and now we know... |
I believe the problem boils down to |
The relevant types in typeshed are: _Str = Union[bytes, str]
@overload
def quote(string: str, safe: _Str = ..., encoding: Optional[str] = ..., errors: Optional[str] = ...) -> str: ...
@overload
def quote(string: bytes, safe: _Str = ...) -> str: ...
def urlencode(
query: Union[Mapping[Any, Any], Mapping[Any, Sequence[Any]], Sequence[Tuple[Any, Any]], Sequence[Tuple[Any, Sequence[Any]]]],
doseq: bool = ...,
safe: AnyStr = ...,
encoding: str = ...,
errors: str = ...,
quote_via: Callable[[str, AnyStr, str, str], str] = ...,
) -> str: ... So mypy is picking the first overload for Maybe there's a way to make this work with overloads in typeshed, but this does seem to be a mypy bug. |
The
but this is not the case: I think this can be resolved by defining a protocol: class Quote(Protocol):
@overload
def __call__(self, string: str, safe: _Str, encoding: str, errors: str): ...
@overload
def __call__(self, string: bytes, safe: _Str): ...
def urlencode(
query: Union[Mapping[Any, Any], Mapping[Any, Sequence[Any]], Sequence[Tuple[Any, Any]], Sequence[Tuple[Any, Sequence[Any]]]],
doseq: bool = ...,
safe: _Str = ...,
encoding: str = ...,
errors: str = ...,
quote_via: Quote = ...,
) -> str: ... |
This is fixed on latest mypy + typeshed, likely because of #6345. |
#6345 doesn’t resolve the four arguments vs. two arguments issue (as noted by a comment there). The protocol I proposed is still needed. |
Remove overloads and type vars. Introduce a protocol for the `quote_via` argument. This means that the interface accepted by the supplied `quote_via` is stricter, and is not dependent on the actual supplied types in the `query` argument, but must work with all possible query types. Closes: python#4234
#13815 fixes the problem mentioned by @andersk. It also removes the previously introduces overloads and just requires the |
https://mypy-play.net/?mypy=0.780&python=3.8&gist=67acdcb73a9d1fef5db0780b9a51d9c9
Short repro
Passes on 0.770 fails on 0.780
The text was updated successfully, but these errors were encountered: