From 3d3df8f0a39a9094fc661c8a7081dc9132fdaaf1 Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Tue, 20 Aug 2024 12:22:36 +0530 Subject: [PATCH] Linter and type fixes --- src/coaster/assets.py | 2 +- src/coaster/auth.py | 2 +- src/coaster/compat.py | 9 +-------- src/coaster/sqlalchemy/mixins.py | 4 +--- src/coaster/sqlalchemy/roles.py | 4 +--- src/coaster/utils/markdown.py | 6 +----- src/coaster/utils/text.py | 3 ++- tests/coaster_tests/conftest.py | 2 +- .../sqlalchemy_annotations_test.py | 19 ++++--------------- 9 files changed, 13 insertions(+), 38 deletions(-) diff --git a/src/coaster/assets.py b/src/coaster/assets.py index e97f6049..6edb7545 100644 --- a/src/coaster/assets.py +++ b/src/coaster/assets.py @@ -195,7 +195,7 @@ def _require_recursive(self, *namespecs: str) -> list[tuple[str, Version, str]]: if provided not in asset_versions: asset_versions[provided] = version for req_name, req_version, _req_bundle in req_bundles: - asset_versions[req_name] = req_version + asset_versions[req_name] = req_version # noqa: PERF403 if bundle is not None: bundles.append((name, version, bundle)) else: diff --git a/src/coaster/auth.py b/src/coaster/auth.py index 24a58273..2ebfd3eb 100644 --- a/src/coaster/auth.py +++ b/src/coaster/auth.py @@ -306,7 +306,7 @@ def __call__(self) -> CurrentAuth: @classmethod def proxy(cls, subcls: type[_CurrentAuthType_co]) -> _CurrentAuthType_co: """Create a local proxy using a specific subclass of :class:`CurrentAuth`.""" - return cast(_CurrentAuthType_co, LocalProxy(cls(subcls))) + return cast(_CurrentAuthType_co, LocalProxy(cls(subcls))) # type: ignore[arg-type] #: A proxy object that hosts state for user authentication, attempting to load diff --git a/src/coaster/compat.py b/src/coaster/compat.py index e828a5a2..20eacebb 100644 --- a/src/coaster/compat.py +++ b/src/coaster/compat.py @@ -447,11 +447,6 @@ async def get_data( self, cache: bool, as_text: Literal[True], parse_form_data: bool ) -> str: ... - @overload - async def get_data( - self, cache: bool = True, as_text: bool = False, parse_form_data: bool = False - ) -> AnyStr: ... - async def get_data( self, cache: bool = True, as_text: bool = False, parse_form_data: bool = False ) -> AnyStr: @@ -593,8 +588,6 @@ def check_return(*args: _P.args, **kwargs: _P.kwargs) -> _R_co: result = func(*args, **kwargs) if isawaitable(result): return sync_await(result) - # The typeguard for `isawaitable` doesn't narrow in the negative context, so we - # need a type-ignore here: - return result # type: ignore[return-value] + return result return check_return diff --git a/src/coaster/sqlalchemy/mixins.py b/src/coaster/sqlalchemy/mixins.py index b6636ae8..1d6e4c1e 100644 --- a/src/coaster/sqlalchemy/mixins.py +++ b/src/coaster/sqlalchemy/mixins.py @@ -173,9 +173,7 @@ def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None: and issubclass(origin_base, IdMixin) and PkeyType in origin_base.__parameters__ # type: ignore[misc] ): - pkey_type = get_args(base)[ - origin_base.__parameters__.index(PkeyType) # type: ignore[misc] - ] + pkey_type = get_args(base)[origin_base.__parameters__.index(PkeyType)] if pkey_type is int: if ( '__uuid_primary_key__' in cls.__dict__ diff --git a/src/coaster/sqlalchemy/roles.py b/src/coaster/sqlalchemy/roles.py index fb080eb2..3369df1b 100644 --- a/src/coaster/sqlalchemy/roles.py +++ b/src/coaster/sqlalchemy/roles.py @@ -406,9 +406,7 @@ def __subclasshook__(cls, subcls: type[Any]) -> bool: """Check if a class implements the RoleGrantABC protocol.""" if cls is RoleGrantABC: # Don't use getattr because that'll trigger descriptor __get__ protocol - if any('offered_roles' in b.__dict__ for b in subcls.__mro__): - return True - return False + return bool(any('offered_roles' in b.__dict__ for b in subcls.__mro__)) return NotImplemented # pragma: no cover diff --git a/src/coaster/utils/markdown.py b/src/coaster/utils/markdown.py index 8f62a7a2..95d8aba4 100644 --- a/src/coaster/utils/markdown.py +++ b/src/coaster/utils/markdown.py @@ -230,10 +230,6 @@ def markdown( ).convert(text) if linkify: output = linkify_processor( - output, - # types-bleach specifies `callbacks: Iterable[_Callback]`, but that - # _Callback has an incorrect definition for the `attrs` parameter - callbacks=LINKIFY_CALLBACKS, # type: ignore[arg-type] - skip_tags=LINKIFY_SKIP_TAGS, + output, callbacks=LINKIFY_CALLBACKS, skip_tags=LINKIFY_SKIP_TAGS ) return Markup(output) diff --git a/src/coaster/utils/text.py b/src/coaster/utils/text.py index 749d58f3..e21cfe69 100644 --- a/src/coaster/utils/text.py +++ b/src/coaster/utils/text.py @@ -4,6 +4,7 @@ import re import string +from collections.abc import Callable from functools import partial from html import unescape from typing import Optional, Union @@ -139,7 +140,7 @@ def dont_linkify_filenames( return attrs -LINKIFY_CALLBACKS = [*DEFAULT_CALLBACKS, dont_linkify_filenames] # type: ignore[list-item] +LINKIFY_CALLBACKS: list[Callable] = [*DEFAULT_CALLBACKS, dont_linkify_filenames] def sanitize_html( diff --git a/tests/coaster_tests/conftest.py b/tests/coaster_tests/conftest.py index 3fec6734..967aae2a 100644 --- a/tests/coaster_tests/conftest.py +++ b/tests/coaster_tests/conftest.py @@ -118,7 +118,7 @@ def task_factory( def new_event_loop(self) -> asyncio.AbstractEventLoop: loop = super().new_event_loop() - loop.set_task_factory(self.task_factory) + loop.set_task_factory(self.task_factory) # type: ignore[arg-type] return loop diff --git a/tests/coaster_tests/sqlalchemy_annotations_test.py b/tests/coaster_tests/sqlalchemy_annotations_test.py index 8d6bbd2f..ea93cede 100644 --- a/tests/coaster_tests/sqlalchemy_annotations_test.py +++ b/tests/coaster_tests/sqlalchemy_annotations_test.py @@ -138,21 +138,15 @@ def test_annotation_in_annotations() -> None: for model in (IdOnly, IdUuid, UuidOnly): assert issubclass(model, ModelBase) for annotation in (immutable, cached): - assert ( - annotation.__name__ in model.__column_annotations__ # type: ignore[attr-defined] - ) + assert annotation.__name__ in model.__column_annotations__ def test_attr_in_annotations() -> None: """Annotated attributes were discovered and documented.""" for model in (IdOnly, IdUuid, UuidOnly): assert issubclass(model, ModelBase) - assert ( - 'is_immutable' in model.__column_annotations__['immutable'] # type: ignore[attr-defined] - ) - assert ( - 'is_cached' in model.__column_annotations__['cached'] # type: ignore[attr-defined] - ) + assert 'is_immutable' in model.__column_annotations__['immutable'] + assert 'is_cached' in model.__column_annotations__['cached'] def test_base_attrs_in_annotations() -> None: @@ -160,12 +154,7 @@ def test_base_attrs_in_annotations() -> None: for model in (IdOnly, IdUuid, UuidOnly): assert issubclass(model, ModelBase) for attr in ('created_at', 'id'): - assert ( - attr - in model.__column_annotations__[ # type: ignore[attr-defined] - 'immutable' - ] - ) + assert attr in model.__column_annotations__['immutable'] assert 'uuid' in IdUuid.__column_annotations__['immutable']