Don't duplicate the elements in dir (#14023)

This commit is contained in:
Sylvain Gugger 2021-10-15 20:09:54 -04:00 committed by GitHub
parent 84ad6af49a
commit 968ae57c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2123,7 +2123,13 @@ class _LazyModule(ModuleType):
# Needed for autocompletion in an IDE
def __dir__(self):
return super().__dir__() + self.__all__
result = super().__dir__()
# The elements of self.__all__ that are submodules may or may not be in the dir already, depending on whether
# they have been accessed or not. So we only add the elements of self.__all__ that are not already in the dir.
for attr in self.__all__:
if attr not in result:
result.append(attr)
return result
def __getattr__(self, name: str) -> Any:
if name in self._objects: