You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Importing external code with leading underscores? #2483
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.
@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?
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
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.
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).
Activity
lorforlinux commentedon Jan 4, 2020
@peterjc i agree that
from XXX import *is a bad practice but we are using for example:-from urllib.request import Requestonly, so i don't know what can go wrong if we remove underscores.But why we are using an underscore in some files but not in all of them?
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?peterjc commentedon Jan 4, 2020
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):
Versus others (avoiding the py3k examples) where the leading underscore makes very clear the import is private to that module:
mdehoon commentedon Jan 4, 2020
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:
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.AlignBio.AlphabetBio.PDBBio.SeqBio.SeqRecordThese 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.peterjc commentedon Jan 4, 2020
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).
Directly import urllib, not via Bio._py3k (#2476)