From 409bae89005e1251c9410dd8ee4fbeb507a370b4 Mon Sep 17 00:00:00 2001 From: Alex Nelson Date: Fri, 6 Dec 2024 09:44:13 -0500 Subject: [PATCH] Align setUp and tearDown type signatures with unittest and rdflib.plugin The references below indicate the type requirements' sources. When reviewing subclasses of `GraphTestCase`, `setUp` and `tearDown` did no further work than the parent class `GraphTestCase` once the signatures were aligned with `typeshed.stdlib`, so the methods were removed. This patch was driven by the following command after activating type review on `setUp` and `tearDown` (adding `-> None`): ```bash mypy test/test_sqlalchemy_sqlite.py ``` This patch was then tested with the following command after identifying sibling ("cousin?") classes of the SQLite test. This command raised no errors after deactivating type review (adding `# type: ignore`) on a package that does not currently provide type signatures: ```bash mypy \ test/test_sqlalchemy_mysql.py \ test/test_sqlalchemy_postgresql.py \ test/test_sqlalchemy_postgresql_pg8000.py \ test/test_sqlalchemy_sqlite.py ``` Disclaimer: Participation by NIST in the creation of the documentation of mentioned software is not intended to imply a recommendation or endorsement by the National Institute of Standards and Technology, nor is it intended to imply that any specific software is necessarily the best available for the purpose. References: * https://github.com/RDFLib/rdflib/blob/7.1.1/rdflib/plugin.py#L128 * https://github.com/python/typeshed/blob/b40eb642e00c538e29fb037992eb4b21d9ff108c/stdlib/unittest/case.pyi#L119-L120 Signed-off-by: Alex Nelson --- test/graph_case.py | 14 +++++++++----- test/test_sqlalchemy_mysql.py | 13 ------------- test/test_sqlalchemy_postgresql.py | 18 ------------------ test/test_sqlalchemy_postgresql_pg8000.py | 20 +------------------- test/test_sqlalchemy_sqlite.py | 14 -------------- 5 files changed, 10 insertions(+), 69 deletions(-) diff --git a/test/graph_case.py b/test/graph_case.py index d05a14c..91b3a18 100644 --- a/test/graph_case.py +++ b/test/graph_case.py @@ -11,6 +11,10 @@ class GraphTestCase(unittest.TestCase): storetest = True identifier = URIRef("rdflib_test") + # storename is expected to be assigned in subclasses. + storename: str + uri: str = "sqlite://" + michel = URIRef(u"michel") tarek = URIRef(u"tarek") bob = URIRef(u"bob") @@ -23,13 +27,13 @@ class GraphTestCase(unittest.TestCase): namespace_dct = "http://purl.org/dc/terms/" namespace_saws = "http://purl.org/saws/ontology#" - def setUp(self, uri="sqlite://", storename=None): - store = plugin.get(storename, Store)(identifier=self.identifier) + def setUp(self) -> None: + store = plugin.get(self.storename, Store)(identifier=self.identifier) self.graph = Graph(store, identifier=self.identifier) - self.graph.open(uri, create=True) + self.graph.open(self.uri, create=True) - def tearDown(self, uri="sqlite://"): - self.graph.destroy(uri) + def tearDown(self) -> None: + self.graph.destroy(self.uri) self.graph.close() def addStuff(self): diff --git a/test/test_sqlalchemy_mysql.py b/test/test_sqlalchemy_mysql.py index 6cb6fa0..2212fef 100644 --- a/test/test_sqlalchemy_mysql.py +++ b/test/test_sqlalchemy_mysql.py @@ -39,12 +39,6 @@ class SQLAMySQLGraphTestCase(graph_case.GraphTestCase): uri = sqlalchemy_url create = True - def setUp(self): - super(SQLAMySQLGraphTestCase, self).setUp(uri=self.uri, storename=self.storename) - - def tearDown(self): - super(SQLAMySQLGraphTestCase, self).tearDown(uri=self.uri) - class SQLAMySQLContextTestCase(context_case.ContextTestCase): storetest = True @@ -52,13 +46,6 @@ class SQLAMySQLContextTestCase(context_case.ContextTestCase): uri = sqlalchemy_url create = True - def setUp(self): - super(SQLAMySQLContextTestCase, self).setUp( - uri=self.uri, storename=self.storename) - - def tearDown(self): - super(SQLAMySQLContextTestCase, self).tearDown(uri=self.uri) - def testLenInMultipleContexts(self): pytest.skip("Known issue.") diff --git a/test/test_sqlalchemy_postgresql.py b/test/test_sqlalchemy_postgresql.py index a2ec044..4c83022 100644 --- a/test/test_sqlalchemy_postgresql.py +++ b/test/test_sqlalchemy_postgresql.py @@ -30,15 +30,6 @@ class SQLAPgSQLGraphTestCase(graph_case.GraphTestCase): uri = sqlalchemy_url create = True - def setUp(self): - super(SQLAPgSQLGraphTestCase, self).setUp( - uri=self.uri, - storename=self.storename, - ) - - def tearDown(self): - super(SQLAPgSQLGraphTestCase, self).tearDown(uri=self.uri) - class SQLAPgSQLContextTestCase(context_case.ContextTestCase): storetest = True @@ -46,15 +37,6 @@ class SQLAPgSQLContextTestCase(context_case.ContextTestCase): uri = sqlalchemy_url create = True - def setUp(self): - super(SQLAPgSQLContextTestCase, self).setUp( - uri=self.uri, - storename=self.storename, - ) - - def tearDown(self): - super(SQLAPgSQLContextTestCase, self).tearDown(uri=self.uri) - def testLenInMultipleContexts(self): pytest.skip("Known issue.") diff --git a/test/test_sqlalchemy_postgresql_pg8000.py b/test/test_sqlalchemy_postgresql_pg8000.py index 407791e..8da93fa 100644 --- a/test/test_sqlalchemy_postgresql_pg8000.py +++ b/test/test_sqlalchemy_postgresql_pg8000.py @@ -5,7 +5,7 @@ import pytest try: - import pg8000 + import pg8000 # type: ignore assert pg8000 is not None except ImportError: pytest.skip("pg8000 not installed, skipping PgSQL tests", @@ -33,15 +33,6 @@ class SQLAPgSQLGraphTestCase(graph_case.GraphTestCase): uri = sqlalchemy_url create = True - def setUp(self): - """Setup.""" - super(SQLAPgSQLGraphTestCase, self).setUp( - uri=self.uri, storename=self.storename) - - def tearDown(self): - """Teardown.""" - super(SQLAPgSQLGraphTestCase, self).tearDown(uri=self.uri) - class SQLAPgSQLContextTestCase(context_case.ContextTestCase): """Context test case.""" @@ -51,15 +42,6 @@ class SQLAPgSQLContextTestCase(context_case.ContextTestCase): uri = sqlalchemy_url create = True - def setUp(self): - """Setup.""" - super(SQLAPgSQLContextTestCase, self).setUp( - uri=self.uri, storename=self.storename) - - def tearDown(self): - """Teardown.""" - super(SQLAPgSQLContextTestCase, self).tearDown(uri=self.uri) - def testLenInMultipleContexts(self): """Test lin in multiple contexts, known issue.""" pytest.skip("Known issue.") diff --git a/test/test_sqlalchemy_sqlite.py b/test/test_sqlalchemy_sqlite.py index e7ad1fe..dc9876b 100644 --- a/test/test_sqlalchemy_sqlite.py +++ b/test/test_sqlalchemy_sqlite.py @@ -22,26 +22,12 @@ class SQLASQLiteGraphTestCase(graph_case.GraphTestCase): storename = "SQLAlchemy" uri = sqlalchemy_url - def setUp(self): - super(SQLASQLiteGraphTestCase, self).setUp( - uri=self.uri, storename=self.storename) - - def tearDown(self): - super(SQLASQLiteGraphTestCase, self).tearDown(uri=self.uri) - class SQLASQLiteContextTestCase(context_case.ContextTestCase): storetest = True storename = "SQLAlchemy" uri = sqlalchemy_url - def setUp(self): - super(SQLASQLiteContextTestCase, self).setUp( - uri=self.uri, storename=self.storename) - - def tearDown(self): - super(SQLASQLiteContextTestCase, self).tearDown(uri=self.uri) - def testLenInMultipleContexts(self): pytest.skip("Known issue.")