Skip to content

gh-118216: Don't consider dotted __future__ imports #118267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_future_stmt/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ 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):
exec("from .__future__ import spam")

code = dedent(
"""
from __future__ import print_function
from ...__future__ import ham
"""
)
with self.assertRaises(ImportError):
exec(code)

code = """
from .__future__ import nested_scopes
from __future__ import barry_as_FLUFL
"""
self.assertSyntaxError(code, lineno=2)

class AnnotationsFutureTestCase(unittest.TestCase):
template = dedent(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't consider :mod:`__future__` imports with dots before the module name.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Python/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__")) {
Expand Down
Loading