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
Rename BaseModel.__values__ to BaseModel.__dict__ #712
Rename BaseModel.__values__ to BaseModel.__dict__ #712
Conversation
Codecov Report
@@ Coverage Diff @@
## master #712 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2727 2721 -6
Branches 539 539
=====================================
- Hits 2727 2721 -6 |
I noticed you are saying in the history update that removing |
Unfortunately >>> object.__getattr__
Traceback (most recent call last)
...
AttributeError: type object 'object' has no attribute '__getattr__' So from typing import Dict, Any
from pydantic import BaseModel
class MyModel(BaseModel):
my_values: Dict[str, Any]
def __getattr__(self, item):
try:
return self.my_values[item]
except KeyError:
return super().__getattr__(item)
m = MyModel(my_values=dict(foo='bar'))
print(m.my_values, m.foo, sep='\n')
m.bar # cause AttributeError
...
{'foo': 'bar'}
bar
Traceback (most recent call last):
...
return self.my_values[item]
KeyError: 'bar'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
return super().__getattr__(item)
AttributeError: 'super' object has no attribute '__getattr__' But having thoughts about this: it will fail only if object actually doesn't have attribute (nor in As for turning |
…into __dict__-instead-of-__values__
D'oh good catch on both points. I think what I was thinking ( So, maybe worth the call out, but unless we're missing something I think this seems very unlikely to affect existing code. |
@dmontagu, just updated #712 (comment) with 'real code' example, check it out :) |
@MrMrRobat |
this looks great, tiny change to history then let's go.
HISTORY.rst
Outdated
v0.32 (unreleased) | ||
.................. | ||
* **breaking change**: remove ``__getattr__`` on ``BaseModel``, #712 by @MrMrRobat | ||
* rename ``__values__`` to ``__dict__`` on ``BaseModel``, deprecation warning on use of old name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make all this one bullet point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, done!
ret = list(object.__dir__(self)) | ||
ret.extend(self.__values__.keys()) | ||
return ret | ||
@property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can leave this in for a couple of releases, I doubt it's necessary but no reason to remove it.
I assume with this change vars(model)
and dir(model)
still work correct, but have we checked that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have test_dir_fields
in tests/test_main.py
, so yeah, I checked that it's working
Just saw your comment here, thanks for adding it! |
No problem, I was working on a script to easily pull from and push to other's pull requests, so wanted to try it. :-) Thanks so much for finding an implementing this. |
Change Summary
What done
__values__
renamed to__dict__
all overBaseModel
__getattr__
and__dir__
removed as they become redundant__values__
property which will allow to use old attr, but will throw deprecation warningWhat gained
Related issue number
#711 / #701
Checklist
HISTORY.rst
has been updated#<number>
@<whomever>