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

[BUG] gettattr / hasattr broken on BaseConfig since 1.2.1 #34

Open
rkrell opened this issue Sep 21, 2023 · 0 comments
Open

[BUG] gettattr / hasattr broken on BaseConfig since 1.2.1 #34

rkrell opened this issue Sep 21, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@rkrell
Copy link
Contributor

rkrell commented Sep 21, 2023

Describe the bug
The has been introduced a change in commit 6aab658 (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 rkrell added the bug Something isn't working label Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants