Skip to content

Importing external code with leading underscores? #2483

Closed

Description

@peterjc

Logging this as a specific issue following recent discussions with @mdehoon etc including:

#2461 (comment)
#2476 (comment)
#2476 (comment)

Taking Bio/Entrez/Parser.py as an example, until recently we had:

from Bio._py3k import urlopen as _urlopen

As part of #2420 (removing Bio._py3k now that we've dropped Python 2 support), I proposed:

from urllib.request import urlopen as _urlopen	

As of #2461, Michiel used:

from urllib.request import urlopen

The leading underscore namespaces the functions as Bio.Entrez.Parser._urlopen where it is clearly private, but it is now Bio.Entrez.Parser.urlopen - to me this is now unintentionally part of the module's public API.

I recall issues in the past where code relied on this kind of accidental second-hand import, although usually in combination with the frowned upon use of from XXX import * - the point being if the user did from Bio.Entrez.Parser import * this would include urlopen.

I think we could be more proactive in avoiding accidentally exposing our inputs by using leading underscores - but would welcome additional perspectives on this - especially from other Python libraries.

(I think it is an aside, but we are not defining public interfaces with __all__ either, but https://docs.python.org/3/tutorial/modules.html#importing-from-a-package suggests that is only important if we expect people to use from XXX import * which is considered bad style.)

Activity

  1. lorforlinux commented on Jan 4, 2020

    @lorforlinux
    Contributor

    @peterjc i agree that from XXX import * is a bad practice but we are using for example:- from urllib.request import Request only, so i don't know what can go wrong if we remove underscores.

    The leading underscore namespaces the functions as Bio.Entrez.Parser._urlopen where it is clearly private, but it is now Bio.Entrez.Parser.urlopen - to me this is now unintentionally part of the module's public API.

    But why we are using an underscore in some files but not in all of them?

    I recall issues in the past where code relied on this kind of accidental second-hand import, although usually in combination with the frowned upon use of from XXX import * - the point being if the user did from Bio.Entrez.Parser import * this would include urlopen.

    I am sorry but I am not getting it clearly 🙇🏻‍♂️ , the function will be included if we use Bio.Entrez.Parser import * only the name will be different right?

  2. peterjc commented on Jan 4, 2020

    @peterjc
    MemberAuthor

    I used underscores on the py3k imports because not only were they not intended to end-user re-use, but also we knew they would be temporary (as the imports would change once we dropped Python 2) so it was even more important to signal they should not be reused.

    The inconsistency is the point of this issue. Consider the following (incomplete set of examples):

    Bio/SeqIO/InsdcIO.py:from datetime import datetime
    Bio/SeqIO/PdbIO.py:from io import StringIO
    Bio/SeqIO/QualityIO.py:from math import log
    Bio/SeqIO/SnapGeneIO.py:from re import sub
    Bio/SeqIO/SnapGeneIO.py:from struct import unpack
    Bio/SeqIO/XdnaIO.py:from re import match
    Bio/SeqIO/XdnaIO.py:from struct import pack, unpack
    

    Versus others (avoiding the py3k examples) where the leading underscore makes very clear the import is private to that module:

    Bio/Application/__init__.py:from subprocess import CalledProcessError as _ProcessCalledError
    Bio/SeqUtils/CheckSum.py:from binascii import crc32 as _crc32
    
  3. mdehoon commented on Jan 4, 2020

    @mdehoon
    Contributor

    I don't think adding underscores are a good idea, at least for now. For one thing, Python itself doesn't have a clear rule for this. Just a random sampling grepped from the Python standard library:

    from shlex import quote
    from socket import SOL_SOCKET, SO_TYPE
    from socket import _GLOBAL_DEFAULT_TIMEOUT
    from socket import socket, AF_INET, SOCK_STREAM, create_connection
    from sre_constants import *
    from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
    from struct import pack, unpack
    from struct import unpack as _unpack
    from sys import maxsize
    from sys import modules as sys_modules
    from termios import *
    from threading import RLock
    from threading import current_thread, RLock
    from time import monotonic as _time
    from time import monotonic as time
    from tkinter import simpledialog
    from token import *
    from token import EXACT_TOKEN_TYPES
    from token import NAME, DEDENT, OP
    from traceback import format_exception_only
    from types import FunctionType
    from types import MappingProxyType, DynamicClassAttribute
    from types import MethodType
    from types import ModuleType
    from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
    from unicodedata import ucd_3_2_0 as unicodedata
    from urllib.parse import quote_from_bytes as urlquote_from_bytes
    from warnings import warn
    from warnings import warn as _warn
    from weakref import ref
    from xml.parsers.expat import ParserCreate
    

    So, why be more strict than Python itself? I have a hard time to think of a scenario where importing without an underscore would cause a problem. Furthermore introducing such underscores adds a new complication to reading and understanding code (especially if written by others). Not to mention that we'd have to change 100's of imports in close to 250 files, and instruct all developers to follow this new rule. Some will, some won't, so you'll end up with a mishmash of imports with and without underscores.

    Most importantly: There are bigger issues in Biopython to worry about. In my mind, at least the following modules in Biopython have serious problems:

    • Bio.Align
    • Bio.Alphabet
    • Bio.PDB
    • Bio.Seq
    • Bio.SeqRecord

    These include modules fundamental to Biopython, and I hope we can spend our time fixing those.
    Other modules would also benefit from some maintenance (for example Bio.SwissProt, but I am sure there are many others). Polishing code can happen later.

  4. peterjc commented on Jan 4, 2020

    @peterjc
    MemberAuthor

    The inconsistency in the standard library is a convincing argument - OK then, let's close this.

    But I disagree with you that where to put our effort is a zero sum approach - code polishing is probably one of the easier ways to bring in new people (although there is still a time cost for the existing developers in reviewing and helping that process along).

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions