Skip to content

Conversation

@asottile
Copy link
Contributor

@asottile asottile commented Sep 9, 2019

Resolves #2749

see also python/cpython#11644

Copy link
Collaborator

@srittau srittau left a comment

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
Copy link
Contributor Author

asottile commented Sep 9, 2019

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
Copy link
Contributor Author

asottile commented Sep 9, 2019

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?

@asottile
Copy link
Contributor Author

hmmm now we're failing pytype 🤔

@rchen152
Copy link
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
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
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
Copy link
Contributor

andersk commented Nov 13, 2019

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