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

special rules in typeshed pyi #270

Closed
matthiaskramm opened this issue Jun 6, 2016 · 13 comments
Closed

special rules in typeshed pyi #270

matthiaskramm opened this issue Jun 6, 2016 · 13 comments

Comments

@matthiaskramm
Copy link
Contributor

matthiaskramm commented Jun 6, 2016

Let's use this issue to track special stub rules that found their way into typeshed stubs. Since mypy is currently the only system used for testing typeshed, it's natural for some mypy logic to become reflected in the typeshed stubs.

So far I have:

(I'll add these to README.md once we have a complete list.)

@matthiaskramm
Copy link
Contributor Author

  • When something is declared as taking int (in the PY2 stubs), then it also takes long. (And the other way around)

@smontanaro
Copy link

Is int <-> long implicit, or does everywhere I have "int" need to be changed to "Union[int, long]"?

@matthiaskramm
Copy link
Contributor Author

It's implicit. This whole thread is about things that are implicit. (And the int <-> long equivalence actually is useful, precisely so that you don't have to use Union[int, long])

@matthiaskramm
Copy link
Contributor Author

  • If a function is declared as taking bool, it also takes NoneType.

@JukkaL
Copy link
Contributor

JukkaL commented Aug 22, 2016

  • int being compatible with float is mentioned in PEP 484, so it's not a mypy-specific thing (https://www.python.org/dev/peps/pep-0484/#the-numeric-tower).
  • When using mypy with --strict-optional, None is no longer valid when bool is expected. Currently mypy considers None always valid for any type by default, not just for bool. Typeshed should use Optional[...] consistently. I think that providing None when bool is expected should generally be an error (except maybe if that turns out to generate a lot of false positives).

@matthiaskramm
Copy link
Contributor Author

Thanks for the clarification! I'll edit the posts above.

I think that providing None when bool is expected should generally be an error.

That's what pytype used to do. But our internal users were complaining about it, so we now allow None, too. It seems "safe" enough. At least, it's not clear what a false negative would look like. (Maybe a function without a return?)

@JukkaL
Copy link
Contributor

JukkaL commented Aug 23, 2016

Here are some potential cases where None is not valid when expecting a bool:

  • Value is encoded as JSON. False and None will be encoded differently.
  • Value is serialized using some other format where False and None encode differently.
  • Value is converted to string (str(None) vs. str(False)), perhaps as part of an error message.
  • Comparisons like x == False (yes, they are bad style but they happen).

@JelleZijlstra
Copy link
Member

Is it worth adding the unicode/str and int/long promotions for Python 2.7 to PEP 484? Does pytype behave differently from mypy?

@gvanrossum
Copy link
Member

I like the idea of specifying promotions in PEP 484. We'd have to have consensus from PyCharm (@vlasovskikh) and pytype (@matthiaskramm).

@matthiaskramm
Copy link
Contributor Author

+1
This should definitely be documented in a more prominent place, instead of in the depths of the typeshed issue tracker.
pytype does the same thing as mypy.

JelleZijlstra added a commit to JelleZijlstra/peps that referenced this issue Apr 24, 2017
This was discussed in python/typeshed#270 (comment). Both mypy and pytype behave as described already. We should document this behavior in the PEP to make sure people are aware of it and promote uniformity.

Mypy also implicitly promotes bytearray to bytes in both Python 2 and 3. Not sure if that's worth codifying in the PEP too.

@vlasovskikh Is this also OK for PyCharm?
@JelleZijlstra
Copy link
Member

python/peps#245

gvanrossum pushed a commit to python/peps that referenced this issue Apr 24, 2017
This was discussed in python/typeshed#270 (comment). Both mypy and pytype behave as described already. We should document this behavior in the PEP to make sure people are aware of it and promote uniformity.

Mypy also implicitly promotes bytearray to bytes in both Python 2 and 3. Not sure if that's worth codifying in the PEP too.

@vlasovskikh Is this also OK for PyCharm?
@vlasovskikh
Copy link
Member

+1. PyCharm already supports these promotions except long -> int.

@JelleZijlstra
Copy link
Member

All of the examples mentioned here have now been standardized in PEP 484, and we should not be standardizing any other mypy peculiarities in typeshed, so I don't see a reason to keep this open.

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

6 participants