Tristan Schrader
07/12/2023, 1:09 AMdagster
. I’ve tried to use a Resource in my Asset, but I’m stuck with a dagster._core.errors.DagsterInvalidDefinitionError: Invalid type: dagster_type must be an instance of DagsterType or a Python type: got GoogleDriveClient.
error.
Here’s my minimal working example:
from dagster import asset, ConfigurableResource, Definitions
from googleapiclient.discovery import build
class GoogleDriveClient(ConfigurableResource):
def get_folder(self, name: str) -> str:
query = f"mimeType='application/vnd.google-apps.folder' and trashed=false and name='{name}'"
response = build("drive", "v3").files().list(q=query).execute()
return response.get("files", [])[0].get("id", "")
@asset
def get_folder(gdrive: GoogleDriveClient) -> str:
return gdrive.get_folder("My Folder")
defs = Definitions(assets=[get_folder], resources={"gdrive": GoogleDriveClient()})
Here’s what I would like to work:
from __future__ import annotations
from typing import TYPE_CHECKING
from dagster import asset, ConfigurableResource, Definitions
from googleapiclient.discovery import build
if TYPE_CHECKING:
from googleapiclient._apis.drive.v3.schemas import File
class GoogleDriveClient(ConfigurableResource):
def get_folder(self, name: str) -> File:
query = f"mimeType='application/vnd.google-apps.folder' and trashed=false and name='{name}'"
response = build("drive", "v3").files().list(q=query).execute()
return response.get("files", [])[0]
@asset
def get_folder(gdrive: GoogleDriveClient) -> File:
return gdrive.get_folder("My Folder")
defs = Definitions(assets=[get_folder], resources={"gdrive": GoogleDriveClient()})
This gives the error I mentioned above. I thought it might be because the File
type is not available at dagster
runtime because of the TYPE_CHECKING
conditional import. The problem is that when I remove the conditional import (I think it’s standard for mypy
these days), I get ModuleNotFoundError: No module named 'googleapiclient._apis'
. That’s because mypy
resolves the correct location in site-packages
where the stubs were downloaded (googleapiclient-stubs
), but I guess dagster
doesn’t.
Not really sure what to do… My understanding is the stubs I’ve downloaded are following all the standards. They work with mypy
at least. Is there some way to tell dagster
to find these correctly? Thanks in advance.Tristan Schrader
07/12/2023, 5:41 PMOdette Harary
07/13/2023, 2:47 PM