diff --git a/frictionless/metadata.py b/frictionless/metadata.py index e8bd379058..45cc7cbc6f 100644 --- a/frictionless/metadata.py +++ b/frictionless/metadata.py @@ -163,7 +163,11 @@ def from_descriptor( # TODO: remove in next version # Transform with a base class in case the type is not available cls.metadata_transform(descriptor) - Class = cls.metadata_select_class(descriptor.get("type")) + type = descriptor.get("type") + class_type = vars(cls).get("type") + if isinstance(class_type, str): + type = class_type + Class = cls.metadata_select_class(type) Error = Class.metadata_Error or platform.frictionless_errors.MetadataError Class.metadata_transform(descriptor) errors = list(Class.metadata_validate(descriptor)) diff --git a/tests/resource/test_datatype.py b/tests/resource/test_datatype.py index e2672c8da8..4e1d1546b9 100644 --- a/tests/resource/test_datatype.py +++ b/tests/resource/test_datatype.py @@ -128,3 +128,37 @@ def test_resource_datatype_package_with_packagify(source): resource = Resource(source, packagify=True) assert resource.datatype == "package" assert isinstance(resource, resources.PackageResource) + + +# Bugs + +DESCRIPTOR = { + "type": "json", + "name": "data", + "path": "data.json", + "scheme": "file", + "format": "json", + "mediatype": "text/json", + "encoding": "utf-8", +} + + +def test_resource_constructor_with_forced_datatype(): + resource = Resource(path="data.json", datatype="table") + assert resource.type == "table" + assert resource.datatype == "table" + assert isinstance(resource, resources.TableResource) + + +def test_resource_from_descriptor_with_forced_datatype(): + resource = Resource.from_descriptor(DESCRIPTOR, datatype="table") + assert resource.type == "table" + assert resource.datatype == "table" + assert isinstance(resource, resources.TableResource) + + +def test_resource_from_descriptor_with_class_datatype(): + resource = resources.TableResource.from_descriptor(DESCRIPTOR) + assert resource.type == "table" + assert resource.datatype == "table" + assert isinstance(resource, resources.TableResource)