Skip to content

Commit 75049c4

Browse files
committed
Make mypy a dev dependency and fix any issues it found
1 parent d69d8ff commit 75049c4

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

nuclearmasses/ame_mass_parse.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,17 @@ def read_file(self) -> pd.DataFrame:
114114
df["Symbol"] = pd.to_numeric(df["Z"]).map(self.z_to_symbol)
115115

116116
# Combine the two columns to create the atomic mass then drop the redundant column
117+
# Pandas is happy to use '+' for any type, but mypy doesn't like it, hence the use of str.cat()
117118
df["AtomicMass"] = (
118-
df["AtomicNumber"].astype("string")
119-
+ "."
120-
+ df["AtomicMass"].astype("string").str.replace(".", "", regex=False)
119+
df["AtomicNumber"]
120+
.astype("string")
121+
.str.cat(df["AtomicMass"].astype("string").str.replace(".", "", regex=False), sep=".")
121122
)
122123
df = df.drop(columns=["AtomicNumber"])
123124

124125
# We need to rescale the error value because we combined the two columns above
125126
df = df.assign(AtomicMassError=df["AtomicMassError"].astype(float) / 1.0e6)
126-
127-
return df.astype(self._data_types())
128127
except ValueError as e:
129128
print(f"Parsing error: {e}")
129+
130+
return df.astype(self._data_types())

nuclearmasses/ame_reaction_1_parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def read_file(self) -> pd.DataFrame:
117117
df["TableYear"] = self.year
118118
df["N"] = pd.to_numeric(df["A"]) - pd.to_numeric(df["Z"])
119119
df["Symbol"] = pd.to_numeric(df["Z"]).map(self.z_to_symbol)
120-
121-
return df.astype(self._data_types())
122120
except ValueError as e:
123121
print(f"Parsing error: {e}")
122+
123+
return df.astype(self._data_types())

nuclearmasses/ame_reaction_2_parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def read_file(self) -> pd.DataFrame:
121121
df["TableYear"] = self.year
122122
df["N"] = pd.to_numeric(df["A"]) - pd.to_numeric(df["Z"])
123123
df["Symbol"] = pd.to_numeric(df["Z"]).map(self.z_to_symbol)
124-
125-
return df.astype(self._data_types())
126124
except ValueError as e:
127125
print(f"Parsing error: {e}")
126+
127+
return df.astype(self._data_types())

nuclearmasses/element_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ElementConverter:
77
TODO: Create accessor function that do some argument validation
88
"""
99

10-
def __init__(self):
10+
def __init__(self) -> None:
1111
"""Construct the symbol -> Z and Z -> symbol dictionaries."""
1212
# fmt: off
1313
# Fromatter wants to put each item on it's own line, I don't

nuclearmasses/nubase_parse.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import pathlib
5+
import typing
56

67
import pandas as pd
78

@@ -133,7 +134,7 @@ def read_file(self) -> pd.DataFrame:
133134
try:
134135
df = pd.read_fwf(
135136
self.filename,
136-
colspecs=self.column_limits,
137+
colspecs=typing.cast(typing.Sequence[tuple[int, int]], self.column_limits), # appease mypy
137138
names=self._column_names(),
138139
na_values=self._na_values(),
139140
keep_default_na=False,
@@ -155,16 +156,13 @@ def read_file(self) -> pd.DataFrame:
155156
df = df.drop(columns=["State", "IsomerEnergy", "IsomerEnergyError"])
156157

157158
# Convert stable isotopes into ones with enormous lifetimes with zero error so we can cast
158-
df.loc[df["HalfLifeValue"] == "stbl", ["HalfLifeValue", "HalfLifeUnit", "HalfLifeError"]] = [
159-
99.99,
160-
"Zy",
161-
0.0,
162-
]
159+
mask = df["HalfLifeValue"] == "stbl"
160+
df.loc[mask, ["HalfLifeValue", "HalfLifeUnit", "HalfLifeError"]] = (99.99, "Zy", 0.0)
163161

164162
df["HalfLifeValue"] = df["HalfLifeValue"].astype("string").str.replace(r"[<>?~]", "", regex=True)
165163
# We'll be lazy here and remove any characters in this column. Future us will parse this properly
166164
df["HalfLifeError"] = df["HalfLifeError"].astype("string").str.replace(r"[<>?~a-z]", "", regex=True)
167-
168-
return df.astype(self._data_types())
169165
except ValueError as e:
170166
print(f"Parsing error: {e}")
167+
168+
return df.astype(self._data_types())

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ keywords = [
4444
[project.optional-dependencies]
4545
dev = [
4646
"coverage",
47+
"mypy",
48+
"pandas-stubs",
4749
]
4850

4951
[project.urls]

0 commit comments

Comments
 (0)