Skip to content

Need exception thrown for missing attributes #92

@roubles

Description

@roubles

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions