Skip to content

Commit e285203

Browse files
authored
[docs] add more info to allow_redefinition (#11951)
1 parent a825c15 commit e285203

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

docs/source/command_line.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,16 @@ of the above sections.
524524
# 'items' has type list[str]
525525
items = [item.split() for item in items]
526526
# 'items' now has type list[list[str]]
527-
...
527+
528+
The variable must be used before it can be redefined:
529+
530+
.. code-block:: python
531+
532+
def process(items: list[str]) -> None:
533+
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet
534+
print(items)
535+
items = "100" # valid, items now has type str
536+
items = int(items) # valid, items now has type int
528537
529538
.. option:: --local-partial-types
530539

docs/source/config_file.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,24 @@ section of the command line docs.
615615

616616
Allows variables to be redefined with an arbitrary type, as long as the redefinition
617617
is in the same block and nesting level as the original definition.
618+
Example where this can be useful:
619+
620+
.. code-block:: python
621+
622+
def process(items: list[str]) -> None:
623+
# 'items' has type list[str]
624+
items = [item.split() for item in items]
625+
# 'items' now has type list[list[str]]
626+
627+
The variable must be used before it can be redefined:
628+
629+
.. code-block:: python
630+
631+
def process(items: list[str]) -> None:
632+
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet
633+
print(items)
634+
items = "100" # valid, items now has type str
635+
items = int(items) # valid, items now has type int
618636
619637
.. confval:: local_partial_types
620638

0 commit comments

Comments
 (0)