Skip to content

SeqFeature.WithinPosition instances fail to unpickle #2207

Closed

Description

@macraild

Setup

I am reporting a problem with Biopython version, Python version, and operating
system as follows:

3.6.6 (default, Jan 26 2019, 16:53:05)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
CPython
Linux-3.10.0-693.21.1.el7.x86_64-x86_64-with-centos-7.2.1511-Core
1.74

Expected behaviour

Instances of WithinPosition should pickle and unpickle

Actual behaviour

Unpickling a WithinPosition instance fails with:


TypeError Traceback (most recent call last)
in
4 wp = SeqFeature.WithinPosition(10,10,13)
5 p = pickle.dumps(wp)
----> 6 unpickled_wp = pickle.loads(p)

TypeError: __new__() missing 2 required positional arguments: 'left' and 'right'

Steps to reproduce

I ran into this unpickling an object containing a SeqRecord derived from UniProt acc P08514, but some digging narrowed it down to:

import pickle
from Bio import SeqFeature

wp = SeqFeature.WithinPosition(10,10,13)
p = pickle.dumps(wp)
unpickled_wp = pickle.loads(p)

Pickle expects __getnewargs__() or __getnewargs_ex__() to be defined for instances of classes that alter the call signature of __new__(cls). I assume WithinPosition inherits __getnewargs__() from int, but then overrides the __new__ signature so the two are not compatible. It looks as if several other *Position classes in this module will fail in the same way

Activity

  1. peterjc commented on Aug 12, 2019

    @peterjc
    Member

    Do you know enough about pickle to fix this and add a couple of unit tests, e.g. to Tests/test_SeqFeature.py for the positions, and perhaps also higher level in Tests/test_SeqRecord.py?

    You can see a test added here to help with pickling a SeqRecord in the past:

    https://github.com/biopython/biopython/blob/biopython-174/Bio/SeqRecord.py#L50

  2. macraild commented on Aug 13, 2019

    @macraild
    ContributorAuthor

    I'm not certain, but the attached diff seems to work.

    Strictly speaking ExactPosition, BeforePosition and AfterPosition should also define __getnewargs__ returning the __new__ arguments position and extension. I haven't done so here because:

    1. extension is only available to the instance by way of a method marked obsolete
    2. in practice it doesn't matter because extension is defined by default and any other value raises an error

    2207diff.txt

  3. peterjc commented on Aug 13, 2019

    @peterjc
    Member

    Other than a repeated typo ("intances"), that looks good. Could you submit it as a pull request please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions