Opened 15 years ago
Closed 11 years ago
#12568 closed Bug (fixed)
SubFieldBase descriptor object should be accessible
Reported by: | Lars van de Kerkhof | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.2-alpha |
Severity: | Normal | Keywords: | SubFieldBase subclassing |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently any field that has SubFieldBase as a metaclass will throw an exception if you try access the descriptor object without an instance.
Things like the following can not be done with the current implementation:
def _get_members_of_type(obj, member_type): """ Finds members of a certain type in obj. """ def filter_member_type(member): try: return type(member) is member_type except AttributeError: return False # if the type of obj is the metaclass for all models, just search in the object # because it is not a model instance but a type if type(obj) is ModelBase: key_hash = inspect.getmembers(obj, filter_member_type) else: key_hash = inspect.getmembers(obj.__class__, filter_member_type) return key_hash
SubFieldBase will throw an AttributeError every time inspect.getmembers is used on a model instance or class.
Normal behaviour would be to return the descriptor object when no instance is passed. All django's standard fields
behave this way, so it would be best if SubFieldBase would do the same.
I will attach a patch against current trunk.
Attachments (3)
Change History (15)
by , 15 years ago
Attachment: | subfieldbase-fix.diff added |
---|
comment:1 by , 15 years ago
comment:2 by , 15 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 15 years ago
Component: | Uncategorized → Forms |
---|
comment:4 by , 15 years ago
milestone: | 1.2 |
---|
1.2 is feature-frozen, moving this feature request off the milestone.
comment:5 by , 14 years ago
I added a new patch + tests to fix this issue in 1.2
Please do not let descriptors break inspect, but instead behave like a proper property, which returns self when a descriptor is accessed without context:
http://users.rcn.com/python/download/Descriptor.htm#properties
If you need the descriptor object itself to be inaccessible for some reason, just return None instead of self. This would also make more sense, an uninitialized property is None, just like a regular attribute.
There are more places where the AttributeError is thrown, for instance ManagerDescriptor raises:
AttributeError: Manager isn't accessible via MyModel instances
When a model instance is inspected.
ImageField and FileField also do it, which to me seems wrong.
by , 14 years ago
Attachment: | subfieldbase_fix_and_test.diff added |
---|
This time the test is also added
comment:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
by , 11 years ago
Attachment: | subfieldbase-20130917.diff added |
---|
Rebased against git master (5be56d0)
comment:9 by , 11 years ago
Any chance this can make it into an official release? I'm happy to polish up however needed, or take this to django-developers
if it really needs discussion.
Rebased patch against git master attached above, as a start.
This bug causes an AttributeError
when trying to use sphinx-autodoc on custom fields which have __metaclass__ = SubFieldBase
, due to the underlying "must be accessed from instance" AttributeError
. The equivalent ticket for fixing inspect()
for Django's built-in fields was #8248, fixed in [9634] (~5 years ago).
comment:12 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
More specific:
SubFieldBase? will throw an AttributeError? every time inspect.getmembers is used on a model instance or class that has fields which use SubFieldBase.