Skip to content
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

[3.12] gh-71261: Add paragraph on shadowing submodules with star imports (GH-107004) #107100

Merged
merged 1 commit into from
Jul 23, 2023
Merged
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
16 changes: 16 additions & 0 deletions Doc/tutorial/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@

.. index:: triple: module; search; path

When a module named :mod:`spam` is imported, the interpreter first searches for

Check warning on line 186 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: spam
a built-in module with that name. These module names are listed in
:data:`sys.builtin_module_names`. If not found, it then searches for a file
named :file:`spam.py` in a list of directories given by the variable
Expand All @@ -191,7 +191,7 @@

* The directory containing the input script (or the current directory when no
file is specified).
* :envvar:`PYTHONPATH` (a list of directory names, with the same syntax as the

Check warning on line 194 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

'envvar' reference target not found: PATH
shell variable :envvar:`PATH`).
* The installation-dependent default (by convention including a
``site-packages`` directory, handled by the :mod:`site` module).
Expand Down Expand Up @@ -388,7 +388,7 @@
Packages
========

Packages are a way of structuring Python's module namespace by using "dotted

Check warning on line 391 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: A.B
module names". For example, the module name :mod:`A.B` designates a submodule
named ``B`` in a package named ``A``. Just like the use of modules saves the
authors of different modules from having to worry about each other's global
Expand Down Expand Up @@ -448,7 +448,7 @@

import sound.effects.echo

This loads the submodule :mod:`sound.effects.echo`. It must be referenced with

Check warning on line 451 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: sound.effects.echo
its full name. ::

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
Expand All @@ -457,7 +457,7 @@

from sound.effects import echo

This also loads the submodule :mod:`echo`, and makes it available without its

Check warning on line 460 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: echo
package prefix, so it can be used as follows::

echo.echofilter(input, output, delay=0.7, atten=4)
Expand All @@ -466,7 +466,7 @@

from sound.effects.echo import echofilter

Again, this loads the submodule :mod:`echo`, but this makes its function

Check warning on line 469 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: echo

Check warning on line 469 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:func reference target not found: echofilter
:func:`echofilter` directly available::

echofilter(input, output, delay=0.7, atten=4)
Expand Down Expand Up @@ -509,10 +509,26 @@

__all__ = ["echo", "surround", "reverse"]

This would mean that ``from sound.effects import *`` would import the three

Check warning on line 512 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: sound.effects
named submodules of the :mod:`sound.effects` package.

Be aware that submodules might become shadowed by locally defined names. For
example, if you added a ``reverse`` function to the
:file:`sound/effects/__init__.py` file, the ``from sound.effects import *``
would only import the two submodules ``echo`` and ``surround``, but *not* the
``reverse`` submodule, because it is shadowed by the locally defined
``reverse`` function::

__all__ = [
"echo", # refers to the 'echo.py' file
"surround", # refers to the 'surround.py' file
"reverse", # !!! refers to the 'reverse' function now !!!
]

def reverse(msg: str): # <-- this name shadows the 'reverse.py' submodule
return msg[::-1] # in the case of a 'from sound.effects import *'

If ``__all__`` is not defined, the statement ``from sound.effects import *``

Check warning on line 531 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: sound.effects

Check warning on line 531 in Doc/tutorial/modules.rst

View workflow job for this annotation

GitHub Actions / Docs

py:mod reference target not found: sound.effects
does *not* import all submodules from the package :mod:`sound.effects` into the
current namespace; it only ensures that the package :mod:`sound.effects` has
been imported (possibly running any initialization code in :file:`__init__.py`)
Expand Down