Skip to content

Commit

Permalink
Use f-strings everywhere (#1798)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Jul 28, 2024
1 parent c7aeae8 commit f433a67
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 61 deletions.
2 changes: 1 addition & 1 deletion betty/extension/nginx/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def stop(self) -> None:
@property
def public_url(self) -> str:
if self._container is not None:
return "http://%s" % self._container.ip
return f"http://{self._container.ip}"
raise NoPublicUrlBecauseServerNotStartedError()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion betty/gramps/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def _load_person(self, element: ElementTree.Element) -> None:
affiliation_name = surname_element.text
surname_prefix = surname_element.get("prefix")
if surname_prefix is not None:
affiliation_name = "%s %s" % (surname_prefix, affiliation_name)
affiliation_name = f"{surname_prefix} {affiliation_name}"
person_name = PersonName(
individual=individual_name,
affiliation=affiliation_name,
Expand Down
26 changes: 5 additions & 21 deletions betty/locale/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ def __init__(

@override
def __repr__(self) -> str:
return "<%s.%s(%s, %s, %s)>" % (
self.__class__.__module__,
self.__class__.__name__,
self.year,
self.month,
self.day,
)
return f"<{self.__class__.__module__}.{self.__class__.__name__}({self.year}, {self.month}, {self.day})>"

@property
def comparable(self) -> bool:
Expand Down Expand Up @@ -86,7 +80,7 @@ def to_range(self) -> DateRange:
"""
if not self.comparable:
raise ValueError(
"Cannot convert non-comparable date %s to a date range." % self
f"Cannot convert non-comparable date {self} to a date range."
)
if self.month is None:
month_start = 1
Expand Down Expand Up @@ -124,9 +118,7 @@ def __contains__(self, other: Any) -> bool:
return self == other
if isinstance(other, DateRange):
return self in other
raise TypeError(
"Expected to check a %s, but a %s was given" % (type(Datey), type(other))
)
raise TypeError(f"Expected to check a {Datey}, but a {type(other)} was given")

def __lt__(self, other: Any) -> bool:
return self._compare(other, operator.lt)
Expand Down Expand Up @@ -244,14 +236,7 @@ def __init__(

@override
def __repr__(self) -> str:
return "%s.%s(%s, %s, start_is_boundary=%s, end_is_boundary=%s)" % (
self.__class__.__module__,
self.__class__.__name__,
repr(self.start),
repr(self.end),
repr(self.start_is_boundary),
repr(self.end_is_boundary),
)
return f"{self.__class__.__module__}.{self.__class__.__name__}({repr(self.start)}, {repr(self.end)}, start_is_boundary={repr(self.start_is_boundary)}, end_is_boundary={repr(self.end_is_boundary)})"

@property
def comparable(self) -> bool:
Expand Down Expand Up @@ -281,8 +266,7 @@ def __contains__(self, other: Any) -> bool:
others.append(other.end)
else:
raise TypeError(
"Expected to check a %s, but a %s was given"
% (type(Datey), type(other))
f"Expected to check a {Datey}, but a {type(other)} was given"
)

if self.start is not None and self.end is not None:
Expand Down
6 changes: 1 addition & 5 deletions betty/tests/_package/test_license_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ def assert_is_compatible(self, package_license: dict[str, Any]) -> None:
if compatible_license in package_license["License"]:
return
raise AssertionError(
"%s is released under the %s, which is not known to be compatible with Betty's own license"
% (
package_license["Name"],
package_license["License"],
)
f"{package_license['Name']} is released under the {package_license['License']}, which is not known to be compatible with Betty's own license"
)

async def test_runtime_dependency_license_compatibility(self) -> None:
Expand Down
25 changes: 10 additions & 15 deletions betty/tests/gramps/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,14 @@ async def test_date_should_load_parts(
self, expected: Date, dateval_val: str
) -> None:
ancestry = await self._load_partial(
"""
f"""
<events>
<event handle="_e7692ea23775e80643fe4fcf91" change="1590243374" id="E0000">
<type>Birth</type>
<dateval val="%s" quality="calculated"/>
<dateval val="{dateval_val}" quality="calculated"/>
</event>
</events>
"""
% dateval_val
)
assert expected == ancestry[Event]["E0000"].date

Expand Down Expand Up @@ -1128,15 +1127,14 @@ async def test_event_should_include_privacy_from_attribute(
self, expected: Privacy, attribute_value: str
) -> None:
ancestry = await self._load_partial(
"""
f"""
<events>
<event handle="_e1dd3ac2fa22e6fefa18f738bdd" change="1552126811" id="E0000">
<type>Birth</type>
<attribute type="betty:privacy" value="%s"/>
<attribute type="betty:privacy" value="{attribute_value}"/>
</event>
</events>
"""
% attribute_value
)
event = ancestry[Event]["E0000"]
assert expected == event.privacy
Expand All @@ -1154,15 +1152,14 @@ async def test_file_should_include_privacy_from_attribute(
self, expected: Privacy, attribute_value: str
) -> None:
ancestry = await self._load_partial(
"""
f"""
<objects>
<object handle="_e66f421249f3e9ebf6744d3b11d" change="1583534526" id="O0000">
<file src="/tmp/file.txt" mime="text/plain" checksum="d41d8cd98f00b204e9800998ecf8427e" description="file"/>
<attribute type="betty:privacy" value="%s"/>
<attribute type="betty:privacy" value="{attribute_value}"/>
</object>
</objects>
"""
% attribute_value
)
file = ancestry[File]["O0000"]
assert expected == file.privacy
Expand Down Expand Up @@ -1201,15 +1198,14 @@ async def test_source_from_source_should_include_privacy_from_attribute(
self, expected: Privacy, attribute_value: str
) -> None:
ancestry = await self._load_partial(
"""
f"""
<sources>
<source handle="_e1dd686b04813540eb3503a342b" change="1558277217" id="S0000">
<stitle>A Whisper</stitle>
<srcattribute type="betty:privacy" value="%s"/>
<srcattribute type="betty:privacy" value="{attribute_value}"/>
</source>
</sources>
"""
% attribute_value
)
source = ancestry[Source]["S0000"]
assert expected == source.privacy
Expand All @@ -1227,12 +1223,12 @@ async def test_citation_should_include_privacy_from_attribute(
self, expected: Privacy, attribute_value: str
) -> None:
ancestry = await self._load_partial(
"""
f"""
<citations>
<citation handle="_e2c25a12a097a0b24bd9eae5090" change="1558277266" id="C0000">
<confidence>2</confidence>
<sourceref hlink="_e1dd686b04813540eb3503a342b"/>
<srcattribute type="betty:privacy" value="%s"/>
<srcattribute type="betty:privacy" value="{attribute_value}"/>
</citation>
</citations>
<sources>
Expand All @@ -1241,7 +1237,6 @@ async def test_citation_should_include_privacy_from_attribute(
</source>
</sources>
"""
% attribute_value
)
source = ancestry[Source]["S0000"]
source.public = True
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/locale/test_localized.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __eq__(self, other: Any) -> bool:
return self.locale == other.locale

def __repr__(self) -> str:
return "%s(%s)" % (self.__class__.__name__, self.locale)
return f"{self.__class__.__name__}({self.locale})"

@pytest.mark.parametrize(
("expected", "preferred_locale", "localizeds"),
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/project/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ async def test_load_not_an_extension_type_name_should_error(
) -> None:
dump: Any = ProjectConfiguration(tmp_path / "betty.json").dump()
dump["extensions"] = {
"%s.%s" % (self.__class__.__module__, self.__class__.__name__): {},
f"{self.__class__.__module__}.{self.__class__.__name__}": {}
}
sut = ProjectConfiguration(tmp_path / "betty.json")
with raises_error(error_type=AssertionFailed):
Expand Down
24 changes: 10 additions & 14 deletions betty/tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ async def test_file(self) -> None:
project.ancestry.add(file)
async with project:
await generate(project)
await assert_betty_html(project, "/file/%s/index.html" % file.id)
await assert_betty_json(project, "/file/%s/index.json" % file.id)
await assert_betty_html(project, f"/file/{file.id}/index.html")
await assert_betty_json(project, f"/file/{file.id}/index.json")

async def test_places(self) -> None:
async with App.new_temporary() as app, app, Project.new_temporary(
Expand All @@ -265,8 +265,8 @@ async def test_place(self) -> None:
project.ancestry.add(place)
async with project:
await generate(project)
await assert_betty_html(project, "/place/%s/index.html" % place.id)
await assert_betty_json(project, "/place/%s/index.json" % place.id)
await assert_betty_html(project, f"/place/{place.id}/index.html")
await assert_betty_json(project, f"/place/{place.id}/index.json")

async def test_people(self) -> None:
async with App.new_temporary() as app, app, Project.new_temporary(
Expand Down Expand Up @@ -309,9 +309,9 @@ async def test_event(self) -> None:
project.ancestry.add(event)
async with project:
await generate(project)
await assert_betty_html(project, "/event/%s/index.html" % event.id)
await assert_betty_html(project, f"/event/{event.id}/index.html")
await assert_betty_json(
project, "/event/%s/index.json" % event.id, "event"
project, f"/event/{event.id}/index.json", "event"
)

async def test_citation(self) -> None:
Expand All @@ -326,12 +326,8 @@ async def test_citation(self) -> None:
project.ancestry.add(citation, source)
async with project:
await generate(project)
await assert_betty_html(
project, "/citation/%s/index.html" % citation.id
)
await assert_betty_json(
project, "/citation/%s/index.json" % citation.id
)
await assert_betty_html(project, f"/citation/{citation.id}/index.html")
await assert_betty_json(project, f"/citation/{citation.id}/index.json")

async def test_sources(self) -> None:
async with App.new_temporary() as app, app, Project.new_temporary(
Expand All @@ -352,8 +348,8 @@ async def test_source(self) -> None:
project.ancestry.add(source)
async with project:
await generate(project)
await assert_betty_html(project, "/source/%s/index.html" % source.id)
await assert_betty_json(project, "/source/%s/index.json" % source.id)
await assert_betty_html(project, f"/source/{source.id}/index.html")
await assert_betty_json(project, f"/source/{source.id}/index.json")


class TestResourceOverride:
Expand Down
2 changes: 1 addition & 1 deletion betty/tests/test_wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ async def test_populate_link_should_set_locale(
new_temporary_app: App,
) -> None:
m_retriever = mocker.patch("betty.wikipedia._Retriever")
link = Link("http://%s.wikipedia.org/wiki/Amsterdam" % page_language)
link = Link(f"http://{page_language}.wikipedia.org/wiki/Amsterdam")
link.locale = locale
async with Project.new_temporary(new_temporary_app) as project, project:
sut = _Populator(project, m_retriever)
Expand Down
2 changes: 1 addition & 1 deletion betty/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def url(self) -> str:
"""
The URL to the web page.
"""
return "https://%s.wikipedia.org/wiki/%s" % (self.locale, self._name)
return f"https://{self.locale}.wikipedia.org/wiki/{self._name}"

@property
def title(self) -> str:
Expand Down

0 comments on commit f433a67

Please sign in to comment.