Skip to content

Vector division broken in python 3.x #1914

Closed

Description

@sconstable

Setup

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

import sys; print(sys.version)
import platform; print(platform.python_implementation()); print(platform.platform())
import Bio; print(Bio.__version__)
python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.version)
3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]
>>> import platform; print(platform.python_implementation()); print(platform.platform())
CPython
Linux-4.18.0-13-generic-x86_64-with-Ubuntu-18.04-bionic
>>> import Bio; print(Bio.__version__)
1.73

Expected behaviour

I should be able to write v = Vector(2, 2, 2) / 2 to divide a vector by a scalar.

Actual behaviour

An error is raised: TypeError: unsupported operand type(s) for /: 'Vector' and 'int'
It appears to be due to the fact that BioPython supplied __div__ for Vectors, but in python 3.x we need to supply __truediv__ and __floordiv__ instead. My current work-around is to call Vector(2, 2, 2).__div__(2)

Steps to reproduce

simply divide a vector by an int.

Activity

  1. platinhom commented on Feb 1, 2019

    @platinhom
    Contributor

    You can add Bio.PDB.Vector.__truediv__ = Bio.PDB.Vector.__div__ before use.

    Biopython should modify the Bio/PDB/vectors.py, add from __future__ import division and modify __div__ to __truediv__.

  2. peterjc commented on Feb 1, 2019

    @peterjc
    Member

    This clearly highlights that vector division is missing in our unit tests, visible here (the __contains__ method also looks untested):

    https://codecov.io/gh/biopython/biopython/src/master/Bio/PDB/vectors.py

    Ideally we'd add something like @sconstable's example to Tests/test_PDB_vectors.py as part of fixing this.

    @platinhom Your fix sounds perfect - do you want to make a pull request?

  3. platinhom commented on Feb 1, 2019

    @platinhom
    Contributor

    @peterjc OK, I have pull a request.

  4. peterjc commented on Feb 2, 2019

    @peterjc
    Member

    It turns out a Python 2 and 3 fix was a little different, but you got there with #1920. Well done.

    Note for next time, GitHub lets you update a pull request by updating the associated branch, so you don't have to make new pull requests.

  5. peterjc commented on Mar 16, 2019

    @peterjc
    Member

    Fixed by #1920, thanks for reporting this issue.

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