Skip to content

Commit ee384da

Browse files
arbitrary_subfolders: Only print deprecation warning once for aliased imports.
1 parent a9d9863 commit ee384da

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ def deprecated_import_alias(fullname):
166166
absolute import path. Use by calling in the module where the class used to be:
167167
ClassName = deprecated_import_alias("new.path.to.ClassName")"""
168168
calling_module_name = inspect.getmodule(inspect.stack()[1][0]).__name__
169+
cls = []
169170
def wrapper(*args, **kwargs):
170-
cls = _import_class_by_fullname(fullname)
171-
shortname = fullname.split('.')[-1]
172-
newmodule = '.'.join(fullname.split('.')[:-1])
173-
msg = """Importing %s from %s is deprecated, please instead import it from %s.
174-
Importing anyway for backward compatibility, but this may cause some
175-
unexpected behaviour."""
176-
msg = dedent(msg) % (shortname, calling_module_name, newmodule)
177-
warnings.warn(msg, stacklevel=2)
178-
return cls(*args, **kwargs)
171+
if not cls:
172+
cls.append(_import_class_by_fullname(fullname))
173+
shortname = fullname.split('.')[-1]
174+
newmodule = '.'.join(fullname.split('.')[:-1])
175+
msg = """Importing %s from %s is deprecated, please instead import it from
176+
%s. Importing anyway for backward compatibility, but this may cause some
177+
unexpected behaviour."""
178+
msg = dedent(msg) % (shortname, calling_module_name, newmodule)
179+
warnings.warn(msg, stacklevel=2)
180+
return cls[0](*args, **kwargs)
179181
return wrapper
180182

181183

0 commit comments

Comments
 (0)