From c6ac30d4ccc26553a12e8c37cdf9078cbe9084b3 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 10 Jul 2023 11:36:47 +0300 Subject: Fix linting errors caused by bump to Python 3.10 --- gn3/api/search.py | 5 +++-- .../authentication/oauth2/endpoints/introspection.py | 4 ++-- .../authentication/oauth2/endpoints/utilities.py | 4 +++- .../oauth2/grants/authorisation_code_grant.py | 2 +- gn3/auth/authentication/oauth2/models/oauth2token.py | 4 +++- gn3/auth/db.py | 8 -------- gn3/computations/partial_correlations.py | 7 +++---- gn3/db/case_attributes.py | 3 ++- gn3/db_utils.py | 1 - tests/integration/test_gemma.py | 2 +- tests/unit/auth/test_groups.py | 4 +++- tests/unit/auth/test_privileges.py | 8 +++++--- tests/unit/auth/test_resources.py | 20 ++++++++++++-------- tests/unit/auth/test_roles.py | 4 +++- tests/unit/computations/test_partial_correlations.py | 2 +- tests/unit/db/test_case_attributes.py | 8 +++++--- tests/unit/db/test_db.py | 1 - tests/unit/test_heatmaps.py | 2 +- 18 files changed, 48 insertions(+), 41 deletions(-) diff --git a/gn3/api/search.py b/gn3/api/search.py index aa844ee..ada845d 100644 --- a/gn3/api/search.py +++ b/gn3/api/search.py @@ -147,12 +147,14 @@ def parse_position(spec: str) -> tuple[Maybe[int], Maybe[int]]: point = apply_si_suffix(spec) return Just(max(0, point - width)), Just(point + width) +def to_megabases(val): + """Convert `val` to megabases.""" + return str(Decimal(val)/10**6) def parse_position_field(location_slot: int, query: bytes) -> xapian.Query: """Parse position and return a xapian query.""" start, end = parse_position(query.decode("utf-8")) # TODO: Convert the xapian index to use bases instead of megabases. - to_megabases = lambda x: str(Decimal(x)/10**6) return (xapian.NumberRangeProcessor(location_slot) (start.maybe("", to_megabases), end.maybe("", to_megabases))) # type: ignore @@ -176,7 +178,6 @@ def parse_location_field(species_query: xapian.Query, def make_query(interval: ChromosomalInterval) -> xapian.Query: # TODO: Convert the xapian index to use bases instead of megabases. - to_megabases = lambda x: str(Decimal(x)/10**6) return combine_queries(xapian.Query.OP_AND, species_query, xapian.Query(chromosome_prefix + interval.chromosome), diff --git a/gn3/auth/authentication/oauth2/endpoints/introspection.py b/gn3/auth/authentication/oauth2/endpoints/introspection.py index a567363..29ade6a 100644 --- a/gn3/auth/authentication/oauth2/endpoints/introspection.py +++ b/gn3/auth/authentication/oauth2/endpoints/introspection.py @@ -24,7 +24,7 @@ class IntrospectionEndpoint(_IntrospectionEndpoint): """Query the token.""" return _query_token(self, token_string, token_type_hint) - def introspect_token(self, token: OAuth2Token) -> dict:# pylint: disable=[no-self-use] + def introspect_token(self, token: OAuth2Token) -> dict: """Return the introspection information.""" url = urlparse(flask_request.url) return { @@ -43,6 +43,6 @@ class IntrospectionEndpoint(_IntrospectionEndpoint): "jti": token.token_id } - def check_permission(self, token, client, request):# pylint: disable=[unused-argument, no-self-use] + def check_permission(self, token, client, request):# pylint: disable=[unused-argument] """Check that the client has permission to introspect token.""" return client.client_type == "internal" diff --git a/gn3/auth/authentication/oauth2/endpoints/utilities.py b/gn3/auth/authentication/oauth2/endpoints/utilities.py index 299f151..b85940d 100644 --- a/gn3/auth/authentication/oauth2/endpoints/utilities.py +++ b/gn3/auth/authentication/oauth2/endpoints/utilities.py @@ -12,7 +12,9 @@ def query_token(# pylint: disable=[unused-argument] endpoint_object: Any, token_str: str, token_type_hint) -> Optional[ OAuth2Token]: """Retrieve the token from the database.""" - __identity__ = lambda val: val + def __identity__(val): + """Identity function.""" + return val token = Nothing with db.connection(current_app.config["AUTH_DB"]) as conn: if token_type_hint == "access_token": diff --git a/gn3/auth/authentication/oauth2/grants/authorisation_code_grant.py b/gn3/auth/authentication/oauth2/grants/authorisation_code_grant.py index f80d02e..fb8d436 100644 --- a/gn3/auth/authentication/oauth2/grants/authorisation_code_grant.py +++ b/gn3/auth/authentication/oauth2/grants/authorisation_code_grant.py @@ -41,7 +41,7 @@ class AuthorisationCodeGrant(grants.AuthorizationCodeGrant): """Retrieve the code from the database.""" return __query_authorization_code__(code, client) - def delete_authorization_code(self, authorization_code):# pylint: disable=[no-self-use] + def delete_authorization_code(self, authorization_code): """Delete the authorisation code.""" with db.connection(app.config["AUTH_DB"]) as conn: with db.cursor(conn) as cursor: diff --git a/gn3/auth/authentication/oauth2/models/oauth2token.py b/gn3/auth/authentication/oauth2/models/oauth2token.py index 72e20cc..981e730 100644 --- a/gn3/auth/authentication/oauth2/models/oauth2token.py +++ b/gn3/auth/authentication/oauth2/models/oauth2token.py @@ -51,7 +51,9 @@ class OAuth2Token(NamedTuple): return self.revoked def __token_from_resultset__(conn: db.DbConnection, rset) -> Maybe: - __identity__ = lambda val: val + def __identity__(val): + """Identity function.""" + return val try: the_user = user_by_id(conn, uuid.UUID(rset["user_id"])) except NotFoundError as _nfe: diff --git a/gn3/auth/db.py b/gn3/auth/db.py index 2ba6619..97f3982 100644 --- a/gn3/auth/db.py +++ b/gn3/auth/db.py @@ -10,40 +10,32 @@ class DbConnection(Protocol): """Type annotation for a generic database connection object.""" def cursor(self) -> Any: """A cursor object""" - ... def commit(self) -> Any: """Commit the transaction.""" - ... def rollback(self) -> Any: """Rollback the transaction.""" - ... class DbCursor(Protocol): """Type annotation for a generic database cursor object.""" def execute(self, *args, **kwargs) -> Any: """Execute a single query""" - ... def executemany(self, *args, **kwargs) -> Any: """ Execute parameterized SQL statement sql against all parameter sequences or mappings found in the sequence parameters. """ - ... def fetchone(self, *args, **kwargs): """Fetch single result if present, or `None`.""" - ... def fetchmany(self, *args, **kwargs): """Fetch many results if present or `None`.""" - ... def fetchall(self, *args, **kwargs): """Fetch all results if present or `None`.""" - ... @contextlib.contextmanager def connection(db_path: str, row_factory: Callable = sqlite3.Row) -> Iterator[DbConnection]: diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py index 9f02ad2..192ad7f 100644 --- a/gn3/computations/partial_correlations.py +++ b/gn3/computations/partial_correlations.py @@ -421,9 +421,8 @@ def literature_correlation_by_list( `web.webqtl.correlation.CorrelationPage.getLiteratureCorrelationByList` function in GeneNetwork1. """ - if any((lambda t: ( - bool(t.get("tissue_corr")) and - bool(t.get("tissue_p_value"))))(trait) + if any((bool(trait.get("tissue_corr")) and + bool(trait.get("tissue_p_value"))) for trait in trait_list): temporary_table_name = f"LITERATURE{random_string(8)}" query1 = ( @@ -491,7 +490,7 @@ def tissue_correlation_by_list( "tissue_p_value": result["p-val"] } - if any((lambda t: bool(t.get("l_corr")))(trait) for trait in trait_list): + if any(bool((trait)) for trait in trait_list): prim_trait_symbol_value_dict = fetch_gene_symbol_tissue_value_dict_for_trait( (primary_trait_symbol,), tissue_probeset_freeze_id, conn) if primary_trait_symbol.lower() in prim_trait_symbol_value_dict: diff --git a/gn3/db/case_attributes.py b/gn3/db/case_attributes.py index 1d82f00..0794c0b 100644 --- a/gn3/db/case_attributes.py +++ b/gn3/db/case_attributes.py @@ -90,7 +90,8 @@ def approve_case_attribute(conn: Any, case_attr_audit_id: int) -> int: elif diff_data.get("Deletion"): data = diff_data.get("Deletion") cursor.execute( - "DELETE FROM CaseAttribute " "WHERE Id = %s", + "DELETE FROM CaseAttribute " + "WHERE Id = %s", (data.get("id"),), ) # Modification diff --git a/gn3/db_utils.py b/gn3/db_utils.py index 7d6a445..8bf7be7 100644 --- a/gn3/db_utils.py +++ b/gn3/db_utils.py @@ -21,7 +21,6 @@ class Connection(Protocol): """Type Annotation for MySQLdb's connection object""" def cursor(self, *args) -> Any: """A cursor in which queries may be performed""" - ... @contextlib.contextmanager diff --git a/tests/integration/test_gemma.py b/tests/integration/test_gemma.py index d8c07f4..79ddaca 100644 --- a/tests/integration/test_gemma.py +++ b/tests/integration/test_gemma.py @@ -382,7 +382,7 @@ class GemmaAPITest(unittest.TestCase): "snps": "snpfile.txt", } mock_hash.return_value = "hash" - response = self.app.post(("/api/gemma/k-gwa-compute/" "my-token")) + response = self.app.post(("/api/gemma/k-gwa-compute/my-token")) mock_hash.assert_called_with([ ('/tmp/cache/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/' 'genotype.txt'), '/tmp/my-token/phenofile.txt', diff --git a/tests/unit/auth/test_groups.py b/tests/unit/auth/test_groups.py index 4824e14..bd62405 100644 --- a/tests/unit/auth/test_groups.py +++ b/tests/unit/auth/test_groups.py @@ -19,7 +19,9 @@ create_group_failure = { "message": "Unauthorised: Failed to create group." } -uuid_fn = lambda : UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") +def uuid_fn(): + """Return a specific UUID""" + return UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") GROUP = Group(UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), "TheTestGroup", {"group_description": "The test group"}) diff --git a/tests/unit/auth/test_privileges.py b/tests/unit/auth/test_privileges.py index 8395293..4894dfc 100644 --- a/tests/unit/auth/test_privileges.py +++ b/tests/unit/auth/test_privileges.py @@ -6,7 +6,9 @@ from gn3.auth.authorisation.privileges import Privilege, user_privileges from tests.unit.auth import conftest -SORT_KEY = lambda x: x.privilege_id +def sort_key(priv): + """Return the key to sort by""" + return priv.privilege_id PRIVILEGES = sorted( (Privilege("system:group:create-group", "Create a group"), @@ -29,7 +31,7 @@ PRIVILEGES = sorted( Privilege("group:role:edit-role", "edit/update an existing role"), Privilege("group:user:assign-role", "Assign a role to an existing user"), Privilege("group:role:delete-role", "Delete an existing role")), - key=SORT_KEY) + key=sort_key) @pytest.mark.unit_test @pytest.mark.parametrize( @@ -43,4 +45,4 @@ def test_user_privileges(auth_testdb_path, fxtr_users, user, expected):# pylint: """ with db.connection(auth_testdb_path) as conn: assert sorted( - user_privileges(conn, user), key=SORT_KEY) == expected + user_privileges(conn, user), key=sort_key) == expected diff --git a/tests/unit/auth/test_resources.py b/tests/unit/auth/test_resources.py index 2884add..a16dcb1 100644 --- a/tests/unit/auth/test_resources.py +++ b/tests/unit/auth/test_resources.py @@ -21,7 +21,9 @@ create_resource_failure = { "status": "error", "message": "Unauthorised: Could not create resource" } -uuid_fn = lambda : uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") +def uuid_fn(): + """UUID function for tests.""" + return uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") @pytest.mark.unit_test @pytest.mark.parametrize( @@ -71,7 +73,9 @@ def test_create_resource_raises_for_unauthorised_users( assert create_resource( conn, "test_resource", resource_category, user, False) == expected -SORTKEY = lambda resource: resource.resource_id +def sort_key(resource): + """Return the sort key for resources.""" + return resource.resource_id @pytest.mark.unit_test def test_public_resources(fxtr_resources): @@ -81,12 +85,12 @@ def test_public_resources(fxtr_resources): THEN: only list the resources that are public """ conn, _res = fxtr_resources - assert sorted(public_resources(conn), key=SORTKEY) == sorted(tuple( - res for res in conftest.TEST_RESOURCES if res.public), key=SORTKEY) + assert sorted(public_resources(conn), key=sort_key) == sorted(tuple( + res for res in conftest.TEST_RESOURCES if res.public), key=sort_key) PUBLIC_RESOURCES = sorted( {res.resource_id: res for res in conftest.TEST_RESOURCES_PUBLIC}.values(), - key=SORTKEY) + key=sort_key) @pytest.mark.unit_test @pytest.mark.parametrize( @@ -97,13 +101,13 @@ PUBLIC_RESOURCES = sorted( {res.resource_id: res for res in (conftest.TEST_RESOURCES_GROUP_01 + conftest.TEST_RESOURCES_PUBLIC)}.values(), - key=SORTKEY), + key=sort_key), sorted( {res.resource_id: res for res in ((conftest.TEST_RESOURCES_GROUP_01[1],) + conftest.TEST_RESOURCES_PUBLIC)}.values() , - key=SORTKEY), + key=sort_key), PUBLIC_RESOURCES, PUBLIC_RESOURCES)))) def test_user_resources(fxtr_group_user_roles, user, expected): """ @@ -114,4 +118,4 @@ def test_user_resources(fxtr_group_user_roles, user, expected): conn, *_others = fxtr_group_user_roles assert sorted( {res.resource_id: res for res in user_resources(conn, user) - }.values(), key=SORTKEY) == expected + }.values(), key=sort_key) == expected diff --git a/tests/unit/auth/test_roles.py b/tests/unit/auth/test_roles.py index 02fd9f7..3bb84a3 100644 --- a/tests/unit/auth/test_roles.py +++ b/tests/unit/auth/test_roles.py @@ -16,7 +16,9 @@ create_role_failure = { "message": "Unauthorised: Could not create role" } -uuid_fn = lambda : uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") +def uuid_fn(): + """UUID function for tests""" + return uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") PRIVILEGES = ( Privilege("group:resource:view-resource", diff --git a/tests/unit/computations/test_partial_correlations.py b/tests/unit/computations/test_partial_correlations.py index 20cd6bc..066c650 100644 --- a/tests/unit/computations/test_partial_correlations.py +++ b/tests/unit/computations/test_partial_correlations.py @@ -234,7 +234,7 @@ class TestPartialCorrelations(TestCase): tissue_correlation(primary, target, method) @pytest.mark.unit_test - def test_tissue_correlation(self): # pylint: disable=R0201 + def test_tissue_correlation(self): """ Test that the correct correlation values are computed for the given: - primary trait diff --git a/tests/unit/db/test_case_attributes.py b/tests/unit/db/test_case_attributes.py index 175249b..596f78a 100644 --- a/tests/unit/db/test_case_attributes.py +++ b/tests/unit/db/test_case_attributes.py @@ -140,7 +140,7 @@ def test_approve_deleting_case_attribute(mocker: MockFixture) -> None: "WHERE id = %s", (3,), ), - mocker.call("DELETE FROM CaseAttribute " "WHERE Id = %s", ("12",)), + mocker.call("DELETE FROM CaseAttribute WHERE Id = %s", ("12",)), mocker.call( "UPDATE caseattributes_audit SET " "status = 'approved' WHERE id = %s", @@ -183,14 +183,16 @@ def test_approve_modifying_case_attribute(mocker: MockFixture) -> None: (3,), ), mocker.call( - "UPDATE CaseAttribute SET " "Description = %s WHERE Id = %s", + "UPDATE CaseAttribute SET " + "Description = %s WHERE Id = %s", ( "Test", "12", ), ), mocker.call( - "UPDATE CaseAttribute SET " "Name = %s WHERE Id = %s", + "UPDATE CaseAttribute SET " + "Name = %s WHERE Id = %s", ( "Height (A)", "12", diff --git a/tests/unit/db/test_db.py b/tests/unit/db/test_db.py index 8ac468c..2b4151c 100644 --- a/tests/unit/db/test_db.py +++ b/tests/unit/db/test_db.py @@ -103,7 +103,6 @@ class TestCrudMethods(TestCase): (35, 'Rob')) @pytest.mark.unit_test - # pylint: disable=R0201 def test_probeset_called_with_right_columns(self): """Given a columns argument, test that the correct sql query is constructed""" diff --git a/tests/unit/test_heatmaps.py b/tests/unit/test_heatmaps.py index 8781d6f..3d4fc78 100644 --- a/tests/unit/test_heatmaps.py +++ b/tests/unit/test_heatmaps.py @@ -29,7 +29,7 @@ class TestHeatmap(TestCase): """Class for testing heatmap computation functions""" @pytest.mark.unit_test - def test_cluster_traits(self): # pylint: disable=R0201 + def test_cluster_traits(self): """ Test that the clustering is working as expected. """ -- cgit v1.2.3