From cfa0f3e5d2015c044fa3674ddf264636ebeecdf4 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:46:43 +0800 Subject: [PATCH 1/7] Add dotted import check in `future.c` --- Python/future.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/future.c b/Python/future.c index 399345bd8fcbd9..8d94d515605dcd 100644 --- a/Python/future.c +++ b/Python/future.c @@ -77,7 +77,7 @@ future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename) * are another future statement and a doc string. */ - if (s->kind == ImportFrom_kind) { + if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) { identifier modname = s->v.ImportFrom.module; if (modname && _PyUnicode_EqualToASCIIString(modname, "__future__")) { From 042554a292cbc10d6c91a4dcf2d8667ef958fff5 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:47:45 +0800 Subject: [PATCH 2/7] Add dotted import check in `compile.c` --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index 4e94f9297a32d1..feedd988834397 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3849,7 +3849,7 @@ compiler_from_import(struct compiler *c, stmt_ty s) } if (location_is_after(LOC(s), c->c_future.ff_location) && - s->v.ImportFrom.module && + s->v.ImportFrom.module && s->v.ImportFrom.level == 0 && _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { Py_DECREF(names); From 536881e519c236126a0b9fa608c3e92538c0a192 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:54:33 +0800 Subject: [PATCH 3/7] Add tests for dotted imports of `__future__` --- Lib/test/test_future_stmt/test_future.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_future_stmt/test_future.py b/Lib/test/test_future_stmt/test_future.py index 2c8ceb664cb362..1ea0891149f60e 100644 --- a/Lib/test/test_future_stmt/test_future.py +++ b/Lib/test/test_future_stmt/test_future.py @@ -203,6 +203,20 @@ def test_syntactical_future_repl(self): out = kill_python(p) self.assertNotIn(b'SyntaxError: invalid syntax', out) + def test_future_dotted_import(self): + with self.assertRaises(ImportError): + from .__future__ import annotations + + with self.assertRaises(ImportError): + from __future__ import print_function + from ...__future__ import annotations + + code = """ + from .__future__ import nested_scopes + from __future__ import barry_as_FLUFL + """ + self.assertSyntaxError(code, lineno=2) + class AnnotationsFutureTestCase(unittest.TestCase): template = dedent( """ From 46c8290e40b00290dca292680966e11797254d9a Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:04:33 +0800 Subject: [PATCH 4/7] Fix dotted import tests --- Lib/test/test_future_stmt/test_future.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_future_stmt/test_future.py b/Lib/test/test_future_stmt/test_future.py index 1ea0891149f60e..69ae58b0fbcae3 100644 --- a/Lib/test/test_future_stmt/test_future.py +++ b/Lib/test/test_future_stmt/test_future.py @@ -205,11 +205,16 @@ def test_syntactical_future_repl(self): def test_future_dotted_import(self): with self.assertRaises(ImportError): - from .__future__ import annotations + exec("from .__future__ import spam") - with self.assertRaises(ImportError): + code = dedent( + """ from __future__ import print_function - from ...__future__ import annotations + from ...__future__ import ham + """ + ) + with self.assertRaises(ImportError): + exec(code) code = """ from .__future__ import nested_scopes From 62d8487905435b391619457d96fa4744a2b607da Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:48:33 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-04-25-11-48-28.gh-issue-118216.SVg700.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst new file mode 100644 index 00000000000000..6b3fdff640c49c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst @@ -0,0 +1 @@ +Don't consider `__future__` imports with dots before the module name. From 9b908eec9fd8fe27e8e6fa381873f06dbda9372a Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 25 Apr 2024 21:36:33 +0800 Subject: [PATCH 6/7] Fix Markdown --- .../2024-04-25-11-48-28.gh-issue-118216.SVg700.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst index 6b3fdff640c49c..937cdffefda6f1 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-25-11-48-28.gh-issue-118216.SVg700.rst @@ -1 +1 @@ -Don't consider `__future__` imports with dots before the module name. +Don't consider :mod:`__future__` imports with dots before the module name. From 019ef6ff2716cc1c78466e686ecf80a60146608c Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Thu, 2 May 2024 14:06:54 +0800 Subject: [PATCH 7/7] Add entry to What's New in 3.13 --- Doc/whatsnew/3.13.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 52fb9752cf4132..3ccf17be9796d5 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -288,6 +288,10 @@ Other Language Changes class scopes are not inlined into their parent scope. (Contributed by Jelle Zijlstra in :gh:`109118` and :gh:`118160`.) +* ``from __future__ import ...`` statements are now just normal + relative imports if dots are present before the module name. + (Contributed by Jeremiah Gabriel Pascual in :gh:`118216`.) + New Modules ===========