From 724490338f65f9d88e0a1afb0cf61248eab61da8 Mon Sep 17 00:00:00 2001 From: javipalanca Date: Thu, 19 Dec 2024 16:05:39 +0100 Subject: [PATCH] Refactor type checks to use isinstance for improved readability and consistency --- spade/__init__.py | 2 +- tests/test_agent.py | 2 +- tests/test_behaviour.py | 12 ++++++------ tests/test_presence.py | 8 ++++---- tests/test_presence_manager.py | 4 ++-- tests/test_trace.py | 4 ++-- tests/test_web.py | 22 +++++++++++----------- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/spade/__init__.py b/spade/__init__.py index bb51709d..4c59e6bc 100644 --- a/spade/__init__.py +++ b/spade/__init__.py @@ -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]) \ No newline at end of file + await asyncio.gather(*[ag.start() for ag in agents]) diff --git a/tests/test_agent.py b/tests/test_agent.py index 7c2581be..a715e4f2 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -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() diff --git a/tests/test_behaviour.py b/tests/test_behaviour.py index 560d0ceb..db3134ec 100644 --- a/tests/test_behaviour.py +++ b/tests/test_behaviour.py @@ -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() diff --git a/tests/test_presence.py b/tests/test_presence.py index 2b11e3fa..d93efd95 100644 --- a/tests/test_presence.py +++ b/tests/test_presence.py @@ -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 diff --git a/tests/test_presence_manager.py b/tests/test_presence_manager.py index fef5c236..74533b0f 100644 --- a/tests/test_presence_manager.py +++ b/tests/test_presence_manager.py @@ -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 == [] diff --git a/tests/test_trace.py b/tests/test_trace.py index 262dee72..dd635dac 100644 --- a/tests/test_trace.py +++ b/tests/test_trace.py @@ -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" diff --git a/tests/test_web.py b/tests/test_web.py index e68401e0..dd618d51 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -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]