Skip to content

[3.7] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913)#15925

Merged
miss-islington merged 1 commit into
python:3.7from
miss-islington:backport-0a6693a-3.7
Sep 11, 2019
Merged

[3.7] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913)#15925
miss-islington merged 1 commit into
python:3.7from
miss-islington:backport-0a6693a-3.7

Conversation

@miss-islington

@miss-islington miss-islington commented Sep 11, 2019

Copy link
Copy Markdown
Contributor

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If package (and spec.parent) are missing then
import uses name, truncating the last segment if
the module is a submodule rather than a package init.py
(which it guesses from whether path is defined).

The name attempt should fail if there is no parent package (top level modules),
if name is 'main' (-m entry points), or both (scripts).
That is, if both name has no subcomponents and the module does not seem
to be a package init module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis benjimin@users.noreply.github.com
(cherry picked from commit 0a6693a)

Co-authored-by: Brett Cannon 54418+brettcannon@users.noreply.github.com

https://bugs.python.org/issue37409

Automerge-Triggered-By: @brettcannon

…ythonGH-15913)

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If __package__ (and __spec__.parent) are missing then
import uses __name__, truncating the last segment if
the module is a submodule rather than a package __init__.py
(which it guesses from whether __path__ is defined).

The __name__ attempt should fail if there is no parent package (top level modules),
if __name__ is '__main__' (-m entry points), or both (scripts).
That is, if both __name__ has no subcomponents and the module does not seem
to be a package __init__ module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis <benjimin@users.noreply.github.com>
(cherry picked from commit 0a6693a)

Co-authored-by: Brett Cannon <54418+brettcannon@users.noreply.github.com>
@brettcannon brettcannon changed the title [3.7] [3.8] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913) [3.7] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913) Sep 11, 2019
@miss-islington

Copy link
Copy Markdown
Contributor Author

@benjimin and @brettcannon: Status check is done, and it's a success ✅ .

@miss-islington
miss-islington merged commit f3480ad into python:3.7 Sep 11, 2019
@miss-islington
miss-islington deleted the backport-0a6693a-3.7 branch September 11, 2019 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants