@@ -593,12 +593,58 @@ of the above sections.
593593 This flag causes mypy to suppress errors caused by not being able to fully
594594 infer the types of global and class variables.
595595
596- .. option :: --allow-redefinition
596+ .. option :: --allow-redefinition-new
597597
598598 By default, mypy won't allow a variable to be redefined with an
599- unrelated type. This flag enables redefinition of a variable with an
599+ unrelated type. This *experimental * flag enables the redefinition of
600+ unannotated variables with an arbitrary type. You will also need to enable
601+ :option: `--local-partial-types <mypy --local-partial-types> `.
602+ Example:
603+
604+ .. code-block :: python
605+
606+ def maybe_convert (n : int , b : bool ) -> int | str :
607+ if b:
608+ x = str (n) # Assign "str"
609+ else :
610+ x = n # Assign "int"
611+ # Type of "x" is "int | str" here.
612+ return x
613+
614+ Without the new flag, mypy only supports inferring optional types
615+ (``X | None ``) from multiple assignments. With this option enabled,
616+ mypy can infer arbitrary union types.
617+
618+ This also enables an unannotated variable to have different types in different
619+ code locations:
620+
621+ .. code-block :: python
622+
623+ if check():
624+ for x in range (n):
625+ # Type of "x" is "int" here.
626+ ...
627+ else :
628+ for x in [' a' , ' b' ]:
629+ # Type of "x" is "str" here.
630+ ...
631+
632+ Note: We are planning to turn this flag on by default in a future mypy
633+ release, along with :option: `--local-partial-types <mypy --local-partial-types> `.
634+ The feature is still experimental, and the semantics may still change.
635+
636+ .. option :: --allow-redefinition
637+
638+ This is an older variant of
639+ :option: `--allow-redefinition-new <mypy --allow-redefinition-new> `.
640+ This flag enables redefinition of a variable with an
600641 arbitrary type *in some contexts *: only redefinitions within the
601642 same block and nesting depth as the original definition are allowed.
643+
644+ We have no plans to remove this flag, but we expect that
645+ :option: `--allow-redefinition-new <mypy --allow-redefinition-new> `
646+ will replace this flag for new use cases eventually.
647+
602648 Example where this can be useful:
603649
604650 .. code-block :: python
0 commit comments