You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to be able to use a schema object to provide type hints for my settings object:
classMySchema(DynaconfBase):
name: strage: intsettings=Dynaconf()
settings.na# IDE autocomplete "name" and recognizes as string
Proposal
Fake the type of the Dynaconf object by pretending it is a Schema object.
To provide both Schema attributes and Dynaconf's usual methods, we could provide a base Schema to be inherited, which will act as a stub, providing only docstrings and type hinting to Dynaconf object methods.
pro:
we avoid the efforts of significantly changing how dynaconf works (backward-compatibility safety)
we avoid the complexity a big change would bring (we have limited maintainability power).
cons:
Not very elegant (but it's not unpractical either, I would argue).
fromdynaconfimportDynaconfBase, DynaconfclassMySchema(DynaconfBase):
name: strage: intsettings: MySchema=Dynaconf(...) # type: ignoresettings.set# shows annotations for dynaconf set methodsettings.na# suggests name: str
Recently, I've been working on adding type hints to a Flask config. I, independently, came up with essentially the same solution and would really like to see this functionality built into Dynaconf.
For reference, I got around the need for #type: ignore by casting it to my schema (after checking that all of the settings had the correct type, of course). In your example, it would look something like this:
If the schema was passed to Dynaconf as in the example: settings: MySchema = Dynaconf(schema=MySchema), the cast could happen internal to Dynaconf, which would be cleaner from the end user perspective.
That's nice, I didn't know about cast, thanks for sharing!
I did a small mock experiment by casting internally and it does seems to work.
I'll try to get some time to work on this.
Problem
I want to be able to use a schema object to provide type hints for my
settings
object:Proposal
Fake the type of the Dynaconf object by pretending it is a Schema object.
To provide both Schema attributes and Dynaconf's usual methods, we could provide a base Schema to be inherited, which will act as a stub, providing only docstrings and type hinting to Dynaconf object methods.
It could work along with this more recent Schema Validation proposal:
Alternatives
# type: ignore
, but I'm not sure what.Additional Context
I did some limited testing around that.
The text was updated successfully, but these errors were encountered: