Skip to content

Commit 4f89b6e

Browse files
committed
Make type hints compatible with numpy 1.20.
1 parent b5f73fe commit 4f89b6e

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

mypy.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ strict_equality = True
3131
[mypy-setuptools.*]
3232
ignore_missing_imports = True
3333

34-
[mypy-numpy.*]
35-
ignore_missing_imports = True
36-
3734
[mypy-scipy.*]
3835
ignore_missing_imports = True
3936

pytest.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
filterwarnings =
33
error
44
ignore::ImportWarning
5-
ignore:numpy.dtype size changed
6-
ignore:numpy.ufunc size changed
7-
ignore:the imp module is deprecated
85
junit_family = xunit2

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ setup_requires =
3333
install_requires =
3434
numpy
3535
scipy>=0.15
36-
h5py>=3.0.0
36+
h5py>=3.2.0
3737
fsc.export
3838
symmetry-representation>=0.2
3939
click>=7.0, !=7.1.0

tbmodels/_tb_model.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,15 @@ def from_wannier_files( # pylint: disable=too-many-locals
633633
wannier_pos_cartesian = np.array(wannier_pos_list_cartesian)
634634
atom_pos_cartesian = np.array([a.pos for a in atom_list_cartesian])
635635
if pos_kind == "wannier":
636-
pos_cartesian = wannier_pos_cartesian
636+
pos_cartesian: ty.Union[
637+
ty.List[np.ndarray], np.ndarray
638+
] = wannier_pos_cartesian
637639
elif pos_kind == "nearest_atom":
638640
if distance_ratio_threshold < 1:
639641
raise ValueError(
640642
"Invalid value for 'distance_ratio_threshold': must be >= 1."
641643
)
642-
pos_cartesian = []
644+
pos_cartesian = ty.cast(ty.List[np.ndarray], [])
643645
for p in wannier_pos_cartesian:
644646
p_reduced = la.solve(kwargs["uc"].T, np.array(p).T).T
645647
T_base = np.floor(p_reduced)
@@ -1130,7 +1132,7 @@ def hamilton(
11301132
H = pos_exponential.conjugate().transpose((0, 2, 1)) * H * pos_exponential
11311133

11321134
if single_point:
1133-
return H[0]
1135+
return ty.cast(np.ndarray, H[0])
11341136
return H
11351137

11361138
def eigenval(
@@ -1149,7 +1151,7 @@ def eigenval(
11491151
hamiltonians = self.hamilton(k)
11501152
if hamiltonians.ndim == 3:
11511153
return [la.eigvalsh(ham) for ham in hamiltonians]
1152-
return la.eigvalsh(hamiltonians)
1154+
return ty.cast(np.ndarray, la.eigvalsh(hamiltonians))
11531155

11541156
# -------------------MODIFYING THE MODEL ----------------------------#
11551157
def add_hop(
@@ -1320,7 +1322,7 @@ def set_sparse(self, sparse: bool = True):
13201322
# change existing matrices
13211323
with contextlib.suppress(AttributeError):
13221324
for k, v in self.hop.items():
1323-
self.hop[k] = self._matrix_type(v)
1325+
self.hop[k] = self._matrix_type(v) # type: ignore
13241326

13251327
# If Python 3.4 support is dropped this could be made more straightforwardly
13261328
# However, for now the default pickle protocol (and thus multiprocessing)
@@ -1592,7 +1594,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
15921594
)
15931595
# convert to reduced coordinates
15941596
if uc is None:
1595-
new_uc = self.uc
1597+
new_uc: ty.Optional[np.ndarray] = self.uc
15961598
uc_reduced = np.eye(self.dim)
15971599
else:
15981600
new_uc = np.array(uc)

tbmodels/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def matrix_to_hop(
1717
orbitals: ty.Optional[ty.Sequence[int]] = None,
1818
R: ty.Sequence[int] = (0, 0, 0),
1919
multiplier: float = 1.0,
20-
) -> ty.List[ty.List[ty.Union[complex, int, np.ndarray]]]:
20+
) -> ty.List[ty.Tuple[complex, int, int, np.ndarray]]:
2121
r"""
2222
Turns a square matrix into a series of hopping terms.
2323
@@ -40,11 +40,11 @@ def matrix_to_hop(
4040
for i, row in enumerate(mat):
4141
for j, x in enumerate(row):
4242
hop.append(
43-
[
43+
(
4444
multiplier * x,
4545
orbitals[i],
4646
orbitals[j],
4747
np.array(R, dtype=int),
48-
]
48+
)
4949
)
5050
return hop

tbmodels/kdotp.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ def hamilton(
6868
else:
6969
single_point = False
7070

71-
ham: np.ndarray = sum(
72-
np.prod(k_array ** k_powers, axis=-1).reshape(-1, 1, 1)
73-
* mat[np.newaxis, :, :]
74-
for k_powers, mat in self.taylor_coefficients.items()
71+
ham = ty.cast(
72+
np.ndarray,
73+
sum(
74+
np.prod(k_array ** k_powers, axis=-1).reshape(-1, 1, 1)
75+
* mat[np.newaxis, :, :]
76+
for k_powers, mat in self.taylor_coefficients.items()
77+
),
7578
)
7679
if single_point:
77-
return ham[0]
80+
return ty.cast(np.ndarray, ham[0])
7881
return ham
7982

8083
def eigenval(
@@ -93,4 +96,4 @@ def eigenval(
9396
hamiltonians = self.hamilton(k)
9497
if hamiltonians.ndim == 3:
9598
return [la.eigvalsh(ham) for ham in hamiltonians]
96-
return la.eigvalsh(hamiltonians)
99+
return ty.cast(np.ndarray, la.eigvalsh(hamiltonians))

0 commit comments

Comments
 (0)