Skip to content

Commit c52318b

Browse files
committed
Add hidden --overwrite-union-syntax option
1 parent c1fb129 commit c52318b

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

mypy/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ def add_invertible_flag(
815815
add_invertible_flag(
816816
"--force-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
817817
)
818+
# For internal use only! Will be removed once Mypy drops support for Python 3.9.
819+
add_invertible_flag(
820+
"--overwrite-union-syntax", default=False, help=argparse.SUPPRESS, group=none_group
821+
)
818822

819823
lint_group = parser.add_argument_group(
820824
title="Configuring warnings",

mypy/options.py

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

416418
# Sets custom output format
417419
self.output: str | None = None
@@ -433,7 +435,7 @@ def use_lowercase_names(self) -> bool:
433435
def use_or_syntax(self) -> bool:
434436
if self.python_version >= (3, 10):
435437
return not self.force_union_syntax
436-
return False
438+
return False or self.overwrite_union_syntax
437439

438440
def use_star_unpack(self) -> bool:
439441
return self.python_version >= (3, 11)

mypy/test/testcmdline.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
5555
args = parse_args(testcase.input[0])
5656
custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None
5757
args.append("--show-traceback")
58+
args.append("--overwrite-union-syntax")
5859
if "--error-summary" not in args:
5960
args.append("--no-error-summary")
6061
if "--show-error-codes" not in args:
6162
args.append("--hide-error-codes")
6263
if "--disallow-empty-bodies" not in args:
6364
args.append("--allow-empty-bodies")
64-
if "--no-force-union-syntax" not in args:
65-
args.append("--force-union-syntax")
6665
# Type check the program.
6766
fixed = [python3_path, "-m", "mypy"]
6867
env = os.environ.copy()

mypy/test/testpythoneval.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
5252
"--no-error-summary",
5353
"--hide-error-codes",
5454
"--allow-empty-bodies",
55+
"--overwrite-union-syntax",
5556
"--test-env", # Speeds up some checks
5657
]
5758
interpreter = python3_path
@@ -71,9 +72,6 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
7172
return
7273
mypy_cmdline.extend(additional_flags)
7374

74-
if "--no-force-union-syntax" not in mypy_cmdline:
75-
mypy_cmdline.append("--force-union-syntax")
76-
7775
# Write the program to a file.
7876
program = "_" + testcase.name + ".py"
7977
program_path = os.path.join(test_temp_dir, program)

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,6 +3736,7 @@ class TypeStrVisitor(SyntheticTypeVisitor[str]):
37363736
Notes:
37373737
- Represent unbound types as Foo? or Foo?[...].
37383738
- Represent the NoneType type as None.
3739+
- Represent Union[x, y] as x | y
37393740
"""
37403741

37413742
def __init__(self, id_mapper: IdMapper | None = None, *, options: Options) -> None:

0 commit comments

Comments
 (0)