Skip to content

Commit

Permalink
Refactor type checks to use isinstance for improved readability and c…
Browse files Browse the repository at this point in the history
…onsistency
javipalanca committed Dec 19, 2024
1 parent a547784 commit 7244903
Showing 7 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion spade/__init__.py
Original file line number Diff line number Diff line change
@@ -35,4 +35,4 @@ async def wait_until_finished(agents: Union[List[AgentType], AgentType]) -> None
async def start_agents(agents: List[AgentType]) -> None:
if not isinstance(agents, list):
agents = [agents]
await asyncio.gather(*[ag.start() for ag in agents])
await asyncio.gather(*[ag.start() for ag in agents])
2 changes: 1 addition & 1 deletion tests/test_agent.py
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ async def test_client():

await agent.start(auto_register=False)

assert type(agent.client) == spade.xmpp_client.XMPPClient
assert isinstance(agent.client, spade.xmpp_client.XMPPClient)

await agent.stop()

12 changes: 6 additions & 6 deletions tests/test_behaviour.py
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ async def run(self):

await behaviour.join()

assert type(behaviour.exit_code) == ZeroDivisionError
assert isinstance(behaviour.exit_code, ZeroDivisionError)
assert not agent.flag
await agent.stop()

@@ -137,7 +137,7 @@ async def run(self):

await behaviour.join()

assert type(behaviour.exit_code) == ZeroDivisionError
assert isinstance(behaviour.exit_code, ZeroDivisionError)
assert not agent.flag
await agent.stop()

@@ -160,7 +160,7 @@ async def on_end(self):

await behaviour.join()

assert type(behaviour.exit_code) == ZeroDivisionError
assert isinstance(behaviour.exit_code, ZeroDivisionError)
assert not agent.flag
await agent.stop()

@@ -981,7 +981,7 @@ async def run(self):

assert fsm_.is_killed()

assert type(fsm_.exit_code) == Exception
assert isinstance(fsm_.exit_code, Exception)

await agent.stop()

@@ -1004,7 +1004,7 @@ async def run(self):

assert fsm_.is_killed()

assert type(fsm_.exit_code) == Exception
assert isinstance(fsm_.exit_code, Exception)

await agent.stop()

@@ -1030,7 +1030,7 @@ async def on_end(self):

assert fsm_.is_killed()

assert type(fsm_.exit_code) == Exception
assert isinstance(fsm_.exit_code, Exception)

await agent.stop()

8 changes: 4 additions & 4 deletions tests/test_presence.py
Original file line number Diff line number Diff line change
@@ -181,7 +181,7 @@ async def test_get_contacts(jid: JID, iq: Iq):
bare_jid = jid.bare
assert bare_jid in contacts
assert len(contacts) == 2
assert type(contacts[bare_jid]) == Contact
assert isinstance(contacts[bare_jid], Contact)
assert contacts[bare_jid].name == "My Friend"
assert contacts[bare_jid].subscription == "both"
assert contacts[bare_jid].groups == ["Friends"]
@@ -209,7 +209,7 @@ async def test_get_contacts_with_update(jid: JID, iq: Iq):

bare_jid = jid.bare
assert bare_jid in contacts
assert type(contacts[bare_jid]) == Contact
assert isinstance(contacts[bare_jid], Contact)
assert contacts[bare_jid].name == "My Friend"
assert contacts[bare_jid].subscription == "both"
assert contacts[bare_jid].groups == ["Friends"]
@@ -239,7 +239,7 @@ async def test_get_contacts_with_update_unavailable(jid: JID, iq: Iq):

bare_jid = jid.bare
assert bare_jid in contacts
assert type(contacts[bare_jid]) == Contact
assert isinstance(contacts[bare_jid], Contact)
assert contacts[bare_jid].name == "My Friend"
assert contacts[bare_jid].subscription == "both"
assert contacts[bare_jid].groups == ["Friends"]
@@ -260,7 +260,7 @@ async def test_get_contact(jid: JID, iq: Iq):
agent.presence.handle_roster_update(iq)
contact = agent.presence.get_contact(jid)

assert type(contact) == Contact
assert isinstance(contact, Contact)
assert contact.name == "My Friend"
assert contact.subscription == "both"
assert len(contact.groups) == 1
4 changes: 2 additions & 2 deletions tests/test_presence_manager.py
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ async def test_handle_roster_update(jid: JID, iq: Iq):

bare_jid = jid.bare
assert bare_jid in contacts
assert type(contacts[bare_jid]) == Contact
assert isinstance(contacts[bare_jid], Contact)
assert contacts[bare_jid].name == "My Friend"
assert contacts[bare_jid].subscription == "both"
assert contacts[bare_jid].groups == ["Friends"]
@@ -213,7 +213,7 @@ async def test_update_roster(jid: JID):

bare_jid = jid2.bare
assert bare_jid in contacts
assert type(contacts[bare_jid]) == Contact
assert isinstance(contacts[bare_jid], Contact)
assert contacts[bare_jid].name == "My Friend"
assert contacts[bare_jid].subscription == "to"
assert contacts[bare_jid].groups == []
4 changes: 2 additions & 2 deletions tests/test_trace.py
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ def test_append():
trace.append("EVENT")
assert len(trace.store) == 1
assert trace.store[0][1] == "EVENT"
assert type(trace.store[0][0]) == datetime.datetime
assert isinstance(trace.store[0][0], datetime.datetime)
assert trace.store[0][2] is None


@@ -63,7 +63,7 @@ def test_append_with_category():
trace.append("EVENT", "CATEGORY")
assert len(trace.store) == 1
assert trace.store[0][1] == "EVENT"
assert type(trace.store[0][0]) == datetime.datetime
assert isinstance(trace.store[0][0], datetime.datetime)
assert trace.store[0][2] == "CATEGORY"


22 changes: 11 additions & 11 deletions tests/test_web.py
Original file line number Diff line number Diff line change
@@ -43,10 +43,10 @@ async def test_default_template_path():
package_loader = loader.loaders[0]
filesystem_loader = loader.loaders[1]

assert type(loader) == ChoiceLoader
assert isinstance(loader, ChoiceLoader)
assert len(loader.loaders) == 2
assert type(package_loader) == PackageLoader
assert type(filesystem_loader) == FileSystemLoader
assert isinstance(package_loader, PackageLoader)
assert isinstance(filesystem_loader, FileSystemLoader)

assert "internal_tpl_agent.html" in package_loader.list_templates()
assert "internal_tpl_agent.html" not in filesystem_loader.list_templates()
@@ -63,11 +63,11 @@ async def test_add_template_path_init():
env = get_env(agent.web.app)
loader = env.loader

assert type(loader) == ChoiceLoader
assert isinstance(loader, ChoiceLoader)
assert len(loader.loaders) == 3
assert type(loader.loaders[0]) == FileSystemLoader
assert type(loader.loaders[1]) == PackageLoader
assert type(loader.loaders[2]) == FileSystemLoader
assert isinstance(loader.loaders[0], FileSystemLoader)
assert isinstance(loader.loaders[1], PackageLoader)
assert isinstance(loader.loaders[2], FileSystemLoader)

filesystem_loader = loader.loaders[0]

@@ -86,11 +86,11 @@ async def test_add_template_path():
env = get_env(agent.web.app)
loader = env.loader

assert type(loader) == ChoiceLoader
assert isinstance(loader, ChoiceLoader)
assert len(loader.loaders) == 3
assert type(loader.loaders[0]) == FileSystemLoader
assert type(loader.loaders[1]) == PackageLoader
assert type(loader.loaders[2]) == FileSystemLoader
assert isinstance(loader.loaders[0], FileSystemLoader)
assert isinstance(loader.loaders[1], PackageLoader)
assert isinstance(loader.loaders[2], FileSystemLoader)

filesystem_loader = loader.loaders[0]

0 comments on commit 7244903

Please sign in to comment.