Skip to content

Commit c079708

Browse files
JukkaLGuido van Rossum
authored and
Guido van Rossum
committed
Document --allow-redefinition (#6345)
Also document the config file option. Fixes #6230
1 parent 5ff5f3b commit c079708

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

docs/source/command_line.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,21 @@ of the above sections.
371371
This flag causes mypy to suppress errors caused by not being able to fully
372372
infer the types of global and class variables.
373373

374+
``--allow-redefinition``
375+
By default, mypy won't allow a variable to be redefined with an
376+
unrelated type. This flag enables redefinion of a variable with an
377+
arbitrary type *in some contexts*: only redefinitions within the
378+
same block and nesting depth as the original definition are allowed.
379+
Example where this can be useful:
380+
381+
.. code-block:: python
382+
383+
def process(items: List[str]) -> None:
384+
# 'items' has type List[str]
385+
items = [item.split() for item in items]
386+
# 'items' now has type List[List[str]]
387+
...
388+
374389
``--strict``
375390
This flag mode enables all optional error checking flags. You can see the
376391
list of flags enabled by strict mode in the full ``mypy --help`` output.

docs/source/config_file.rst

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ assume here is some 3rd party library you've installed and are importing. These
122122
1. Selectively disallow untyped function definitions only within the ``mycode.foo``
123123
package -- that is, only for function definitions defined in the
124124
``mycode/foo`` directory.
125-
126-
2. Selectively *disable* the "function is returning any" warnings within
125+
126+
2. Selectively *disable* the "function is returning any" warnings within
127127
``mycode.bar`` only. This overrides the global default we set earlier.
128128

129129
3. Suppress any error messages generated when your codebase tries importing the
@@ -155,22 +155,22 @@ Note: this section describes options that can be used both globally and per-modu
155155
See below for a list of import discovery options that may be used
156156
:ref:`only globally <config-file-import-discovery-global>`.
157157

158-
``ignore_missing_imports`` (bool, default False)
158+
``ignore_missing_imports`` (bool, default False)
159159
Suppresses error messages about imports that cannot be resolved.
160160

161161
If this option is used in a per-module section, the module name should
162162
match the name of the *imported* module, not the module containing the
163163
import statement.
164164

165-
``follow_imports`` (string, default ``normal``)
165+
``follow_imports`` (string, default ``normal``)
166166
Directs what to do with imports when the imported module is found
167-
as a ``.py`` file and not part of the files, modules and packages
167+
as a ``.py`` file and not part of the files, modules and packages
168168
provided on the command line.
169169

170170
The four possible values are ``normal``, ``silent``, ``skip`` and
171171
``error``. For explanations see the discussion for the
172-
:ref:`--follow-imports <follow-imports>` command line flag.
173-
172+
:ref:`--follow-imports <follow-imports>` command line flag.
173+
174174
If this option is used in a per-module section, the module name should
175175
match the name of the *imported* module, not the module containing the
176176
import statement.
@@ -228,7 +228,7 @@ section of the command line docs.
228228
annotations.
229229

230230
``disallow_incomplete_defs`` (bool, default False)
231-
Disallows defining functions with incomplete type annotations.
231+
Disallows defining functions with incomplete type annotations.
232232

233233
``check_untyped_defs`` (bool, default False)
234234
Type-checks the interior of functions without type annotations.
@@ -265,7 +265,7 @@ section of the command line docs.
265265
``warn_unused_ignores`` (bool, default False)
266266
Warns about unneeded ``# type: ignore`` comments.
267267

268-
``warn_no_return`` (bool, default True)
268+
``warn_no_return`` (bool, default True)
269269
Shows errors for missing return statements on some execution paths.
270270

271271
``warn_return_any`` (bool, default False)
@@ -287,6 +287,12 @@ no analog available via the command line options.
287287
``ignore_errors`` (bool, default False)
288288
Ignores all non-fatal errors.
289289

290+
Miscellaneous strictness flags
291+
------------------------------
292+
293+
``allow_redefinition`` (bool, default false)
294+
Allows variables to be redefined with an arbitrary type, as long as the redefinition
295+
is in the same block and nesting level as the original definition.
290296

291297
Global-only options
292298
*******************
@@ -302,7 +308,7 @@ For more information, see the :ref:`import discovery <import-discovery>`
302308
section of the command line docs.
303309

304310
Note: this section describes only global-only import discovery options. See above for
305-
a list of import discovery options that may be used
311+
a list of import discovery options that may be used
306312
:ref:`both per-module and globally <config-file-import-discovery-per-module>`.
307313

308314
``namespace_packages`` (bool, default False)
@@ -330,7 +336,7 @@ Platform configuration
330336
For more information, see the :ref:`platform configuration <platform-configuration>`
331337
section of the command line docs.
332338

333-
``python_version`` (string)
339+
``python_version`` (string)
334340
Specifies the Python version used to parse and check the target
335341
program. The string should be in the format ``DIGIT.DIGIT`` --
336342
for example ``2.7``. The default is the version of the Python
@@ -343,11 +349,11 @@ section of the command line docs.
343349
``sys.platform`` variable.
344350

345351
``always_true`` (comma-separated list of strings)
346-
Specifies a list of variables that mypy will treat as
352+
Specifies a list of variables that mypy will treat as
347353
compile-time constants that are always true.
348-
349-
``always_false`` (comma-separated list of strings)
350-
Specifies a list of variables that mypy will treat as
354+
355+
``always_false`` (comma-separated list of strings)
356+
Specifies a list of variables that mypy will treat as
351357
compile-time constants that are always false.
352358

353359

@@ -357,10 +363,10 @@ Incremental mode
357363
For more information, see the :ref:`incremental mode <incremental>`
358364
section of the command line docs.
359365

360-
``incremental`` (bool, default True)
366+
``incremental`` (bool, default True)
361367
Enables :ref:`incremental mode <incremental>`.
362368

363-
``cache_dir`` (string, default ``.mypy_cache``)
369+
``cache_dir`` (string, default ``.mypy_cache``)
364370
Specifies the location where mypy stores incremental cache info.
365371
Note that the cache is only read when incremental mode is enabled
366372
but is always written to, unless the value is set to ``/dev/nul``
@@ -378,7 +384,7 @@ Configuring error messages
378384
For more information, see the :ref:`configuring error messages <configuring-error-messages>`
379385
section of the command line docs.
380386

381-
``show_error_context`` (bool, default False)
387+
``show_error_context`` (bool, default False)
382388
Prefixes each error with the relevant context.
383389

384390
``show_column_numbers`` (bool, default False)
@@ -397,7 +403,7 @@ section of the command line docs.
397403
``show_traceback`` (bool, default False)
398404
Shows traceback on fatal error.
399405

400-
``custom_typing_module`` (string)
406+
``custom_typing_module`` (string)
401407
Specifies a custom module to use as a substitute for the ``typing`` module.
402408

403409
``custom_typeshed_dir`` (string)
@@ -425,5 +431,3 @@ Miscellaneous
425431

426432
``verbosity`` (integer, default 0)
427433
Controls how much debug output will be generated. Higher numbers are more verbose.
428-
429-

0 commit comments

Comments
 (0)