Skip to content

Commit c2bdbae

Browse files
authored
Enable strict optional by default (#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`. Also rewrote much of the documentation for strict optional checking. Fixes #359.
1 parent cdfc272 commit c2bdbae

14 files changed

+236
-112
lines changed

docs/source/command_line.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ flag (or its long form ``--help``)::
2121
[--warn-unused-ignores] [--warn-unused-configs]
2222
[--show-error-context] [--no-implicit-optional] [--no-incremental]
2323
[--quick-and-dirty] [--cache-dir DIR] [--cache-fine-grained]
24-
[--skip-version-check] [--strict-optional]
24+
[--skip-version-check] [--no-strict-optional]
2525
[--strict-optional-whitelist [GLOB [GLOB ...]]]
2626
[--always-true NAME] [--always-false NAME] [--junit-xml JUNIT_XML]
2727
[--pdb] [--show-traceback] [--stats] [--inferstats]
@@ -298,11 +298,14 @@ Here are some more useful flags:
298298
- ``--ignore-missing-imports`` suppresses error messages about imports
299299
that cannot be resolved (see :ref:`follow-imports` for some examples).
300300

301-
- ``--strict-optional`` enables strict checking of ``Optional[...]``
302-
types and ``None`` values. Without this option, mypy doesn't
301+
- ``--no-strict-optional`` disables strict checking of ``Optional[...]``
302+
types and ``None`` values. With this option, mypy doesn't
303303
generally check the use of ``None`` values -- they are valid
304-
everywhere. See :ref:`strict_optional` for more about this feature.
305-
This flag will become the default in the near future.
304+
everywhere. See :ref:`no_strict_optional` for more about this feature.
305+
306+
**Note:** Strict optional checking was enabled by default starting in
307+
mypy 0.600, and in previous versions it had to be explicitly enabled
308+
using ``--strict-optional`` (which is still accepted).
306309

307310
- ``--disallow-untyped-defs`` reports an error whenever it encounters
308311
a function definition without type annotations.

docs/source/common_issues.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ flagged as an error.
5151
5252
If you don't know what types to add, you can use ``Any``, but beware:
5353

54-
- **One of the values involved has type ``Any``.** Extending the above
54+
- **One of the values involved has type 'Any'.** Extending the above
5555
example, if we were to leave out the annotation for ``a``, we'd get
5656
no error:
5757

@@ -85,17 +85,16 @@ flagged as an error.
8585
clarity about the latter use ``--follow-imports=error``. You can
8686
read up about these and other useful flags in :ref:`command-line`.
8787

88-
- **A function annotated as returning a non-optional type returns ``None``
88+
- **A function annotated as returning a non-optional type returns 'None'
8989
and mypy doesn't complain**.
9090

9191
.. code-block:: python
9292
9393
def foo() -> str:
9494
return None # No error!
9595
96-
By default, the ``None`` value is considered compatible with everything. See
97-
:ref:`optional` for details on strict optional checking, which allows mypy to
98-
check ``None`` values precisely, and will soon become default.
96+
You may have disabled strict optional checking (see
97+
:ref:`no_strict_optional` for more).
9998

10099
.. _silencing_checker:
101100

@@ -131,6 +130,17 @@ The second line is now fine, since the ignore comment causes the name
131130
if we did have a stub available for ``frobnicate`` then mypy would
132131
ignore the ``# type: ignore`` comment and typecheck the stub as usual.
133132

133+
134+
Unexpected errors about 'None' and/or 'Optional' types
135+
------------------------------------------------------
136+
137+
Starting from mypy 0.600, mypy uses
138+
:ref:`strict optional checking <strict_optional>` by default,
139+
and ``None`` is not compatible with non-optional types. It's
140+
easy to switch back to the older behavior where ``None`` was
141+
compatible with arbitrary types (see :ref:`no_strict_optional`).
142+
143+
134144
Types of empty collections
135145
--------------------------
136146

docs/source/config_file.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ The following global flags may only be set in the global section
9292
per-module sections in the config file that didn't match any
9393
files processed in the current run.
9494

95-
- ``strict_optional`` (Boolean, default False) enables experimental
96-
strict Optional checks.
97-
9895
- ``scripts_are_modules`` (Boolean, default False) makes script ``x``
9996
become module ``x`` instead of ``__main__``. This is useful when
10097
checking multiple scripts in a single run.
@@ -180,6 +177,13 @@ overridden by the pattern sections matching the module name.
180177
- ``almost_silent`` (Boolean, deprecated) equivalent to
181178
``follow_imports=skip``.
182179

180+
- ``strict_optional`` (Boolean, default True) enables or disables
181+
strict Optional checks. If False, mypy treats ``None`` as
182+
compatible with every type.
183+
184+
**Note::** This was False by default
185+
in mypy versions earlier than 0.600.
186+
183187
- ``disallow_any_unimported`` (Boolean, default false) disallows usage of types that come
184188
from unfollowed imports (such types become aliases for ``Any``).
185189

0 commit comments

Comments
 (0)