-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type checking of None values #359
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
Comments
I'd like to have Optional[x] which means Union[x, None]. As long as None is compatible with every type (as it is now), Optional[x] would be equivalent with just x and would basically be just for documentation, which is not optimal. More strict type checking of None values (i.e., turning None to an ordinary type) probably requires at least better type inference of conditional None checks. For example, consider this code:
The code is valid, but inferring that the second reference to x is never None is not entirely trivial, and there are more complex cases that will probably have to be handled as well. The current implementation of union types supports some common idioms but not all of them. |
In addition to isinstance guards, could we utilize something like single dispatch (https://docs.python.org/3/library/functools.html#functools.singledispatch) to handle these kind of assertions? Ultimately this comes down to a pattern matching issue. I think using a dispatch decorator, we could shoehorn pattern matching in. |
Mypy already has something like single dispatch ( Of course, for new code, single/multiple dispatch function decorators may often make a lot of sense. General pattern matching is tricky without new syntax. Repeated isinstance tests + type inference that knows about them is about as good as I've been able to come up with. |
Nullable types is one of the most important features we'd like in a type checker for Python at Facebook. For Hack (our typed PHP variant), assuming that arguments don't accept "null" by default catches many simple bugs before they get committed. So, all in all this is a crucial feature. That's not enough, though. Nullable types will be common in the wild. There are thousands of cases of optional ints in just the Facebook codebase. What that means is, optionality must be easy to express. Arguably As a side note: single dispatch was intentionally kept simple. The worry was that making it too powerful would possibly skew how future code is structured in Python to a form that is harder to debug and read. This is why we didn't even add method support (e.g. "skip the first argument"), let alone multiple dispatch. By just introducing single dispatch we single-handedly got rid of most problems with conflict resolution. I had to anyway re-implement C3 linearization to support abstract base classes, which is in my opinion the biggest win from the PEP. |
I agree that nullable types are important for static type checking. Also, even though supporting them is non-trivial, it seems quite feasible. Much of the needed machinery is already implemented for union types. I have some ideas for making syntax verbosity less of a problem. First, for arguments with a default value of
Also, we could infer an optional type from assigning a
Currently the type of
Verbosity could still be an issue. Most of the syntax decisions in mypy are still up for debate. I'm not completely against the syntax About your side note: Overloading in in mypy is actually pretty simple -- the variants are always checked in the order they are introduced in a source file, and the first match is chosen. There is no attempt to pick the most precise one. Actually, many forms of overlapping overload variants are disallowed by the type checker. |
I'm confused how Isn't I feel that without a proof that As for syntax, |
Is the type |
No, when used as a type, None is a shorthand for type(None). Union [] is
|
Thanks. It seems Union[…] is already implemented and working, but type None (as disjoint from every other type) is not yet implemented. I was wondering of treating type None as Union[] might make it easier to implement… On Apr 7, 2015, at 2:36 PM, Guido van Rossum [email protected] wrote:
|
Type None does not mean "not any type". It means "must be the value None". This is primarily useful to be explicit about the return value of a function that has no result (because it still returns None):
Without the |
Technically,
|
See the dupe issue #875 for more discussion. |
The flag |
IMO the ticket should remain open until strict optional checking is the default. |
As an exception, tests still don't do this since there are hundreds of tests that would have to be updated. If tests explicitly define command line options, strict optional will be on by default -- basically command line options in tests default to `--no-strict-optional`. Fixes #359.
As an exception, tests still don't do this since there are hundreds of tests that would have to be updated. If tests explicitly define command line options, strict optional will be on by default -- basically command line options in tests default to `--no-strict-optional`. Also rewrote much of the documentation for strict optional checking. Fixes #359.
A common pattern in alot of functions is to use None to signify something alternative behavior.
It might be beneficial to include a convenience Maybe type that is a partial application of Union with None.
I'm still familiarizing myself with the code and am not 100% what to do yet
The text was updated successfully, but these errors were encountered: