File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -524,7 +524,16 @@ of the above sections.
524
524
# 'items' has type list[str]
525
525
items = [item.split() for item in items]
526
526
# '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
528
537
529
538
.. option :: --local-partial-types
530
539
Original file line number Diff line number Diff line change @@ -615,6 +615,24 @@ section of the command line docs.
615
615
616
616
Allows variables to be redefined with an arbitrary type, as long as the redefinition
617
617
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
618
636
619
637
.. confval :: local_partial_types
620
638
You can’t perform that action at this time.
0 commit comments