pyaml_env icon indicating copy to clipboard operation
pyaml_env copied to clipboard

[BUG] gettattr / hasattr broken on BaseConfig since 1.2.1

Open rkrell opened this issue 2 years ago • 0 comments

Describe the bug The has been introduced a change in commit 6aab658e2aa805ea8d4d0944b256d7cb4d9f944a (version 1.2.1) which makes it impossible to test for an not existing attribute on a BaseConfig object instance and use a default using

getattr(base, key, default)
hasattr(base, key)

If the attribute key doesn't exist, there is always thrown a KeyError due to the override of the __getattr__ function on BaseConfig. This is highly impractical and a bigger breaking change in this minor version step.

To Reproduce Steps to reproduce the behavior:

YAML example:

jobs:
  a:
    foo: "bar"
    is_optional: true
  b:
    foo: "bar"

Results:

  File "/home/user/work/scripts/job.py", line ??, in __init__
    conf.is_optional if hasattr(conf, 'is_optional') else None
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/work/lib/python3.11/site-packages/pyaml_env/base_config.py", line 28, in __getattr__
    return self.__dict__[field_name]
           ~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'is_optional'

Expected behavior getattr and hasattr should work on BaseConfig without limitations.

Additional context

  • Python version 3.11
  • OS Debian 12

rkrell avatar Sep 21 '23 07:09 rkrell