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:
@@ -3291,7 +3291,11 @@ else:
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
sys.modules[__name__] = _LazyModule(
|
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__},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1997,7 +1997,7 @@ class _LazyModule(ModuleType):
|
|||||||
|
|
||||||
# Very heavily inspired by optuna.integration._IntegrationModule
|
# Very heavily inspired by optuna.integration._IntegrationModule
|
||||||
# https://github.com/optuna/optuna/blob/master/optuna/integration/__init__.py
|
# 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)
|
super().__init__(name)
|
||||||
self._modules = set(import_structure.keys())
|
self._modules = set(import_structure.keys())
|
||||||
self._class_to_module = {}
|
self._class_to_module = {}
|
||||||
@@ -2007,6 +2007,7 @@ class _LazyModule(ModuleType):
|
|||||||
# Needed for autocompletion in an IDE
|
# Needed for autocompletion in an IDE
|
||||||
self.__all__ = list(import_structure.keys()) + sum(import_structure.values(), [])
|
self.__all__ = list(import_structure.keys()) + sum(import_structure.values(), [])
|
||||||
self.__file__ = module_file
|
self.__file__ = module_file
|
||||||
|
self.__spec__ = module_spec
|
||||||
self.__path__ = [os.path.dirname(module_file)]
|
self.__path__ = [os.path.dirname(module_file)]
|
||||||
self._objects = {} if extra_objects is None else extra_objects
|
self._objects = {} if extra_objects is None else extra_objects
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|||||||
@@ -12,9 +12,11 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import importlib
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import transformers
|
||||||
|
|
||||||
# Try to import everything from transformers to ensure every object can be loaded.
|
# Try to import everything from transformers to ensure every object can be loaded.
|
||||||
from transformers import * # noqa F406
|
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
|
# 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):
|
class GetFromCacheTests(unittest.TestCase):
|
||||||
def test_bogus_url(self):
|
def test_bogus_url(self):
|
||||||
# This lets us simulate no connection
|
# This lets us simulate no connection
|
||||||
|
|||||||
Reference in New Issue
Block a user