-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Type inference problem with overloaded mkdtemp #1541
Comments
As a workaround: What if the optional arguments were of type Note that this is completely untested... |
Could we solve this by a convention involving the type of the defaults?
…--Guido (mobile)
|
Yeah, using the type of the defaults would likely be a better approach. For example:
Just one default would be sufficient here. |
This highlights an interesting difference in how mypy and pytype interpret @overload in pytype will pick the first matching signature, mypy (it seems?) all matching signatures. |
Mypy doesn't just pick all matching signatures, though it's more complex than just picking the first signature. The way the example handled is a bug in mypy. Originally overloads used runtime dispatch (when used outside stubs) and mypy tried to predict precisely which overload variants could be triggered by the dispatcher. There is longer runtime dispatch, but some of the old logic still persists. One of these days I (or somebody) should clean up the implementation of overloads. Picking just the first matching signature is not correct if one of the arguments has an
There may be other tricky cases, but it's been while since I thought about overloads carefully. |
The original example appears to typecheck as expected now, most likely due to f61c2ba. I think we can close this; feel free to reopen if necessary. |
tempfile.mkdtemp
returnsstr
(in Python 2) if given no arguments. However, if any of the optional arguments isunicode
, the return type should beunicode
. We can use an overloaded function to represent this:However, mypy infers type
Any
for callmkdtemp()
, because it thinks that both signatures could match equally well. Instead, mypy should give preference to the first signature.The text was updated successfully, but these errors were encountered: