Skip to content

Commit

Permalink
fix ruff DTZ003 (closes #3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Aug 4, 2024
1 parent 41e6d99 commit 0f8b8fd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 23 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ ignore = [
"D105", # Missing docstring in magic method
"D205", # 1 blank line required between summary line and description
"D212", # Multi-line docstring summary should start at the first line
"DTZ003", # TODO: fix this (issue #3791)
"FBT001", # Boolean-typed positional argument in function definition
"FBT002", # Boolean default positional argument in function
"PD901", # pandas-df-variable-name
Expand Down
8 changes: 4 additions & 4 deletions src/pymatgen/alchemy/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from __future__ import annotations

import datetime
import json
import re
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from warnings import warn

Expand Down Expand Up @@ -302,7 +302,7 @@ def from_cif_str(
source = "uploaded cif"
source_info = {
"source": source,
"datetime": str(datetime.datetime.now(tz=datetime.timezone.utc)),
"datetime": str(datetime.now(tz=timezone.utc)),
"original_file": raw_str,
"cif_data": cif_dict[cif_keys[0]],
}
Expand Down Expand Up @@ -330,7 +330,7 @@ def from_poscar_str(
struct = poscar.structure
source_info = {
"source": "POSCAR",
"datetime": str(datetime.datetime.now(tz=datetime.timezone.utc)),
"datetime": str(datetime.now(tz=timezone.utc)),
"original_file": raw_str,
}
return cls(struct, transformations, history=[source_info])
Expand All @@ -341,7 +341,7 @@ def as_dict(self) -> dict[str, Any]:
dct["@module"] = type(self).__module__
dct["@class"] = type(self).__name__
dct["history"] = jsanitize(self.history)
dct["last_modified"] = str(datetime.datetime.now(datetime.timezone.utc))
dct["last_modified"] = str(datetime.now(timezone.utc))
dct["other_parameters"] = jsanitize(self.other_parameters)
return dct

Expand Down
6 changes: 3 additions & 3 deletions src/pymatgen/analysis/ewald.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import bisect
import math
from copy import copy, deepcopy
from datetime import datetime
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from warnings import warn

Expand Down Expand Up @@ -535,7 +535,7 @@ def __init__(self, matrix, m_list, num_to_return=1, algo=ALGO_FAST):
# sets this to true it breaks the recursion and stops the search.
self._finished = False

self._start_time = datetime.utcnow()
self._start_time = datetime.now(tz=timezone.utc)

self.minimize_matrix()

Expand Down Expand Up @@ -605,7 +605,7 @@ def best_case(self, matrix, m_list, indices_left):
interaction_correction = np.sum(step3)

if self._algo == self.ALGO_TIME_LIMIT:
elapsed_time = datetime.utcnow() - self._start_time
elapsed_time = datetime.now(tz=timezone.utc) - self._start_time
speedup_parameter = elapsed_time.total_seconds() / 1800
avg_int = np.sum(interaction_matrix, axis=None)
avg_frac = np.mean(np.outer(1 - fractions, 1 - fractions))
Expand Down
8 changes: 4 additions & 4 deletions src/pymatgen/entries/entry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import collections
import csv
import datetime
import itertools
import json
import logging
import multiprocessing as mp
import re
from collections import defaultdict
from datetime import datetime, timezone
from typing import TYPE_CHECKING

from monty.json import MontyDecoder, MontyEncoder, MSONable
Expand Down Expand Up @@ -112,7 +112,7 @@ def group_entries_by_structure(
"""
if comparator is None:
comparator = SpeciesComparator()
start = datetime.datetime.now(tz=datetime.timezone.utc)
start = datetime.now(tz=timezone.utc)
logger.info(f"Started at {start}")
entries_host = [(entry, _get_host(entry.structure, species_to_remove)) for entry in entries]
if ncpus:
Expand Down Expand Up @@ -161,8 +161,8 @@ def group_entries_by_structure(
entry_groups = []
for g in groups:
entry_groups.append(json.loads(g, cls=MontyDecoder))
logging.info(f"Finished at {datetime.datetime.now(tz=datetime.timezone.utc)}")
logging.info(f"Took {datetime.datetime.now(tz=datetime.timezone.utc) - start}")
logging.info(f"Finished at {datetime.now(tz=timezone.utc)}")
logging.info(f"Took {datetime.now(tz=timezone.utc) - start}")
return entry_groups


Expand Down
7 changes: 3 additions & 4 deletions src/pymatgen/io/res.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from __future__ import annotations

import datetime
import re
from dataclasses import dataclass
from datetime import date, datetime, timezone
from typing import TYPE_CHECKING

from monty.io import zopen
Expand All @@ -24,7 +24,6 @@

if TYPE_CHECKING:
from collections.abc import Iterator
from datetime import date
from pathlib import Path
from typing import Any, Callable, Literal

Expand Down Expand Up @@ -421,9 +420,9 @@ def _parse_date(cls, string: str) -> date:
raise ResParseError(f"Could not parse the date from {string=}.")

day, month, year, *_ = match.groups()
month_num = datetime.datetime.strptime(month, "%b").replace(tzinfo=datetime.timezone.utc).month
month_num = datetime.strptime(month, "%b").replace(tzinfo=timezone.utc).month

return datetime.date(int(year), month_num, int(day))
return date(int(year), month_num, int(day))

def _raise_or_none(self, err: ResParseError) -> None:
if self.parse_rems != "strict":
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/vasp/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import datetime
import itertools
import logging
import math
Expand All @@ -13,6 +12,7 @@
from collections import defaultdict
from collections.abc import Iterable
from dataclasses import dataclass
from datetime import datetime, timezone
from glob import glob
from io import StringIO
from pathlib import Path
Expand Down Expand Up @@ -845,7 +845,7 @@ def get_computed_entry(
ComputedStructureEntry/ComputedEntry
"""
if entry_id is None:
entry_id = f"vasprun-{datetime.datetime.now(tz=datetime.timezone.utc)}"
entry_id = f"vasprun-{datetime.now(tz=timezone.utc)}"
param_names = {
"is_hubbard",
"hubbards",
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/util/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from __future__ import annotations

import datetime
import json
import re
import sys
from datetime import datetime, timezone
from io import StringIO
from typing import TYPE_CHECKING, NamedTuple

Expand Down Expand Up @@ -256,7 +256,7 @@ def __init__(
if not all(sys.getsizeof(h) < MAX_HNODE_SIZE for h in history):
raise ValueError(f"One or more history nodes exceeds the maximum size limit of {MAX_HNODE_SIZE} bytes")

self.created_at = created_at or datetime.datetime.utcnow()
self.created_at = created_at or datetime.now(tz=timezone.utc)

def as_dict(self):
"""Get MSONable dict."""
Expand Down
6 changes: 3 additions & 3 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

from __future__ import annotations

import datetime
import json
import os
import re
import subprocess
import webbrowser
from datetime import datetime, timezone
from typing import TYPE_CHECKING

import requests
Expand Down Expand Up @@ -150,7 +150,7 @@ def update_changelog(ctx: Context, version: str | None = None, dry_run: bool = F
dry_run (bool, optional): If True, the function will only print the changes without
updating the actual change log file. Defaults to False.
"""
version = version or f"{datetime.datetime.now(tz=datetime.timezone.utc):%Y.%-m.%-d}"
version = version or f"{datetime.now(tz=timezone.utc):%Y.%-m.%-d}"
output = subprocess.check_output(["git", "log", "--pretty=format:%s", f"v{__version__}..HEAD"])
lines = []
ignored_commits = []
Expand Down Expand Up @@ -197,7 +197,7 @@ def release(ctx: Context, version: str | None = None, nodoc: bool = False) -> No
version (str, optional): The version to release.
nodoc (bool, optional): Whether to skip documentation generation.
"""
version = version or f"{datetime.datetime.now(tz=datetime.timezone.utc):%Y.%-m.%-d}"
version = version or f"{datetime.now(tz=timezone.utc):%Y.%-m.%-d}"
ctx.run("rm -r dist build pymatgen.egg-info", warn=True)
set_ver(ctx, version)
if not nodoc:
Expand Down

0 comments on commit 0f8b8fd

Please sign in to comment.