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

call-overload error on environ.pop("…", default="…") #11831

Closed
l0b0 opened this issue Dec 23, 2021 · 4 comments · Fixed by python/typeshed#6694
Closed

call-overload error on environ.pop("…", default="…") #11831

l0b0 opened this issue Dec 23, 2021 · 4 comments · Fixed by python/typeshed#6694
Labels
bug mypy got something wrong

Comments

@l0b0
Copy link

l0b0 commented Dec 23, 2021

Bug Report

It's all in the title.

As far as I can tell it's not a duplicate of #10152, because I'm not dealing with TypeVars, or #3750, because I'm not doing any overloading myself.

To Reproduce

$ mypy --strict <(echo 'from os import environ; environ.pop("…", default="…")')
/dev/fd/63:1: error: No overload variant of "pop" of "MutableMapping" matches argument types "str", "str"  [call-overload]
/dev/fd/63:1: note: Possible overload variants:
/dev/fd/63:1: note:     def pop(self, str) -> str
/dev/fd/63:1: note:     def [_T] pop(self, str, Union[str, _T] = ...) -> Union[str, _T]
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

This should succeed. This is a regression since 0.910.

Your Environment

  • Mypy version used: 0.930
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.12
  • Operating system and version: NixOS 21.11
@l0b0 l0b0 added the bug mypy got something wrong label Dec 23, 2021
l0b0 added a commit to linz/geostore that referenced this issue Dec 23, 2021
kodiakhq bot pushed a commit to linz/geostore that referenced this issue Dec 23, 2021
* build(deps-dev): Bump mypy from 0.910 to 0.930

Bumps [mypy](https://github.com/python/mypy) from 0.910 to 0.930.
- [Release notes](https://github.com/python/mypy/releases)
- [Commits](python/mypy@v0.910...v0.930)

---
updated-dependencies:
- dependency-name: mypy
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: Work around missing tomli build dependency

* fix: Ignore mypy error

Works around <python/mypy#11831>.

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Victor Engmark <vengmark@linz.govt.nz>
@jamescooke
Copy link
Contributor

Similar problems happen with get(). A reminder that the help for environ.get() includes the default kwarg in the signature:

In [2]: os.environ.get?
Signature: os.environ.get(key, default=None)
Docstring: D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
File:      /usr/lib/python3.8/_collections_abc.py
Type:      method

With the following:

cat > papersize.py 
import os

print(os.environ.get('PAPERSIZE', default='fool'))

Then:

mypy papersize.py
papersize.py:3: error: No overload variant of "get" of "Mapping" matches argument types "str", "str"
papersize.py:3: note: Possible overload variants:
papersize.py:3: note:     def get(self, key: str) -> Optional[str]
papersize.py:3: note:     def [_T] get(self, str, Union[str, _T]) -> Union[str, _T]

I've reproduced with mypy==0.930, strict is not required and on Pythons 3.8.10 and 3.6.15.

I think these problems might be related to this change in typeshed here: python/typeshed#5772 which is an attempt to solve this error: python/typeshed#5771

What I'm wondering is what Python developers are meant to experience with this functionality? Use the os.environ methods as described in help() (with explicit and readable kwargs), but get a type error - so have to remove the helpful kwargs to get it to work correctly?! 🤔 There must be a better way!

@jamescooke
Copy link
Contributor

For anyone looking back on this issue, this is still failing as of 0.942. The connected PR in typeshed is waiting on a sync from typeshed happening in this PR: #12321

@domWalters
Copy link

I got this error today in slightly different circumstances.

mypy 1.2.0, python 3.10.10.

The example given in this issue does not error for me:

$ mypy --strict <(echo 'from os import environ; environ.pop("…", default="…")')
Success: no issues found in 1 source file

However, a modification does:

$ mypy --strict <(echo '{}.pop("…", default=None)')
/proc/self/fd/11:1: error: No overload variant of "pop" of "dict" matches argument types "str", "None"  [call-overload]
/proc/self/fd/11:1: note: Possible overload variants:
/proc/self/fd/11:1: note:     def pop(self, <nothing>, /) -> <nothing>
/proc/self/fd/11:1: note:     def [_T] pop(self, <nothing>, _T, /) -> _T
Found 1 error in 1 file (checked 1 source file)

Interestingly, calling pop on a type annotated variable, plus passing None as a positional argument instead of a keyword avoids this issue:

$ mypy --strict <(echo 'a: dict[str, str] = {}; a.pop("…", None)')
Success: no issues found in 1 source file

If you keep default=None, this will error:

$ mypy --strict <(echo 'a: dict[str, str] = {}; a.pop("…", default=None)')
/proc/self/fd/11:1: error: No overload variant of "pop" of "dict" matches argument types "str", "None"  [call-overload]
/proc/self/fd/11:1: note: Possible overload variants:
/proc/self/fd/11:1: note:     def pop(self, str, /) -> str
/proc/self/fd/11:1: note:     def [_T] pop(self, str, Union[str, _T], /) -> Union[str, _T]
Found 1 error in 1 file (checked 1 source file)

@hauntsaninja
Copy link
Collaborator

mypy's correct here:

λ python
Python 3.9.9 (main, Aug  7 2022, 02:25:38) 
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> {}.pop("...", default=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dict.pop() takes no keyword arguments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants