Skip to content

Commit

Permalink
DRAFT: Add ruff rules datetimez (DTZ)
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Feb 9, 2024
1 parent 4074baf commit a73cb50
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 72 deletions.
9 changes: 7 additions & 2 deletions scripts/make_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os.path
import random
import sys
from datetime import datetime
from datetime import datetime, timezone

from whoosh import fields, index
from whoosh.compat import u
Expand Down Expand Up @@ -46,7 +46,12 @@
frac += 0.15
path = u(f"{segnum}/{num}")
title = " ".join(random.choice(words) for _ in range(100))
dt = datetime(year=2000 + counter, month=(counter % 12) + 1, day=15)
dt = datetime(
year=2000 + counter,
month=(counter % 12) + 1,
day=15,
tzinfo=timezone.utc,
)

w.add_document(
path=path,
Expand Down
6 changes: 3 additions & 3 deletions src/whoosh/qparser/dateparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import re
import sys
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from whoosh.compat import iteritems, string_type
from whoosh.qparser import plugins, syntax
Expand Down Expand Up @@ -89,7 +89,7 @@ def parse(self, text, dt, pos=0, debug=-9999):

def date_from(self, text, dt=None, pos=0, debug=-9999):
if dt is None:
dt = datetime.now()
dt = datetime.now(tz=timezone.utc)

d, pos = self.parse(text, dt, pos, debug + 1)
return d
Expand Down Expand Up @@ -661,7 +661,7 @@ def parse(self, text, dt, pos=0, debug=-9999):

def date_from(self, text, basedate=None, pos=0, debug=-9999, toend=True):
if basedate is None:
basedate = datetime.utcnow()
basedate = datetime.now(tz=timezone.utc)

parser = self.get_parser()
if toend:
Expand Down
9 changes: 5 additions & 4 deletions src/whoosh/util/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import calendar
import copy
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone

from whoosh.compat import iteritems

Expand Down Expand Up @@ -171,7 +171,7 @@ def tuple(self):
)

def date(self):
return date(self.year, self.month, self.day)
return date(self.year, self.month, self.day, tzinfo=timezone.utc)

def copy(self):
return adatetime(
Expand Down Expand Up @@ -237,7 +237,7 @@ def floor(self):
s = 0
if ms is None:
ms = 0
return datetime(y, m, d, h, mn, s, ms)
return datetime(y, m, d, h, mn, s, ms, tzinfo=timezone.utc)

def ceil(self):
"""Returns a ``datetime`` version of this object with all unspecified
Expand Down Expand Up @@ -275,7 +275,7 @@ def ceil(self):
s = 59
if ms is None:
ms = 999999
return datetime(y, m, d, h, mn, s, ms)
return datetime(y, m, d, h, mn, s, ms, tzinfo=timezone.utc)

def disambiguated(self, basedate):
"""Returns either a ``datetime`` or unambiguous ``timespan`` version
Expand Down Expand Up @@ -514,4 +514,5 @@ def fix(at):
minute=at.minute,
second=at.second,
microsecond=at.microsecond,
tzinfo=timezone.utc,
)
6 changes: 4 additions & 2 deletions stress/test_bigsort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os.path
import random
import shutil
from datetime import datetime
from datetime import datetime, timezone

from whoosh import fields, index, query
from whoosh.compat import text_type
Expand All @@ -24,7 +24,9 @@ def test_bigsort():
t = now()
w = ix.writer(limitmb=512)
for i in range(times):
dt = datetime.fromtimestamp(random.randint(15839593, 1294102139))
dt = datetime.fromtimestamp(
random.randint(15839593, 1294102139), tz=timezone.utc
)
w.add_document(id=text_type(i), date=dt)
w.commit()
print("Writing took ", now() - t)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dateparse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from whoosh.qparser.dateparse import (
English,
Expand All @@ -8,7 +8,7 @@
timespan,
)

basedate = datetime(2010, 9, 20, 15, 16, 6, 454000)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000, tzinfo=timezone.utc)
english = English()


Expand Down
19 changes: 11 additions & 8 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import pytest
from whoosh import fields, qparser, query
Expand Down Expand Up @@ -340,7 +340,7 @@ def test_nontext_document():
)
ix = RamStorage().create_index(schema)

dt = datetime.now()
dt = datetime.now(tz=timezone.utc)
w = ix.writer()
for i in range(50):
w.add_document(id=i, num=i, date=dt + timedelta(days=i), even=not (i % 2))
Expand All @@ -365,7 +365,7 @@ def test_nontext_update():
)
ix = RamStorage().create_index(schema)

dt = datetime.now()
dt = datetime.now(tz=timezone.utc)
w = ix.writer()
for i in range(10):
w.add_document(id=i, num=i, date=dt + timedelta(days=i))
Expand All @@ -391,7 +391,8 @@ def test_datetime():
for month in range(1, 12):
for day in range(1, 28):
w.add_document(
id=u("%s-%s") % (month, day), date=datetime(2010, month, day, 14, 0, 0)
id=u("%s-%s") % (month, day),
date=datetime(2010, month, day, 14, 0, 0, tzinfo=timezone.utc),
)
w.commit()

Expand All @@ -409,8 +410,8 @@ def test_datetime():
assert len(r) == 27

q = qp.parse(u("date:[2010-05 to 2010-08]"))
startdt = datetime(2010, 5, 1, 0, 0, 0, 0)
enddt = datetime(2010, 8, 31, 23, 59, 59, 999999)
startdt = datetime(2010, 5, 1, 0, 0, 0, 0, tzinfo=timezone.utc)
enddt = datetime(2010, 8, 31, 23, 59, 59, 999999, tzinfo=timezone.utc)
assert q.__class__ is query.NumericRange
assert q.start == times.datetime_to_long(startdt)
assert q.end == times.datetime_to_long(enddt)
Expand Down Expand Up @@ -694,9 +695,11 @@ def test_valid_date_string():
query = field.parse_query("date", date_string)

# Define the expected start and end dates
expected_start = datetime_to_long(datetime.datetime(2022, 1, 1))
expected_start = datetime_to_long(
datetime.datetime(2022, 1, 1, tzinfo=timezone.utc)
)
expected_end = datetime_to_long(
datetime.datetime(2022, 1, 1)
datetime.datetime(2022, 1, 1, tzinfo=timezone.utc)
+ datetime.timedelta(days=1)
- datetime.timedelta(microseconds=1)
)
Expand Down
13 changes: 9 additions & 4 deletions tests/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
from collections import defaultdict
from datetime import datetime
from datetime import datetime, timezone

import pytest
from whoosh import __version__, analysis, fields, index, qparser, query
Expand Down Expand Up @@ -629,15 +629,20 @@ def test_multivalue():
)
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(id=1, date=datetime(2001, 1, 1), num=5)
w.add_document(id=1, date=datetime(2001, 1, 1, tzinfo=timezone.utc), num=5)
w.add_document(
id=2, date=[datetime(2002, 2, 2), datetime(2003, 3, 3)], num=[1, 2, 3, 12]
id=2,
date=[
datetime(2002, 2, 2, tzinfo=timezone.utc),
datetime(2003, 3, 3, tzinfo=timezone.utc),
],
num=[1, 2, 3, 12],
)
w.add_document(txt=u("a b c").split())

with ix.reader() as r:
assert ("num", 3) in r
assert ("date", datetime(2003, 3, 3)) in r
assert ("date", datetime(2003, 3, 3, tzinfo=timezone.utc)) in r
assert " ".join(r.field_terms("txt")) == "a b c"


Expand Down
6 changes: 3 additions & 3 deletions tests/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ def test_dismax():


def test_exclusion():
from datetime import datetime
from datetime import datetime, timezone

schema = fields.Schema(id=fields.ID(stored=True), date=fields.DATETIME)
ix = RamStorage().create_index(schema)
dt1 = datetime(1950, 1, 1)
dt2 = datetime(1960, 1, 1)
dt1 = datetime(1950, 1, 1, tzinfo=timezone.utc)
dt2 = datetime(1960, 1, 1, tzinfo=timezone.utc)
with ix.writer() as w:
# Make 39 documents with dates != dt1 and then make a last document
# with feed == dt1.
Expand Down
20 changes: 13 additions & 7 deletions tests/test_parse_plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from datetime import datetime
from datetime import datetime, timezone

from whoosh import analysis, fields, formats, qparser, query
from whoosh.compat import text_type, u
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_dateparser():
def cb(arg):
errs.append(arg)

basedate = datetime(2010, 9, 20, 15, 16, 6, 454000)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000, tzinfo=timezone.utc)
qp.add_plugin(dateparse.DateParserPlugin(basedate, callback=cb))

q = qp.parse(u("hello date:'last tuesday'"))
Expand Down Expand Up @@ -118,7 +118,7 @@ def cb(arg):
def test_date_range():
schema = fields.Schema(text=fields.TEXT, date=fields.DATETIME)
qp = qparser.QueryParser("text", schema)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000, tzinfo=timezone.utc)
qp.add_plugin(dateparse.DateParserPlugin(basedate))

q = qp.parse(u("date:['30 march' to 'next wednesday']"))
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_date_range():
def test_daterange_multi():
schema = fields.Schema(text=fields.TEXT, start=fields.DATETIME, end=fields.DATETIME)
qp = qparser.QueryParser("text", schema)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000, tzinfo=timezone.utc)
qp.add_plugin(dateparse.DateParserPlugin(basedate))

q = qp.parse("start:[2008 to] AND end:[2011 to 2011]")
Expand All @@ -177,7 +177,11 @@ def test_daterange_empty_field():
writer.commit()

with ix.searcher() as s:
q = query.DateRange("test", datetime.fromtimestamp(86400), datetime.today())
q = query.DateRange(
"test",
datetime.fromtimestamp(86400, tz=timezone.utc),
datetime.now(tz=timezone.utc),
)
r = s.search(q)
assert len(r) == 0

Expand All @@ -186,7 +190,7 @@ def test_free_dates():
a = analysis.StandardAnalyzer(stoplist=None)
schema = fields.Schema(text=fields.TEXT(analyzer=a), date=fields.DATETIME)
qp = qparser.QueryParser("text", schema)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000)
basedate = datetime(2010, 9, 20, 15, 16, 6, 454000, tzinfo=timezone.utc)
qp.add_plugin(dateparse.DateParserPlugin(basedate, free=True))

q = qp.parse(u("hello date:last tuesday"))
Expand Down Expand Up @@ -366,7 +370,9 @@ def test_gtlt():
assert len(q) == 3
assert q[0] == query.Term("a", "hello")
# As of this writing, date ranges don't support startexcl/endexcl
assert q[1] == query.DateRange("e", datetime(2001, 3, 29, 0, 0), None)
assert q[1] == query.DateRange(
"e", datetime(2001, 3, 29, 0, 0, tzinfo=timezone.utc), None
)
assert q[2] == query.Term("a", "there")

q = qp.parse(u("a:> alfa c:<= bravo"))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_requires():


def test_highlight_daterange():
from datetime import datetime
from datetime import datetime, timezone

schema = fields.Schema(
id=fields.ID(unique=True, stored=True),
Expand All @@ -412,15 +412,15 @@ def test_highlight_daterange():
id=u("1"),
title=u("Life Aquatic"),
content=u("A nautic film crew sets out to kill a gigantic shark."),
released=datetime(2004, 12, 25),
released=datetime(2004, 12, 25, tzinfo=timezone.utc),
)
w.update_document(
id=u("2"),
title=u("Darjeeling Limited"),
content=u(
"Three brothers meet in India for a life changing train " + "journey."
),
released=datetime(2007, 10, 27),
released=datetime(2007, 10, 27, tzinfo=timezone.utc),
)
w.commit()

Expand All @@ -433,7 +433,7 @@ def test_highlight_daterange():
== 'for a life changing <b class="match term0">train</b> journey'
)

r = s.search(DateRange("released", datetime(2007, 1, 1), None))
r = s.search(DateRange("released", datetime(2007, 1, 1, tzinfo=timezone.utc), None))
assert len(r) == 1
assert r[0].highlights("content") == ""

Expand Down
Loading

0 comments on commit a73cb50

Please sign in to comment.