Skip to content

No overload variant of "get" … for os.environ with unicode_literals #1274

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

Closed
posita opened this issue May 18, 2017 · 3 comments
Closed

No overload variant of "get" … for os.environ with unicode_literals #1274

posita opened this issue May 18, 2017 · 3 comments

Comments

@posita
Copy link
Contributor

posita commented May 18, 2017

This works:

# test.py
import os
print('PWD: {}'.format(os.environ.get('PWD', '<not set>')))
$ python --version
Python 2.7.13
$ python test.py
PWD: …
$ python3 --version
Python 3.6.0
$ python3 test.py
PWD: …
$ mypy test.py
$ mypy --py2 test.py

This fails validation with --py2:

# test_unicode_literals.py
from __future__ import unicode_literals  # this is new
import os
print('PWD: {}'.format(os.environ.get('PWD', '<not set>')))
$ python test_unicode_literals.py
PWD: …
$ python3 test_unicode_literals.py
PWD: …
$ mypy test_unicode_literals.py
$ mypy --py2 test_unicode_literals.py
test_unicode_literals.py:3: error: No overload variant of "get" of "Mapping" matches argument types [builtins.unicode, builtins.unicode]

See stdlib/2/os/__init__.pyi#L67.

@posita
Copy link
Contributor Author

posita commented May 18, 2017

This seems to address the issue for Python 2:

# test_unicode_literals_workaound.py
from __future__ import unicode_literals
import os
import typing
_EnvironWorkAround = typing.MutableMapping[typing.AnyStr, typing.AnyStr]
environ = typing.cast(_EnvironWorkAround, os.environ)
print('PWD: {}'.format(environ.get('PWD', '<not set>')))
$ mypy --py2 test_unicode_literals_workaound.py

@JelleZijlstra
Copy link
Member

I'm not sure this is fixable in typeshed. python/mypy#3619 might provide a suitable workaround.

@JelleZijlstra
Copy link
Member

Duplicate of #872.

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

No branches or pull requests

2 participants