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

unpack Tuple[int, AnyStr] to _, Optional[str] #5147

Closed
dfroger opened this issue Jun 5, 2018 · 2 comments
Closed

unpack Tuple[int, AnyStr] to _, Optional[str] #5147

dfroger opened this issue Jun 5, 2018 · 2 comments

Comments

@dfroger
Copy link
Contributor

dfroger commented Jun 5, 2018

I have problems with mkstemp, which return type is Tuple[int, AnyStr]. I can't assign the second returned value to an Optional[str]:

from typing import Optional
from tempfile import mkstemp

def foo(path: Optional[str]):
    if path is None:
        f, path = mkstemp('f')

With mypy 0.600 and Python 3.6.2, I get the error:

foo.py:8: error: Value of type variable "AnyStr" of "mkstemp" cannot be "Optional[str]"
@ilevkivskyi
Copy link
Member

We are sorry for this. This is yet another duplicate of #4872 (already high priority). The current workaround is to use a temporary variable:

from typing import Optional
from tempfile import mkstemp

def foo(path: Optional[str]):
    if path is None:
        f, _path = mkstemp('f')
        path = _path  # add link to this issue for future readers of your code

@dfroger
Copy link
Contributor Author

dfroger commented Jun 5, 2018

@ilevkivskyi great thanks for pointing to the duplicated (I would not have find it by myself) and for the workaround!

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