Skip to content

Commit 022ced8

Browse files
authored
Remove Python 2 shims (#1260)
Remove legacy Python 2 compatibility code. Since Python 3 is the only supported version now, these shims are no longer necessary.
1 parent dfa64ca commit 022ced8

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

src/openfermion/chem/molecular_data.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
+ 0.5 * \sum_{p,q,r,s} h[p,q,r,s] a_p^\dagger a_q^\dagger a_r a_s
3232
"""
3333

34-
# Define a compatible basestring for checking between Python 2 and 3
35-
try:
36-
basestring
37-
except NameError: # pragma: no cover
38-
basestring = str
39-
4034

4135
# Define error objects which inherit from Exception.
4236
class MoleculeNameError(Exception):
@@ -248,7 +242,7 @@ def name_molecule(geometry, basis, multiplicity, charge, description):
248242
Raises:
249243
MoleculeNameError: If spin multiplicity is not valid.
250244
"""
251-
if not isinstance(geometry, basestring):
245+
if not isinstance(geometry, str):
252246
# Get sorted atom vector.
253247
atoms = [item[0] for item in geometry]
254248
atom_charge_info = [(atom, atoms.count(atom)) for atom in set(atoms)]
@@ -501,7 +495,7 @@ def __init__(
501495

502496
# Metadata fields with default values.
503497
self.charge = charge
504-
if not isinstance(description, basestring):
498+
if not isinstance(description, str):
505499
raise TypeError("description must be a string.")
506500
self.description = description
507501

@@ -518,7 +512,7 @@ def __init__(
518512
self.filename = data_directory + '/' + self.name
519513

520514
# Attributes generated automatically by class.
521-
if not isinstance(geometry, basestring):
515+
if not isinstance(geometry, str):
522516
self.n_atoms = len(geometry)
523517
self.atoms = sorted(
524518
[row[0] for row in geometry], key=lambda atom: periodic_hash_table[atom]
@@ -707,7 +701,7 @@ def save(self):
707701
with h5py.File("{}.hdf5".format(tmp_name), "w") as f:
708702
# Save geometry (atoms and positions need to be separate):
709703
d_geom = f.create_group("geometry")
710-
if not isinstance(self.geometry, basestring):
704+
if not isinstance(self.geometry, str):
711705
atoms = [numpy.bytes_(item[0]) for item in self.geometry]
712706
positions = numpy.array([list(item[1]) for item in self.geometry])
713707
else:

src/openfermion/testing/testing_utils.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,9 @@ def module_importable(module):
338338
bool
339339
340340
"""
341-
import sys
341+
from importlib import util
342342

343-
if sys.version_info >= (3, 4):
344-
from importlib import util
345-
346-
plug_spec = util.find_spec(module)
347-
else:
348-
# Won't enter unless Python<3.4, so ignore for testing
349-
import pkgutil # pragma: ignore
350-
351-
plug_spec = pkgutil.find_loader(module) # pragma: no cover
343+
plug_spec = util.find_spec(module)
352344
if plug_spec is None:
353345
return False
354346
else:

0 commit comments

Comments
 (0)