This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kaorihinata
Recipients brett.cannon, eric.smith, kaorihinata
Date 2021-03-17.00:26:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1615940773.95.0.0552359944569.issue43477@roundup.psfhosted.org>
In-reply-to
Content
Given the previous example, in test.py, replace:

```
print(test_module.test_submodule)
```

...with:

```
assert(not hasattr(test_module, "test_submodule"))
```

...because the issue is only the bottom half of `_find_and_load_unlocked`. Specifically, the chunk starting at line 1006:

```
    if parent:
        # Set the module as an attribute on its parent.
        parent_module = sys.modules[parent]
        child = name.rpartition('.')[2]
        try:
            setattr(parent_module, child, module)
        except AttributeError:
            msg = f"Cannot set an attribute on {parent!r} for child module {child!r}"
            _warnings.warn(msg, ImportWarning)
```

The issue with these lines is that nothing here was requested by the user, and the actions you mentioned (preventing redundant/duplicate imports) is not handled by anything in, or relating to this code (at least, not that I've seen.) The module and all dependencies would still be loaded into `sys.modules` despite this code. If the module has already been loaded, then we'll never make it past `_find_and_load` to `_find_and_load_unlocked` anyway, as `_NEEDS_LOADING` will no longer match.

Does that make more sense?
History
Date User Action Args
2021-03-17 00:26:14kaorihinatasetrecipients: + kaorihinata, brett.cannon, eric.smith
2021-03-17 00:26:13kaorihinatasetmessageid: <1615940773.95.0.0552359944569.issue43477@roundup.psfhosted.org>
2021-03-17 00:26:13kaorihinatalinkissue43477 messages
2021-03-17 00:26:12kaorihinatacreate