-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeprecationWarnings for item getting, setting and deleting (#162)
Closes #160
- Loading branch information
1 parent
01145c4
commit dda0859
Showing
6 changed files
with
234 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
import warnings | ||
|
||
|
||
class BaseModel: | ||
def __getitem__(self, key): | ||
self._warn_deprecated_access(key, "getting") | ||
return getattr(self, key) | ||
|
||
def __setitem__(self, key, value): | ||
self._warn_deprecated_access(key, "setting") | ||
return setattr(self, key, value) | ||
|
||
def __delitem__(self, key): | ||
self._warn_deprecated_access(key, "deleting") | ||
return delattr(self, key) | ||
|
||
@staticmethod | ||
def _warn_deprecated_access(key, action): | ||
warnings.warn( | ||
message=( | ||
f'Using ["{key}"] for {action} attributes is deprecated ' | ||
"and will be removed in the next release. " | ||
f'Suggestion: Replace ["{key}"] with .{key}' | ||
), | ||
category=DeprecationWarning, | ||
stacklevel=3, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.