Skip to content

Commit 464934c

Browse files
committed
[docs] add more info to allow_redefinition
1 parent fd32852 commit 464934c

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

docs/source/command_line.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,6 @@ of the above sections.
516516
unrelated type. This flag enables redefinition of a variable with an
517517
arbitrary type *in some contexts*: only redefinitions within the
518518
same block and nesting depth as the original definition are allowed.
519-
520-
The expression for the new value must contain a reference to the variable.
521-
522519
Example where this can be useful:
523520

524521
.. code-block:: python
@@ -528,7 +525,15 @@ of the above sections.
528525
items = [item.split() for item in items]
529526
# 'items' now has type list[list[str]]
530527
531-
items = "mypy" # invalid redefinition to str because the expression doesn't contain a reference to 'items'
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 not has type str
536+
items = int(items) # valid, items not has type int
532537
533538
.. option:: --local-partial-types
534539

docs/source/config_file.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,7 @@ section of the command line docs.
613613

614614
Allows variables to be redefined with an arbitrary type, as long as the redefinition
615615
is in the same block and nesting level as the original definition.
616-
617-
The expression for the new value must contain a reference to the variable:
616+
Example where this can be useful:
618617

619618
.. code-block:: python
620619
@@ -623,7 +622,15 @@ section of the command line docs.
623622
items = [item.split() for item in items]
624623
# 'items' now has type list[list[str]]
625624
626-
items = "mypy" # invalid redefinition to str because the expression doesn't contain a reference to 'items'
625+
The variable must be used before it can be redefined.
626+
627+
.. code-block:: python
628+
629+
def process(items: list[str]) -> None:
630+
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet
631+
print(items)
632+
items = "100" # valid, items not has type str
633+
items = int(items) # valid, items not has type int
627634
628635
.. confval:: local_partial_types
629636

0 commit comments

Comments
 (0)