Rename BaseModel.__values__ to BaseModel.__dict__#712
Rename BaseModel.__values__ to BaseModel.__dict__#712samuelcolvin merged 9 commits intopydantic:masterfrom
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 (😅) was that getattr(obj, "key") would still work. Theoretically I guess it's possible someone could be using 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 |
samuelcolvin
left a comment
There was a problem hiding this comment.
this looks great, tiny change to history then let's go. 🚀
| 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.
can you make all this one bullet point.
| ret = list(object.__dir__(self)) | ||
| ret.extend(self.__values__.keys()) | ||
| return ret | ||
| @property |
There was a problem hiding this comment.
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.
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.rsthas been updated#<number>@<whomever>