classification
Title: Document that importlib.import_module accepts names that are not valid Python syntax
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, docs@python, eric.smith, eric.snow, gvanrossum, matrixise, ncoghlan, serhiy.storchaka
Priority: low Keywords:

Created on 2018-11-30 08:30 by matrixise, last changed 2019-02-05 11:55 by matrixise. This issue is now closed.

Messages (7)
msg330761 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-11-30 08:30
maybe related to this issue: https://bugs.python.org/issue18831

we can't import a module where the name contains '-', for example

from my-module import my_function

but with importlib.import_module

we can import this module.

import_module does not respect the python syntax for the name of the module, we should avoid that.

1. Add a DeprecationWarning for 3.8+
2. in 3.10, remove this DeprecationWarning and remove the support of the '-' in importlib.import_module
msg330763 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-30 09:09
'-' is not special. The import statement requires the module name been an identifier. This is just a requirement of Python syntax. But when pass the module name as a string, there is no such limitation in CPython. This is like an attribute name or keyword argument name should be identifiers in expressions, but you can pass an arbitrary string to getattr() and use a dict with arbitrary string keys as kwargs. Currently non-identifier strings are accepted, but this is an implementation detail, not a part of the language.

There are reasons against adding additional checks for attribute names and keyword argument names. This will slow down execution of all code. This is less critical for importing, because it is slower than attribute access and function call. But on other side, the benefit of adding this check is not obvious.

It may be worth to discuss this on mailing lists. First read previous discussions about attribute names and keyword argument names.
msg330766 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-11-30 10:02
hi @serhiy,

I understand but today, by error my CI has started to run the tests of an external python package and in the tests, there was a lot of `from my-module import XYZ` but in the django project, they load this module via importlib.import_module or builtins.__import__.

For the developer of this external module, he didn't execute the tests before, and for him, there was no problem, because django can load it.
msg330789 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-11-30 15:10
I dynamically load a lot of modules whose names contain hyphens, or are otherwise non-identifiers (like 3rdparty.py). The suggested change would break a lot of working code.

The only thing I can see being possible is to add a warning that no one would likely ever see. So I think we should take no action here.
msg330797 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-11-30 16:01
Do not change this. I'd rather see it documented that importlib can import any name as long as it doesn't contain a dot, slash or backslash.

(Clearly the fact that Django loads it is an additional argument that this should be supported, not forbidden.)

It's the same as for getattr etc.
msg330821 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-11-30 19:55
I agree that this shouldn't change and at best can be a documentation update to mention the fact that importlib.import_module() doesn't restrict itself to valid syntax names on purpose.
msg334863 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-05 11:55
I close this issue, thank you for your feedback.
History
Date User Action Args
2019-02-05 11:55:39matrixisesetstatus: open -> closed
resolution: rejected
messages: + msg334863

stage: resolved
2018-11-30 19:55:18brett.cannonsetpriority: normal -> low

assignee: docs@python
components: + Documentation
title: avoid '-' in importlib.import_module and builtins.__import__ -> Document that importlib.import_module accepts names that are not valid Python syntax
nosy: + docs@python

messages: + msg330821
2018-11-30 16:01:36gvanrossumsetmessages: + msg330797
2018-11-30 15:10:12eric.smithsetversions: - Python 3.5, Python 3.6, Python 3.7
nosy: + eric.smith

messages: + msg330789

type: enhancement
2018-11-30 10:02:20matrixisesetmessages: + msg330766
2018-11-30 09:09:23serhiy.storchakasetnosy: + eric.snow, brett.cannon, serhiy.storchaka, ncoghlan, gvanrossum
messages: + msg330763
2018-11-30 08:30:51matrixisecreate