Repository navigation
Enable strict optional by default - #4971
Conversation
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.
ilevkivskyi
left a comment
There was a problem hiding this comment.
Thanks, looks good! Just some optional comments.
|
|
||
| If you don't know what types to add, you can use ``Any``, but beware: | ||
|
|
||
| - **One of the values involved has type ``Any``.** Extending the above |
There was a problem hiding this comment.
I think you can do something like **Text** ``Any`` **more text**. Then you will have both bold face and proper formatting for Any. (also below for None)
There was a problem hiding this comment.
Good idea, I'll give it a try.
| .. note:: | ||
|
|
||
| ``None`` is also used as the return type for functions that don't | ||
| return a value, i.e. that implicitly return ``None``. (Mypy doesn't |
There was a problem hiding this comment.
PEP 484 already specifies this, I am not sure we need to discuss this here again (on the other hand there was a recent issue that proposed to improve some wording there).
There was a problem hiding this comment.
Do you expect mypy users to read through PEP 484 (which is rather long and complex)? Especially for a fairly basic thing like this.
There was a problem hiding this comment.
I'd like the mypy documentation to be self-contained and PEP 484 should be strictly optional reading.
There was a problem hiding this comment.
Then I would reformulate this to make message more clear. Currently it starts from mentioning return type, but then moves to something else. Also I would not mention NoneType at all, it can only add to the confusion, I would just re-iterate that for simplicity None in type context means a type with a single value None. And separately mention that Python (this is not specific to mypy) returns None from a function that either reaches its end without return statement, or has a bare return.
There was a problem hiding this comment.
Yeah, NoneType never works in annotations. (Nor does type(None).) NoneType isn't even defined in the stdlib.
| if f == os.path.join('test-data', 'samples', 'crawl2.py'): | ||
| # This test requires 3.5 for async functions | ||
| driver.add_mypy_cmd('file {}'.format(f), ['--python-version=3.5', f]) | ||
| driver.add_mypy_cmd('file {}'.format(f), ['--python-version=3.5', |
There was a problem hiding this comment.
I am not sure we actually need the --python-version=3.5 flag. I thought mypy now uses the latest Python version by default.
There was a problem hiding this comment.
Unless I missed a PR, mypy uses the Python version uses sys.version_info of the executable used to run mypy, which still may be 3.4.
There was a problem hiding this comment.
Indeed, I was confused that the Python 3.6 syntax can be used in 2.7 stubs.
|
|
||
| If you don't know what types to add, you can use ``Any``, but beware: | ||
|
|
||
| - **One of the values involved has type ``Any``.** Extending the above |
| :ref:`optional` for details on strict optional checking, which allows mypy to | ||
| check ``None`` values precisely, and will soon become default. | ||
| You may have disabled strict optional checking (see | ||
| :ref:`no_strict_optional` for more). |
There was a problem hiding this comment.
I wonder if we should have a bullet for complaints about None as well. It will be a common issue for people who have used mypy with the default setting (for strict_optional anyway) before.
| generally check the use of ``None`` values -- they are valid | ||
| everywhere. See :ref:`strict_optional` for more about this feature. | ||
| This flag will become the default in the near future. | ||
| everywhere. See :ref:`no_strict_optional` for more about this feature. |
There was a problem hiding this comment.
Maybe mention that the default changed in mypy 0.600, like you do in the config file section about strict_optional.
|
|
||
| - ``strict_optional`` (Boolean, default True) enables or disables | ||
| strict Optional checks. If False, mypy treats ``None`` as | ||
| compatible with every type. This was False by default in mypy |
There was a problem hiding this comment.
**NOTE:** This was False ...
| doesn't treat primitives types | ||
| specially: ``None`` is also valid for primitive types such as ``int`` | ||
| and ``float``. | ||
| You can use the ``Optional`` type modifier to define a type variant |
There was a problem hiding this comment.
I'd start (redundantly) by noting that this is on by default as of 0.600.
|
|
||
| ``Optional[...]`` *does not* mean a function argument with a default value. | ||
| However, if the default value of an argument is ``None``, you can use | ||
| an optional type for the argument. |
There was a problem hiding this comment.
Maybe make this stronger? There's a flag --no-implicit-optional that requires using Optional[] here, and there's talk of deprecating the default behavior altogether.
|
|
||
| x = inc(None) # No error reported by mypy! | ||
|
|
||
| In mypy versions before 0.600 this was the default mode, and if you |
| * Publish ``mypy-lang`` version 0.4.3 on PyPI. | ||
|
|
||
| * Add :ref:`strict_optional`. | ||
| * Add ``--strict-optional``. |
| parser.add_argument('--strict-optional', action='store_true', | ||
| help=argparse.SUPPRESS) | ||
| parser.add_argument('--no-strict-optional', action='store_false', dest='strict_optional', | ||
| help="disable strict Optional checks (inverse: --no-strict-optional)") |
There was a problem hiding this comment.
I think the inverse should be --strict-optional? Also can't this use add_invertible_flag()?
| if modules: | ||
| driver.add_mypy_modules('stdlibsamples (%s)' % (version,), modules, | ||
| cwd=stdlibsamples_dir) | ||
| cwd=stdlibsamples_dir, extra_args=['--no-strict-optional']) |
Forgot to add these changes to #4971.
Forgot to add these changes to #4971.
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.