[Biopython] type object 'RestrictionType' has no attribute 'size'

Wibowo Arindrarto w.arindrarto at gmail.com
Thu Dec 5 00:03:02 UTC 2013


Hi Christopher, Peter,

Thank you for reporting the issue. After prodding around, I have to
say this looks like a very interesting 'bug' :). I'm going to ramble a
bit below, so I'll give a short TL;DR first:

I don't think this is a bug from Biopython per se, since if you try
the same code in a regular Python shell, it works (as Peter has
shown). Don't get me wrong,
I do appreciate the report in IPython notebook form (I'm a user myself
and I think more people should use it, also for reporting bugs like
this). But in the context of fixing the problem, I'd suggest that you
use a regular Python shell instead.

Now, that being said, let's delve in a bit into what's causing the bug
(with a small disclaimer: these are my observations and I'm quite sure
there are others who know more about this issue).

If you see the stack
trace of the error, you'd see that a huge chunk is coming from the
IPython codebase. Initially I thought this was something of an
artifact (I expected the entire stack to trace only Biopython code),
but I was wrong. That stack trace really points to the cause of the
problem: the way IPython displays cell results in the browser.

More specifically, I've pinpointed this problem into the way IPython
displays restriction enzymes. You can try it in your same Ipython
notebook:

   from Bio import Restriction as rst
   rst.EcoRI

The code above will never fail in a regular Python shell, but will
fail in IPython.

Why is this the case? I suspect it has something to do with the way
Biopython's Restriction module is written. Turns out, there's already
some nifty metaclass tricks being employed there that allows a given
enzyme to have the class type of itself (e.g. instead of having a
RestrictionType class with an instance called EcoRI, we have a
metaclass RestrictionType with a class called EcoRI). To keep it
short, IPython seems to do an attribute lookup on these metaclasses
when it tries to display the object.  You'll notice that on line 333
of /data7/cfriedline/anaconda/envs/conda/lib/python2.7/site-packages/IPython/lib/pretty.py,
there is an expression that tries to get the class of the object to
display:

    obj_class = getattr(obj, '__class__', None) or type(obj)

However, these metaclasses have not been instantiated into a class and
thus do not have any 'size' attribute, IPython complains and raises an
error.

So to sum up, this behavior may be cause by IPython instead of
Biopython. It's perhaps a good idea to mention this in the IPython
forum / mailing list, too. They've been doing a very impressive job so
far, so I have a hunch this may have popped up in one of their
discussions.

I have to admit that my metaclass chops are still very limited as
these usually aren't required for most Python programming. But if
you're interested this SO page
(http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python)
has a very easy to follow explanation about what they are and why they
are useful.

Anyway, that's enough for now I guess :).

Cheers,
Bow

P.S. Peter, perhaps these other Restriction bugs are related with the
metaclass? I've never delved that deep into the submodule's codebase,
but it looks like there are some very interesting tidbits there.

On Wed, Dec 4, 2013 at 5:48 PM, Christopher J Friedline
<cfriedline at vcu.edu> wrote:
> Hi everyone,
>
> I ran into this problem today, and wanted to bring it up to see if anyone
> else has seen it.  Searching the archives didn't get me anywhere.  I'm
> running a simple restriction analysis (on a random genome), and getting
> this error. If I look for cut sites with the enzymes individually, it works
> for each. However, when I put them into a RestrictionBatch, I get an error
> on the class.
>
> type object 'RestrictionType' has no attribute 'size'
>
> I put an IPython notebook describing this here:
>
> http://nbviewer.ipython.org/gist/cfriedline/7790932
>
> Bio (1.6.2), IPython (1.1.0), Python (2.7.6/Anaconda 1.8)
>
> Any help is appreciated,
>
> Thanks,
> Chris
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython



More information about the Biopython mailing list