Skip to content

Commit e7e5a09

Browse files
authored
Remove --force-union-syntax flag (python#20405)
This PR removed the `--force-union-syntax` flag. It also removes the temporary `--overwrite-union-syntax` flag which was added to be able to update the mypy test output.
1 parent a08b623 commit e7e5a09

21 files changed

Lines changed: 22 additions & 115 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Support for this will be dropped in the first half of 2026!
1111

1212
Contributed by Marc Mueller (PR [20156](https://github.com/python/mypy/pull/20156)).
1313

14+
### Removed Flag: `--force-union-syntax`
15+
16+
Mypy only supports Python 3.10+. Removed the `--force-union-syntax` flag as it's no longer necessary.
17+
18+
Contributed by Marc Mueller (PR [20405](https://github.com/python/mypy/pull/20405))
19+
1420
## Mypy 1.19
1521

1622
We’ve just uploaded mypy 1.19.0 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)).

docs/source/command_line.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,6 @@ in error messages.
958958
useful or they may be overly noisy. If ``N`` is negative, there is
959959
no limit. The default limit is -1.
960960

961-
.. option:: --force-union-syntax
962-
963-
Always use ``Union[]`` and ``Optional[]`` for union types
964-
in error messages (instead of the ``|`` operator),
965-
even on Python 3.10+.
966-
967961

968962
.. _incremental:
969963

docs/source/config_file.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -930,15 +930,6 @@ These options may only be set in the global section (``[mypy]``).
930930

931931
Show absolute paths to files.
932932

933-
.. confval:: force_union_syntax
934-
935-
:type: boolean
936-
:default: False
937-
938-
Always use ``Union[]`` and ``Optional[]`` for union types
939-
in error messages (instead of the ``|`` operator),
940-
even on Python 3.10+.
941-
942933
Incremental mode
943934
****************
944935

mypy/main.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -820,14 +820,6 @@ def add_invertible_flag(
820820
"--force-uppercase-builtins", default=False, help=argparse.SUPPRESS, group=none_group
821821
)
822822

823-
add_invertible_flag(
824-
"--force-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
825-
)
826-
# For internal use only! Will be removed once Mypy drops support for Python 3.9.
827-
add_invertible_flag(
828-
"--overwrite-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
829-
)
830-
831823
lint_group = parser.add_argument_group(
832824
title="Configuring warnings",
833825
description="Detect code that is sound but redundant or problematic.",

mypy/messages.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,10 +1813,7 @@ def need_annotation_for_var(
18131813
type_dec = "<type>"
18141814
if not node.type.type:
18151815
# partial None
1816-
if options.use_or_syntax():
1817-
recommended_type = f"{type_dec} | None"
1818-
else:
1819-
recommended_type = f"Optional[{type_dec}]"
1816+
recommended_type = f"{type_dec} | None"
18201817
elif node.type.type.fullname in reverse_builtin_aliases:
18211818
# partial types other than partial None
18221819
name = node.type.type.fullname.partition(".")[2]
@@ -2713,17 +2710,9 @@ def format_literal_value(typ: LiteralType) -> str:
27132710
)
27142711

27152712
if len(union_items) == 1 and isinstance(get_proper_type(union_items[0]), NoneType):
2716-
return (
2717-
f"{literal_str} | None"
2718-
if options.use_or_syntax()
2719-
else f"Optional[{literal_str}]"
2720-
)
2713+
return f"{literal_str} | None"
27212714
elif union_items:
2722-
return (
2723-
f"{literal_str} | {format_union(union_items)}"
2724-
if options.use_or_syntax()
2725-
else f"Union[{', '.join(format_union_items(union_items))}, {literal_str}]"
2726-
)
2715+
return f"{literal_str} | {format_union(union_items)}"
27272716
else:
27282717
return literal_str
27292718
else:
@@ -2734,17 +2723,9 @@ def format_literal_value(typ: LiteralType) -> str:
27342723
)
27352724
if print_as_optional:
27362725
rest = [t for t in typ.items if not isinstance(get_proper_type(t), NoneType)]
2737-
return (
2738-
f"{format(rest[0])} | None"
2739-
if options.use_or_syntax()
2740-
else f"Optional[{format(rest[0])}]"
2741-
)
2726+
return f"{format(rest[0])} | None"
27422727
else:
2743-
s = (
2744-
format_union(typ.items)
2745-
if options.use_or_syntax()
2746-
else f"Union[{', '.join(format_union_items(typ.items))}]"
2747-
)
2728+
s = format_union(typ.items)
27482729
return s
27492730
elif isinstance(typ, NoneType):
27502731
return "None"

mypy/options.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,6 @@ def __init__(self) -> None:
412412
self.disable_memoryview_promotion = False
413413
# Deprecated, Mypy only supports Python 3.9+
414414
self.force_uppercase_builtins = False
415-
self.force_union_syntax = False
416-
# Mypy internal use only! Set during test run.
417-
self.overwrite_union_syntax = False
418415

419416
# Sets custom output format
420417
self.output: str | None = None
@@ -433,11 +430,6 @@ def use_lowercase_names(self) -> bool:
433430
)
434431
return True
435432

436-
def use_or_syntax(self) -> bool:
437-
if self.python_version >= (3, 10):
438-
return not self.force_union_syntax
439-
return self.overwrite_union_syntax
440-
441433
def use_star_unpack(self) -> bool:
442434
return self.python_version >= (3, 11)
443435

mypy/suggestions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def visit_typeddict_type(self, t: TypedDictType) -> str:
891891
def visit_union_type(self, t: UnionType) -> str:
892892
if len(t.items) == 2 and is_overlapping_none(t):
893893
s = remove_optional(t).accept(self)
894-
return f"{s} | None" if self.options.use_or_syntax() else f"Optional[{s}]"
894+
return f"{s} | None"
895895
else:
896896
return super().visit_union_type(t)
897897

mypy/test/helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ def parse_options(
357357
options = Options()
358358
options.error_summary = False
359359
options.hide_error_codes = True
360-
options.overwrite_union_syntax = True
361360

362361
# Allow custom python version to override testfile_pyversion.
363362
if all(flag.split("=")[0] != "--python-version" for flag in flag_list):

mypy/test/testcheck.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def run_case_once(
136136
options = parse_options(original_program_text, testcase, incremental_step)
137137
options.use_builtins_fixtures = True
138138
options.show_traceback = True
139-
options.overwrite_union_syntax = True
140139

141140
if options.num_workers:
142141
options.fixed_format_cache = True

mypy/test/testcmdline.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
6060
args = parse_args(testcase.input[0])
6161
custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None
6262
args.append("--show-traceback")
63-
args.append("--overwrite-union-syntax")
6463
if "--error-summary" not in args:
6564
args.append("--no-error-summary")
6665
if "--show-error-codes" not in args:

0 commit comments

Comments
 (0)