https://dagster.io/ logo
#ask-community
Title
# ask-community
r

Riccardo Tesselli

04/12/2023, 1:20 PM
I have an old-style resource like this:
Copy code
class A:
    def __init__(self, secret: SecretStr):
        self.secret = secret

    ...

@resource(config_schema={"secret": str})
def a(init_context: InitResourceContext):
    return A(
        secret=SecretStr(init_context.resource_config["secret"])
    )
I would like to convert this to the new pythonic-style resources by doing this:
Copy code
class A(ConfigurableResource):
    secret: Any

    @root_validator()
    def to_secret_str(cls, values):
        values["secret"] = SecretStr(values["secret"])
        return values
This is the only way I’ve managed to make it work, even though ideally I’d prefer to do like:
Copy code
class A(ConfigurableResource):
    secret: SecretStr
but this fails because
SecretStr
is not a valid Dagster type. Is there another way to convert this resource into a Pythonic-style without using
Any
? I don’t like using
Any
because then my typing would be too generic and not useful at all
👀 2
b

ben

04/14/2023, 9:58 PM
Hi Riccardo, this is a reasonable ask - adding support for
SecretStr
to our list of improvements to make to the system
🎉 1
r

Riccardo Tesselli

04/18/2023, 8:22 AM
thanks for the answer! I really appreciate it. I have still one more question: so it makes sense to add support for
SecretStr
, but I wonder how many more such cases there will be and so it is virtually impossible to manually add support for all of them. There will be a way to let the programmer decide which types to use in its own resources?
b

ben

04/21/2023, 4:37 PM
Hi Riccardo - that’s right. We think of the resource class as one to model the config schema for the resource rather than all of the possible state fields that it may need to keep track of. In the case where you need to keep track of types which might not be serializable/representable in the config system, we’d suggest splitting out into two classes, one to model config and one to handle that state, e.g.
Copy code
class MyClient:
    """Client class with mutable state."""

    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password
        self._cache = {}

    ...


class MyClientResource(ConfigurableResource):
    username: str
    password: str

    def get_client(self):
        return MyClient(self.username, self.password)
2 Views