dynaconf icon indicating copy to clipboard operation
dynaconf copied to clipboard

Question: How do you use autocomplete with dynaconf?

Open Markxy opened this issue 5 years ago • 9 comments

Hello! First of all, great project, love the feel, idea and execution!

To the question- How do you use autocomplete with IDEs (PyCharm in my case) when using dynaconf? from dynaconf import settings gets its attributes on execution and I get that, but it takes away so much with it.. The autocomplete plus the IDE not being able to tell if there's a variable like that at all.

Or am I just missing something?

Because I already had a (quite long) settings.py file before introducing dynaconf and I wanted just to take out some vars and tuck them into separate files, I just set their values as: VAR_1 = dynaconf_settings.VAR_1

Markxy avatar Feb 18 '20 03:02 Markxy

resolved @Markxy still pending release

rochacbruno avatar May 12 '20 12:05 rochacbruno

awesome, @rochacbruno 👍

Markxy avatar May 12 '20 16:05 Markxy

Hi! My team is looking to adopt a new configuration framework and DynaConf looks really good. One of our goals is to maximize productivity, for example through code completion available in the IDE (e.g. PyCharm or VS Code).

settings.toml:

[foo]
bar = 1

my_app.py

from config import settings

# When I type "settings.", IDE should suggest "foo"
settings.foo.bar

I was initially thinking that this issue was about to enable this behavior, but after testing with dynaconf==3.1.4 and PyCharm 2020.3.5, I realized that I might have misinterpreted the scope of this specific feature. I can see that the foo is present at execution, but is there anyway to make it appear in IDE's autocompletions at coding time?

Thanks!

ynouri avatar Apr 05 '21 15:04 ynouri

Would very much like the IDE to autocomplete the settings as well! Would be a big boost to productivity

timovanasten avatar Apr 29 '22 08:04 timovanasten

+1 for the IDE autocomplete

efesurekli avatar May 31 '22 14:05 efesurekli

Hi. This would be extremely helpful. I did a workaround with arg parser

# settings.yaml
app:
  models_dir: models
  skip_download: true
# main.py
...
# Dynaconf
from config import settings

parser = argparse.ArgumentParser()
parser.add_argument('-m', '--models-dir', dest="models_dir", action="store", default=settings.app.models_dir)
parser.add_argument('-sd', '--skip-download', dest='skip_download', action="store_true", default=settings.app.skip_download)

@dataclass
class MyAppArgs:
    models_dir: str
    skip_download: bool

args = MyAppArgs(**vars(parser.parse_args()))

# Then VSCode autocompletes
skip_downloads = args.sk|  --> skip_downloads

It would be most pleasant to have it in dynaconf too. Without a workaround.

settings.a|  --> app

It there some kind of ETA?

SonGokussj4 avatar Mar 23 '23 10:03 SonGokussj4

The work is being done on #683

rochacbruno avatar Mar 23 '23 11:03 rochacbruno

I'm using a derived singleton class. I write the config parameters as class attributes so the IDE can auto-complete. Of course, this also means that you should keep the environment variables / settings file in sync with those attributes.

class Singleton(type):
    def __init__(cls, name, bases, mmbs):
        super(Singleton, cls).__init__(name, bases, mmbs)
        cls._instance = super(Singleton, cls).__call__()

    def __call__(cls, *args, **kw):
        return cls._instance


class Settings(Dynaconf, metaclass=Singleton):
    name: str
    age: int
    not_in_env_or_settings: str

    def __init__(self):
        print('Initializing project config...')
        super().__init__(
            envvar_prefix="PROJECT",
            settings_files="settings.yaml",
            environments=True,
            env_switcher="PROJECT_ENV",
        )
        
os.environ['PROJECT_NAME'] = 'my_name'
os.environ['PROJECT_AGE'] = '30'

settings = Settings()

print(settings.name)  # 'my_name'
print(settings.age)  # 30
print(settings.not_in_env_or_settings)  # AttributeError: 'Settings' object has no attribute 'NOT_IN_ENV_OR_SETTINGS'

ferreirajoaouerj avatar Feb 02 '24 03:02 ferreirajoaouerj

@ferreirajoaouerj we are planning to implement something similar to your example, but using Pydantic as the base model see #1045

rochacbruno avatar Feb 02 '24 14:02 rochacbruno