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

[RFC] Provide workaround for auto-complete w/ Schema #1082

Open
pedro-psb opened this issue Mar 22, 2024 · 2 comments
Open

[RFC] Provide workaround for auto-complete w/ Schema #1082

pedro-psb opened this issue Mar 22, 2024 · 2 comments
Labels
Not a Bug Not a Problem, expected behavior RFC
Milestone

Comments

@pedro-psb
Copy link
Member

Problem

I want to be able to use a schema object to provide type hints for my settings object:

class MySchema(DynaconfBase):
    name: str
    age: int

settings = 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).
from dynaconf import DynaconfBase, Dynaconf

class MySchema(DynaconfBase):
    name: str
    age: int
    
settings: MySchema = Dynaconf(...) # type: ignore
settings.set # shows annotations for dynaconf set method
settings.na # suggests name: str

It could work along with this more recent Schema Validation proposal:

# Not elegant, but practical and backward-compatible safe
settings: MySchema = Dynaconf(schema=MySchema) # type: ignore

Alternatives

  • This draft PR should provide that, but it falls into the complexity issue
  • Maybe there is another kind of trick we can do that won't require the # type: ignore, but I'm not sure what.

Additional Context

I did some limited testing around that.

  • Django setups might provide some trouble to do that.
  • We could try on more IDEs (experimented with Helix and Vscode).
@pedro-psb pedro-psb added Not a Bug Not a Problem, expected behavior RFC labels Mar 22, 2024
@breakid
Copy link

breakid commented May 15, 2024

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:

from typing import cast
from dynaconf import Dynaconf

class MySchema(Dynaconf):
    name: str
    age: int
    
settings: MySchema = cast(MySchema, Dynaconf(...))

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.

@pedro-psb
Copy link
Member Author

pedro-psb commented May 15, 2024

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.

@rochacbruno rochacbruno added this to the 3.3.x milestone May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Bug Not a Problem, expected behavior RFC
Projects
None yet
Development

No branches or pull requests

3 participants