From 91d2d8c5a9132b1cb303c0fdcb3d2bd08d0dd83b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 23 May 2024 22:30:20 -0700 Subject: [PATCH] gh-119443: Turn off from __future__ import annotations in REPL --- Lib/_pyrepl/simple_interact.py | 2 +- Lib/test/test_pyrepl/test_interact.py | 9 +++++++++ .../2024-05-23-22-29-59.gh-issue-119443.KAGz6S.rst | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-05-23-22-29-59.gh-issue-119443.KAGz6S.rst diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 8ab4dab757685e..3dfb1d7ad736e3 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -95,7 +95,7 @@ def runsource(self, source, filename="", symbol="single"): the_symbol = symbol if stmt is last_stmt else "exec" item = wrapper([stmt]) try: - code = compile(item, filename, the_symbol) + code = compile(item, filename, the_symbol, dont_inherit=True) except (OverflowError, ValueError): self.showsyntaxerror(filename) return False diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py index 10e34045bcf92d..6ebd51fe14dd62 100644 --- a/Lib/test/test_pyrepl/test_interact.py +++ b/Lib/test/test_pyrepl/test_interact.py @@ -94,3 +94,12 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self): with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror: console.runsource(source) mock_showsyntaxerror.assert_called_once() + + def test_no_active_future(self): + console = InteractiveColoredConsole() + source = "x: int = 1; print(__annotations__)" + f = io.StringIO() + with contextlib.redirect_stdout(f): + result = console.runsource(source) + self.assertFalse(result) + self.assertEqual(f.getvalue(), "{'x': }\n") diff --git a/Misc/NEWS.d/next/Library/2024-05-23-22-29-59.gh-issue-119443.KAGz6S.rst b/Misc/NEWS.d/next/Library/2024-05-23-22-29-59.gh-issue-119443.KAGz6S.rst new file mode 100644 index 00000000000000..4470c566a37d88 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-05-23-22-29-59.gh-issue-119443.KAGz6S.rst @@ -0,0 +1,2 @@ +The interactive REPL no longer runs with ``from __future__ import +annotations`` enabled. Patch by Jelle Zijlstra.