Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation on cast and default #1039

Open
q-wertz opened this issue Jan 10, 2024 · 3 comments
Open

Improve documentation on cast and default #1039

q-wertz opened this issue Jan 10, 2024 · 3 comments

Comments

@q-wertz
Copy link

q-wertz commented Jan 10, 2024

Currently it is not clear from the documentation when using default together with cast, whether

  1. the default value has to be given as string/int/etc. and the cast is done on it
  2. the default value has to be given in the correct type
  3. It doesn't matter

e.g.

import enum

import dynaconf as dc

class VisCheck(enum.StrEnum):
    NONE = enum.auto()
    A = enum.auto()
    B = enum.auto()


# Variant 1
dc.Validator(
    "VISIBILITY_CHECK",
    default="a",
    cast=VisCheck,
)

# Variant 2
dc.Validator(
    "VISIBILITY_CHECK",
    default=VisCheck.A,
    cast=VisCheck,
)

It would be nice to have a hint in the docs :)

EDIT: Wrong default type in Variant 2 (typo)

@pedro-psb
Copy link
Member

Hey @q-wertz , option 1 is correct, cast is called for defaults.
Agreed that this can be more clear in the docs!

@q-wertz
Copy link
Author

q-wertz commented Jan 15, 2024

Not sure if I oversee something, but it looks like both solutions work in my case.
When I do not have the value in my config file, the default VisCheck.A is in the settings. When I set it to an object with invalid type (e.g. np.random.default_rng()) it fails with

Generator(PCG64) at 0x7F64087CF840 is not a valid VisCheck

For me from "user" view it looks like both variants are equivalent 😅

@pedro-psb
Copy link
Member

Yeah, I guess they are equivalent because the casting function will usually bypass the value when called over an already casted value (like VisCheck(VisCheck('a'))).
We can say that it's usually equivalent, but I suggest describing that the cast is called for defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants