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

Need exception thrown for missing attributes #92

Open
roubles opened this issue Jan 6, 2023 · 0 comments
Open

Need exception thrown for missing attributes #92

roubles opened this issue Jan 6, 2023 · 0 comments

Comments

@roubles
Copy link

roubles commented Jan 6, 2023

Need a way to tell Munch to throw an exception (AttributeError or something) when an attribute is missing. Today, it returns None, or you can set a default value, but you can't configure it to throw an exception.

Why is this important?

Munchified dictionaries don't work with existing getattr() calls

Say you have existing code without munch that does this:

    foo = getattr(conf, 'foo', 1)

If conf['foo'] does not exist, foo gets set to 1

Now, lets say you munchify conf (so that you can easily access the fields). This will break existing getattrs. Using the same code:

    from munch import DefaultMunch

    conf = DefaultMunch.fromDict(conf)        
    foo = getattr(conf, 'foo', 1)

Now, id conf['foo'] does not exist, BUT foo gets set to None. This breaks any existing code that was using getattr(). getattr() is a very commonly used for dict access and specifying default values.

We need a way to use the ease of accessing fields, but still throw an exception on missing attributes

@roubles roubles changed the title Need undefined exception thrown for missing attributes Need exception thrown for missing attributes Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant