[Django] #12568: SubFieldBase descriptor object should be accessible

0 views
Skip to first unread message

Django

unread,
Jan 10, 2010, 2:53:54 PM1/10/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
--------------------------------------+-------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone: 1.2
Component: Uncategorized | Version: 1.2-alpha
Keywords: SubFieldBase subclassing | Stage: Unreviewed
Has_patch: 1 |
--------------------------------------+-------------------------------------
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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12568>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 10, 2010, 2:58:32 PM1/10/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
---------------------------------------+------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone: 1.2
Component: Uncategorized | Version: 1.2-alpha
Resolution: | Keywords: SubFieldBase subclassing
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------+------------------------------------
Changes (by specialunderwear):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0

Comment:

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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:1>

Django

unread,
Feb 9, 2010, 9:02:27 AM2/9/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
---------------------------------------+------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone: 1.2
Component: Uncategorized | Version: 1.2-alpha
Resolution: | Keywords: SubFieldBase subclassing
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------+------------------------------------
Changes (by russellm):

* needs_tests: 0 => 1
* stage: Unreviewed => Accepted

--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:2>

Django

unread,
Feb 10, 2010, 8:41:02 AM2/10/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
---------------------------------------+------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone: 1.2
Component: Forms | Version: 1.2-alpha
Resolution: | Keywords: SubFieldBase subclassing
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------+------------------------------------
Changes (by russellm):

* component: Uncategorized => Forms

--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:3>

Django

unread,
Feb 23, 2010, 7:34:58 PM2/23/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
---------------------------------------+------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone:
Component: Forms | Version: 1.2-alpha
Resolution: | Keywords: SubFieldBase subclassing
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------+------------------------------------
Changes (by ubernostrum):

* milestone: 1.2 =>

Comment:

1.2 is feature-frozen, moving this feature request off the milestone.

--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:4>

Django

unread,
Aug 4, 2010, 3:28:10 PM8/4/10
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
---------------------------------------+------------------------------------
Reporter: specialunderwear | Owner: nobody
Status: new | Milestone:
Component: Forms | Version: 1.2-alpha
Resolution: | Keywords: SubFieldBase subclassing
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------+------------------------------------
Comment (by specialunderwear):

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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:5>

Django

unread,
Apr 2, 2011, 1:16:28 AM4/2/11
to djang...@holovaty.com, django-...@googlegroups.com
#12568: SubFieldBase descriptor object should be accessible
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
specialunderwear | Status: new
Type: Bug | Component: Forms
Milestone: | Severity: Normal
Version: 1.2-alpha | Keywords: SubFieldBase
Resolution: | subclassing
Triage Stage: Accepted | Has patch: 1
Needs documentation: 0 | Needs tests: 1
Patch needs improvement: 0 |
-------------------------------------+-------------------------------------
Changes (by mattmcc):

* type: => Bug
* severity: => Normal


--
Ticket URL: <http://code.djangoproject.com/ticket/12568#comment:6>

Reply all
Reply to author
Forward
0 new messages