Add missing module __spec__ (#13321)

* added missing __spec__ to _LazyModule

* test __spec__ is not None after module import

* changed module_spec arg to be optional in _LazyModule

* fix style issue

* added module spec test to test_file_utils
This commit is contained in:
Laura Hanu
2021-08-30 17:39:05 +01:00
committed by GitHub
parent 4ebe798ff2
commit 35236b870e
3 changed files with 14 additions and 2 deletions

View File

@@ -3291,7 +3291,11 @@ else:
import sys
sys.modules[__name__] = _LazyModule(
__name__, globals()["__file__"], _import_structure, extra_objects={"__version__": __version__}
__name__,
globals()["__file__"],
_import_structure,
module_spec=__spec__,
extra_objects={"__version__": __version__},
)

View File

@@ -1997,7 +1997,7 @@ class _LazyModule(ModuleType):
# Very heavily inspired by optuna.integration._IntegrationModule
# https://github.com/optuna/optuna/blob/master/optuna/integration/__init__.py
def __init__(self, name, module_file, import_structure, extra_objects=None):
def __init__(self, name, module_file, import_structure, module_spec=None, extra_objects=None):
super().__init__(name)
self._modules = set(import_structure.keys())
self._class_to_module = {}
@@ -2007,6 +2007,7 @@ class _LazyModule(ModuleType):
# Needed for autocompletion in an IDE
self.__all__ = list(import_structure.keys()) + sum(import_structure.values(), [])
self.__file__ = module_file
self.__spec__ = module_spec
self.__path__ = [os.path.dirname(module_file)]
self._objects = {} if extra_objects is None else extra_objects
self._name = name

View File

@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import unittest
import requests
import transformers
# Try to import everything from transformers to ensure every object can be loaded.
from transformers import * # noqa F406
@@ -38,6 +40,11 @@ PINNED_SHA256 = "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d
# Sha-256 of pytorch_model.bin on the top of `main`, for checking purposes
def test_module_spec():
assert transformers.__spec__ is not None
assert importlib.util.find_spec("transformers") is not None
class GetFromCacheTests(unittest.TestCase):
def test_bogus_url(self):
# This lets us simulate no connection