From 7d67aa1067896f55cbc72eb752885eac3a325e60 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Fri, 27 Feb 2026 21:36:31 +0000 Subject: [PATCH] Show an error when old and new redefinion are enabled in a file --- mypy/main.py | 7 +++++++ mypy/semanal.py | 11 +++++++++++ test-data/unit/check-flags.test | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/mypy/main.py b/mypy/main.py index bd04a427c4a23..bc20eb38ca2b7 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -104,6 +104,13 @@ def main( options, ) + if options.allow_redefinition_new and options.allow_redefinition_old: + fail( + "--allow-redefinition-old and --allow-redefinition-new should not be used together", + stderr, + options, + ) + if options.install_types and (stdout is not sys.stdout or stderr is not sys.stderr): # Since --install-types performs user input, we want regular stdout and stderr. fail("error: --install-types not supported in this mode of running mypy", stderr, options) diff --git a/mypy/semanal.py b/mypy/semanal.py index e2c073fd4dea8..75994c93b1ccb 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -713,6 +713,17 @@ def refresh_top_level(self, file_node: MypyFile) -> None: n.end_line = 1 n.end_column = 0 self.fail("--local-partial-types must be enabled if using --allow-redefinition-new", n) + if self.options.allow_redefinition_new and self.options.allow_redefinition_old: + n = TempNode(AnyType(TypeOfAny.special_form)) + n.line = 1 + n.column = 0 + n.end_line = 1 + n.end_column = 0 + self.fail( + "--allow-redefinition-old and --allow-redefinition-new" + " should not be used together", + n, + ) self.recurse_into_functions = False self.add_implicit_module_attrs(file_node) for d in file_node.defs: diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 5e9c7f4be8dcd..b5f8f641b539b 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2737,3 +2737,9 @@ def f() -> None: [file mypy.ini] \[mypy] disallow_redefinition = true + +[case testOnlyOneRedefinitionModeAllowed] +# flags: --allow-redefinition --allow-redefinition-new --local-partial-types +x = 1 +[out] +main:1: error: --allow-redefinition-old and --allow-redefinition-new should not be used together