Skip to content

Commit 9137cc9

Browse files
committed
🧹 Use stacklevel for warnings, enable ruff rules 'B'
1 parent 48d7bb3 commit 9137cc9

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where = ["src"]
4848

4949
[tool.ruff]
5050
line-length = 120
51-
lint.select = ["E", "F", "I"]
51+
lint.select = ["E", "F", "I", "B"]
5252

5353

5454
[tool.liccheck]

src/mods4pandas/alto4pandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def walk(m):
157157
try:
158158
yield from walk(f.path)
159159
except PermissionError:
160-
warnings.warn(f"Error walking {f.path}")
160+
warnings.warn(f"Error walking {f.path}", stacklevel=1)
161161
else:
162162
yield m.path
163163

src/mods4pandas/lib.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ def filter(self, cond, warn=None) -> TagGroup:
8888
new_group.append(e)
8989
else:
9090
if warn:
91-
warnings.warn("Filtered {} element ({})".format(self.tag, warn))
91+
warnings.warn(f"Filtered {self.tag} element ({warn})", stacklevel=1)
9292
return TagGroup(self.tag, new_group)
9393

9494
def force_singleton(self, warn=True) -> TagGroup:
9595
if len(self.group) == 1:
9696
return self
9797
else:
9898
if warn:
99-
warnings.warn("Forced single instance of {}".format(self.tag))
99+
warnings.warn(f"Forced single instance of {self.tag}", stacklevel=1)
100100
return TagGroup(self.tag, self.group[:1])
101101

102102
RE_ISO8601_DATE = r"^\d{2}(\d{2}|XX)(-\d{2}-\d{2})?$" # Note: Includes non-specific century dates like '18XX'
@@ -106,31 +106,33 @@ def fix_date(self) -> TagGroup:
106106
for e in self.group:
107107
if e.attrib.get("encoding") == "w3cdtf":
108108
# This should be 'iso8601' according to MODS-AP 2.3.1
109-
warnings.warn("Changed w3cdtf encoding to iso8601")
109+
warnings.warn("Changed w3cdtf encoding to iso8601", stacklevel=1)
110110
e.attrib["encoding"] = "iso8601"
111111

112112
new_group = []
113113
for e in self.group:
114114
if e.text is None:
115-
warnings.warn("Empty date")
115+
warnings.warn("Empty date", stacklevel=1)
116116
continue
117117
if e.attrib.get("encoding") == "iso8601" and re.match(
118118
self.RE_ISO8601_DATE, e.text
119119
):
120120
new_group.append(e)
121121
elif re.match(self.RE_ISO8601_DATE, e.text):
122-
warnings.warn("Added iso8601 encoding to date {}".format(e.text))
122+
warnings.warn(f"Added iso8601 encoding to date {e.text}", stacklevel=1)
123123
e.attrib["encoding"] = "iso8601"
124124
new_group.append(e)
125125
elif m := re.match(self.RE_GERMAN_DATE, e.text):
126-
warnings.warn("Converted date {} to iso8601 encoding".format(e.text))
126+
warnings.warn(
127+
f"Converted date {e.text} to iso8601 encoding", stacklevel=1
128+
)
127129
e.text = "{}-{}-{}".format(
128130
m.group("yyyy"), m.group("mm"), m.group("dd")
129131
)
130132
e.attrib["encoding"] = "iso8601"
131133
new_group.append(e)
132134
else:
133-
warnings.warn('Not a iso8601 date: "{}"'.format(e.text))
135+
warnings.warn(f'Not a iso8601 date: "{e.text}"', stacklevel=1)
134136
new_group.append(e)
135137
self.group = new_group
136138

@@ -159,21 +161,27 @@ def fix_event_type(self) -> TagGroup:
159161
and e.find("mods:edition", ns).text == "[Electronic ed.]"
160162
):
161163
e.attrib["eventType"] = "digitization"
162-
warnings.warn("Fixed eventType for electronic ed.")
164+
warnings.warn(
165+
"Fixed eventType for electronic ed.", stacklevel=1
166+
)
163167
continue
164168
except AttributeError:
165169
pass
166170
try:
167171
if e.find("mods:dateIssued", ns) is not None:
168172
e.attrib["eventType"] = "publication"
169-
warnings.warn("Fixed eventType for an issued origin")
173+
warnings.warn(
174+
"Fixed eventType for an issued origin", stacklevel=1
175+
)
170176
continue
171177
except AttributeError:
172178
pass
173179
try:
174180
if e.find("mods:dateCreated", ns) is not None:
175181
e.attrib["eventType"] = "production"
176-
warnings.warn("Fixed eventType for a created origin")
182+
warnings.warn(
183+
"Fixed eventType for a created origin", stacklevel=1
184+
)
177185
continue
178186
except AttributeError:
179187
pass
@@ -184,20 +192,25 @@ def fix_script_term(self) -> TagGroup:
184192
# MODS-AP 2.3.1 is not clear about this, but it looks like that this should be lower case.
185193
if e.attrib["authority"] == "ISO15924":
186194
e.attrib["authority"] = "iso15924"
187-
warnings.warn("Changed scriptTerm authority to lower case")
195+
warnings.warn(
196+
"Changed scriptTerm authority to lower case", stacklevel=1
197+
)
188198
return self
189199

190200
def fix_language_term(self) -> TagGroup:
191201
for e in self.group:
192202
if e.attrib["authority"] == "iso639-2":
193203
e.attrib["authority"] = "iso639-2b"
194-
warnings.warn("Changed languageTerm authority to iso639-2b")
204+
warnings.warn(
205+
"Changed languageTerm authority to iso639-2b", stacklevel=1
206+
)
195207
if e.attrib["authority"] == "rfc3066":
196208
if e.text == "de":
197209
e.attrib["authority"] = "iso639-2b"
198210
e.text = "deu"
199211
warnings.warn(
200-
"Changed languageTerm authority from rfc3066 to iso639-2b"
212+
"Changed languageTerm authority from rfc3066 to iso639-2b",
213+
stacklevel=1,
201214
)
202215
return self
203216

@@ -206,7 +219,7 @@ def add_missing_type_text(self) -> TagGroup:
206219
# Default to type=text
207220
if "type" not in e.attrib:
208221
e.attrib["type"] = "text"
209-
warnings.warn("Added placeTerm type='text'")
222+
warnings.warn("Added placeTerm type='text'", stacklevel=1)
210223
return self
211224

212225
def remove_attributes(self, attribs) -> TagGroup:
@@ -304,7 +317,9 @@ def fix_recordIdentifier_source_zdb(self) -> TagGroup:
304317
if e.get("type") == "zdb":
305318
e.attrib["source"] = "zdb"
306319
del e.attrib["type"]
307-
warnings.warn("Fixed recordIdentifier type 'zdb' to source")
320+
warnings.warn(
321+
"Fixed recordIdentifier type 'zdb' to source", stacklevel=1
322+
)
308323
return self
309324

310325

src/mods4pandas/mods4pandas.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ def only_standard_title(title_info):
337337
TagGroup(tag, group).is_singleton().has_no_attributes().text()
338338
)
339339
elif tag == "{http://www.loc.gov/mods/v3}origininfo":
340-
warnings.warn("Ignoring invalid tag origininfo (should be originInfo)")
340+
warnings.warn(
341+
"Ignoring invalid tag origininfo (should be originInfo)", stacklevel=1
342+
)
341343
else:
342344
if raise_errors:
343345
raise ValueError('Unknown tag "{}"'.format(tag))

0 commit comments

Comments
 (0)