Skip to content

dir argument to tempfile functions may take PathLike#3226

Merged
srittau merged 1 commit into
python:masterfrom
asottile:dir_pathlike_py36
Sep 29, 2019
Merged

dir argument to tempfile functions may take PathLike#3226
srittau merged 1 commit into
python:masterfrom
asottile:dir_pathlike_py36

Conversation

@asottile

@asottile asottile commented Sep 9, 2019

Copy link
Copy Markdown
Contributor

Resolves #2749

see also python/cpython#11644

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Missing imports for os and Union. Otherwise looks good to me, although personally I'd have left the Optional in the arguments and only put the differing types in _DirT.

@asottile

asottile commented Sep 9, 2019

Copy link
Copy Markdown
Contributor Author

Missing imports for os and Union. Otherwise looks good to me, although personally I'd have left the Optional in the arguments and only put the differing types in _DirT.

oops fixed, I'm spoiled by pre-commit 🙃

@asottile

asottile commented Sep 9, 2019

Copy link
Copy Markdown
Contributor Author

urgh, AnyStr is a type variable making this more difficult -- I think I need to duplicate all the signatures? is there a less wacky way to do what I want here?

Comment thread stdlib/3/tempfile.pyi Outdated
Comment thread stdlib/3/tempfile.pyi Outdated
Comment thread stdlib/3/tempfile.pyi Outdated
@asottile

Copy link
Copy Markdown
Contributor Author

hmmm now we're failing pytype 🤔

@rchen152

Copy link
Copy Markdown
Collaborator

The reason for the pytype failure is that pytype looks for the presence of type variables defined in the same pyi file to figure out whether an alias is generic. If you define _DirT as:

from typing import TypeVar
_T = TypeVar("_T")
if sys.version_info >= (3, 6):
    _DirT = Union[_T, os.PathLike[_T]]
else:
    _DirT = Union[_T]

it should work.

@asottile

Copy link
Copy Markdown
Contributor Author

The reason for the pytype failure is that pytype looks for the presence of type variables defined in the same pyi file to figure out whether an alias is generic. If you define _DirT as:

from typing import TypeVar
_T = TypeVar("_T")
if sys.version_info >= (3, 6):
    _DirT = Union[_T, os.PathLike[_T]]
else:
    _DirT = Union[_T]

it should work.

this has different semantics than AnyStr

@rchen152

Copy link
Copy Markdown
Collaborator

Everywhere you're using _DirT, it's _DirT[AnyStr], which should replace the _T with AnyStr, unless I've misunderstood how generic type aliases work.

And you can always just copy the definition of AnyStr for _T.

@srittau
srittau merged commit c0371df into python:master Sep 29, 2019
@asottile
asottile deleted the dir_pathlike_py36 branch September 29, 2019 14:09
@andersk

andersk commented Nov 13, 2019

Copy link
Copy Markdown
Contributor

This seems to cause a regression on Python 3.5; see #3449.

$ echo 'import tempfile; tempfile.mkstemp(dir="/var/tmp")' | mypy /dev/stdin
dev/stdin:1: error: Argument "dir" to "mkstemp" has incompatible type "str"; expected "None"
Found 1 error in 1 file (checked 1 source file)

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.

NamedTemporaryFile / mkstemp appear to allow Path for dir= parameter though undocumented

7 participants