Repository navigation
Closed
Description
Hi,
I am writting genbank file, and sometime date are silently ignored and replace by 1980. My first suggestion is to add a warning somehow indicating that the date annotation is invalide.
When investigating the code producing the date if found that datetime instance are only considered if the abbreviated month is equivalent to the english abbreviation. With not english locale it thus sometime ignore the date and silently replace it by 01-JAN-1980.
If you confirm me that it is not the expected behavior to not work with non-english locale , I can work on a PR
Best regards,
Step to reproduce:
"""
docker run -it python:3.12 bash
apt-get update && apt-get install -y locales
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen
echo "de_DE.UTF-8 UTF-8" >> /etc/locale.gen
echo "pt_BR.UTF-8 UTF-8" >> /etc/locale.gen
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
echo "en_GB.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
locale -a # check that all three lang are not there
pip install git+https://github.com/biopython/biopython.git
python # and run following script
"""
import locale
from datetime import datetime
from tempfile import NamedTemporaryFile
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
import Bio.SeqIO
date = datetime.strptime('2025-10-01', '%Y-%m-%d')
record = SeqRecord(
Seq('ATGC'),
id="foo",
annotations={
"molecule_type": "dna",
"topology": "circular",
"date": date,
},
)
this_year = str(date.year)
for l in [
'en_GB.utf8',
'de_DE.utf8',
'fr_FR.utf8',
'pt_BR.utf8',
'en_US.utf8',
]:
locale.setlocale(locale.LC_ALL, l)
with NamedTemporaryFile(suffix='.gb') as file:
with open(file.name, 'w') as gb_file:
Bio.SeqIO.write(record, gb_file, "genbank")
with open(file.name, 'r') as written_file:
first_line = written_file.readlines()[0]
print(f"In {l}: {first_line}")
if this_year not in first_line:
print(f"ERROR with {l} as date is {date.strftime('%d-%b-%Y').upper()}")'en_GB.utf8'
1
In en_GB.utf8: LOCUS foo 4 bp dna circular UNK 01-OCT-2025
'de_DE.utf8'
1
In de_DE.utf8: LOCUS foo 4 bp dna circular UNK 01-JAN-1980
ERROR with de_DE.utf8 as date is 01-OKT-2025
'fr_FR.utf8'
1
In fr_FR.utf8: LOCUS foo 4 bp dna circular UNK 01-JAN-1980
ERROR with fr_FR.utf8 as date is 01-OCT.-2025
'pt_BR.utf8'
1
In pt_BR.utf8: LOCUS foo 4 bp dna circular UNK 01-JAN-1980
ERROR with pt_BR.utf8 as date is 01-OUT-2025
'en_US.utf8'
1
In en_US.utf8: LOCUS foo 4 bp dna circular UNK 01-OCT-2025
Metadata
Metadata
Assignees
Labels
No labels
Activity
peterjc commentedon Mar 19, 2025
The code usually expects
record.annotations["date"]to be a string, but in your example it is adatetimeobject, and it seems this is line of our code is unintentionally locale aware:See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
A pull request would be welcome! I guess this would most simply be fixed with an integer to three letter English month abbreviation dictionary, but you might have a better idea?
bryan-brancotte commentedon Mar 20, 2025
Yes integer to three letter English is enough. I was thinking of adding a test to highlight that we have to be independent of local, but I haven't yet looked at the test environment and the local available. Going for a PR in the following days.
uxritu commentedon Mar 20, 2025
Hi, I'm new to open-source contributions. Can I work on this issue?
peterjc commentedon Mar 20, 2025
@uxritu That would not be helpful. Bryan has already documented this issue and offered to work on it.
peterjc commentedon Mar 20, 2025
Note @uxritu has written the same comment on multiple issues today - which I am initially willing to assuming is merely over enthusiasm.
bryan-brancotte commentedon Mar 21, 2025
Hi, I'm on it indeed, but thanks !
I have a question, do I propose to add a warning and fix the issue in one or two distinct PR ?
Edit: I went for one, but ok to split it into two PR
Locale independent date serialization, also accept datetime.date
Add warning when date is invalide/not in the good format
Locale independent date serialization, also accept datetime.date
Add warning when date is invalide/not in the good format
Add warning when date is invalide/not in the good format
peterjc commentedon Mar 21, 2025
One PR should be enough 👍
Locale independent date serialization (biopython#4977)