Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ruff linting issue UP006: Use type instead of Type for type annotation #1555

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/forms/duration_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Callable, Mapping, Optional, Type
from typing import Callable, Mapping, Optional

from wtforms import Form

Expand Down Expand Up @@ -52,7 +52,7 @@ def data(self) -> Optional[dict[str, Optional[str]]]:

def get_duration_form(
answer: Mapping, error_messages: ErrorMessageType
) -> Type[DurationForm]:
) -> type[DurationForm]:
class CustomDurationForm(DurationForm):
mandatory = answer["mandatory"]
units = answer["units"]
Expand Down
4 changes: 2 additions & 2 deletions app/forms/field_handlers/number_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cached_property
from typing import Any, Type, Union
from typing import Any, Union

from wtforms import DecimalField, IntegerField
from wtforms.fields.core import UnboundField
Expand Down Expand Up @@ -58,7 +58,7 @@ def max_decimals(self) -> int:
@property
def _field_type(
self,
) -> Type[Union[DecimalFieldWithSeparator, IntegerFieldWithSeparator]]:
) -> type[Union[DecimalFieldWithSeparator, IntegerFieldWithSeparator]]:
return (
DecimalFieldWithSeparator
if self.max_decimals > 0
Expand Down
4 changes: 2 additions & 2 deletions app/forms/fields/date_field.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from functools import cached_property
from typing import Any, Callable, Sequence, Type
from typing import Any, Callable, Sequence

from werkzeug.datastructures import MultiDict
from wtforms import Form, FormField, StringField
Expand All @@ -11,7 +11,7 @@
logger = logging.getLogger(__name__)


def get_form_class(validators: Sequence[DateValidatorType]) -> Type[Form]:
def get_form_class(validators: Sequence[DateValidatorType]) -> type[Form]:
class DateForm(Form):
# Validation is only ever added to the 1 field that shows in all 3 variants
# This is to prevent an error message for each input box
Expand Down
4 changes: 2 additions & 2 deletions app/forms/fields/month_year_date_field.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from functools import cached_property
from typing import Any, Callable, Sequence, Type
from typing import Any, Callable, Sequence

from werkzeug.datastructures import MultiDict
from wtforms import Form, FormField, StringField
Expand All @@ -11,7 +11,7 @@
logger = logging.getLogger(__name__)


def get_form_class(validators: Sequence[DateValidatorType]) -> Type[Form]:
def get_form_class(validators: Sequence[DateValidatorType]) -> type[Form]:
class YearMonthDateForm(Form):
year = StringField(validators=validators)
month = StringField()
Expand Down
4 changes: 2 additions & 2 deletions app/forms/fields/year_date_field.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from functools import cached_property
from typing import Any, Callable, Sequence, Type
from typing import Any, Callable, Sequence

from werkzeug.datastructures import MultiDict
from wtforms import Form, FormField, StringField
Expand All @@ -11,7 +11,7 @@
logger = logging.getLogger(__name__)


def get_form_class(validators: Sequence[DateValidatorType]) -> Type[Form]:
def get_form_class(validators: Sequence[DateValidatorType]) -> type[Form]:
class YearDateForm(Form):
year = StringField(validators=validators)

Expand Down
4 changes: 2 additions & 2 deletions app/forms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from datetime import datetime, timezone
from decimal import Decimal, InvalidOperation
from typing import TYPE_CHECKING, Iterable, List, Mapping, Optional, Sequence, Union
from typing import TYPE_CHECKING, Iterable, Mapping, Optional, Sequence, Union

import flask_babel
from babel import numbers
Expand Down Expand Up @@ -417,7 +417,7 @@ def __init__(
def __call__(
self,
form: QuestionnaireForm,
conditions: List[str],
conditions: list[str],
total: Decimal | int,
target_total: Decimal | float | int,
decimal_limit: int | None = None,
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/template_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import cached_property, lru_cache
from typing import Any, Type
from typing import Any

from flask import current_app
from flask import render_template as flask_render_template
Expand Down Expand Up @@ -171,7 +171,7 @@ def _footer_warning(self) -> str | None:
def survey_config_mapping(
*, theme: SurveyType, language: str, base_url: str, schema: QuestionnaireSchema
) -> SurveyConfig:
survey_type_to_config: dict[SurveyType, Type[SurveyConfig]] = {
survey_type_to_config: dict[SurveyType, type[SurveyConfig]] = {
SurveyType.DEFAULT: BusinessSurveyConfig,
SurveyType.BUSINESS: BusinessSurveyConfig,
SurveyType.HEALTH: SocialSurveyConfig,
Expand Down
3 changes: 1 addition & 2 deletions app/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from copy import deepcopy
from typing import Dict
from uuid import uuid4

import boto3
Expand Down Expand Up @@ -256,7 +255,7 @@ def setup_jinja_env(application):
application.jinja_env.add_extension("jinja2.ext.do")


def _add_cdn_url_to_csp_policy(cdn_url) -> Dict:
def _add_cdn_url_to_csp_policy(cdn_url) -> dict:
csp_policy = deepcopy(CSP_POLICY)
for directive in csp_policy:
if directive not in ["frame-src", "object-src", "base-uri"]:
Expand Down
4 changes: 2 additions & 2 deletions app/storage/datastore.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Type
from typing import Optional

from google.api_core.retry import Retry
from google.cloud import datastore
Expand Down Expand Up @@ -36,7 +36,7 @@ def put(self, model: ModelTypes, overwrite: bool = True) -> bool:
return True

@Retry()
def get(self, model_type: Type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
def get(self, model_type: type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
storage_model = StorageModel(model_type=model_type)
key = self.client.key(storage_model.table_name, key_value)

Expand Down
4 changes: 2 additions & 2 deletions app/storage/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Type
from typing import Optional

import boto3
from botocore.exceptions import ClientError
Expand Down Expand Up @@ -33,7 +33,7 @@ def put(self, model: ModelTypes, overwrite: bool = True) -> bool:

raise # pragma: no cover

def get(self, model_type: Type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
def get(self, model_type: type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
storage_model = StorageModel(model_type=model_type)
table = self.client.Table(storage_model.table_name)
key = {storage_model.key_field: key_value}
Expand Down
4 changes: 2 additions & 2 deletions app/storage/redis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timezone
from typing import Optional, Type
from typing import Optional

import redis
from redis.exceptions import ConnectionError as RedisConnectionError
Expand Down Expand Up @@ -56,7 +56,7 @@ def put(self, model: ModelTypes, overwrite: bool = True) -> bool:

return True

def get(self, model_type: Type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
def get(self, model_type: type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
storage_model = StorageModel(model_type=model_type)
try:
item = self.client.get(key_value)
Expand Down
10 changes: 5 additions & 5 deletions app/storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from abc import ABC, abstractmethod
from functools import cached_property
from typing import Any, Optional, Type, TypedDict, Union
from typing import Any, Optional, TypedDict, Union

from flask import current_app
from google.cloud import datastore
Expand All @@ -23,13 +23,13 @@
class TableConfig(TypedDict, total=False):
key_field: str
table_name_key: str
schema: Type[ModelSchemaTypes]
schema: type[ModelSchemaTypes]
expiry_field: str
index_fields: list[str]


class StorageModel:
TABLE_CONFIG_BY_TYPE: dict[Type[ModelTypes], TableConfig] = {
TABLE_CONFIG_BY_TYPE: dict[type[ModelTypes], TableConfig] = {
app_models.QuestionnaireState: {
"key_field": "user_id",
"table_name_key": "EQ_QUESTIONNAIRE_STATE_TABLE_NAME",
Expand All @@ -51,7 +51,7 @@ class StorageModel:
},
}

def __init__(self, model_type: Type[ModelTypes]) -> None:
def __init__(self, model_type: type[ModelTypes]) -> None:
self._model_type = model_type

if self._model_type not in self.TABLE_CONFIG_BY_TYPE:
Expand Down Expand Up @@ -95,7 +95,7 @@ def put(self, model: ModelTypes, overwrite: bool = True) -> bool:
pass # pragma: no cover

@abstractmethod
def get(self, model_type: Type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
def get(self, model_type: type[ModelTypes], key_value: str) -> Optional[ModelTypes]:
pass # pragma: no cover

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions app/views/contexts/calculated_summary_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Iterable, Mapping, Tuple
from typing import Callable, Iterable, Mapping

from werkzeug.datastructures import ImmutableDict

Expand Down Expand Up @@ -246,7 +246,7 @@ def _get_formatted_total(

return self._format_total(answer_format=answer_format, total=calculated_total)

def _get_answer_format(self, groups: Iterable[Mapping]) -> Tuple[dict, list]:
def _get_answer_format(self, groups: Iterable[Mapping]) -> tuple[dict, list]:
values_to_calculate: list = []
answer_format: dict = {"type": None}
decimal_limits: list[int] = []
Expand Down
4 changes: 2 additions & 2 deletions app/views/contexts/summary/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterable, Mapping, Type
from typing import Iterable, Mapping

from werkzeug.datastructures import ImmutableDict

Expand Down Expand Up @@ -86,7 +86,7 @@ def _build_blocks_and_links(
if parent_list_collector_block_id not in routing_path_block_ids:
continue

list_collector_block_class: Type[
list_collector_block_class: type[
ListCollectorBlock | ListCollectorContentBlock
] = (
ListCollectorBlock
Expand Down
4 changes: 1 addition & 3 deletions app/views/handlers/calculation_summary.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import Type

from app.views.contexts import GrandCalculatedSummaryContext
from app.views.contexts.calculated_summary_context import CalculatedSummaryContext
from app.views.handlers.content import Content


class _SummaryWithCalculation(Content):
summary_class: Type[CalculatedSummaryContext] | Type[GrandCalculatedSummaryContext]
summary_class: type[CalculatedSummaryContext] | type[GrandCalculatedSummaryContext]

def get_context(self) -> dict[str, dict]:
summary_context = self.summary_class(
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ extend-ignore = [
"UP018", # Unnecessary {literal_type} call (rewrite as a literal)
"UP015", # Unnecessary open mode parameters
"UP007", # Use `X | Y` for type annotations
"UP006", # Use `type` instead of `Type` for type annotation
"UP009", # UTF-8 encoding declaration is unnecessary
"UP017", # Use `datetime.UTC` alias
"UP033", # Use @functools.cache instead of @functools.lru_cache(maxsize=None)
Expand Down
4 changes: 1 addition & 3 deletions tests/app/helpers/test_template_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Type

import pytest
from flask import Flask, current_app
from flask import session as cookie_session
Expand Down Expand Up @@ -893,7 +891,7 @@ def test_get_survey_config(
],
)
def test_survey_config_base_url_provided_used_in_links(
app: Flask, survey_config_type: Type[SurveyConfig], base_url: str
app: Flask, survey_config_type: type[SurveyConfig], base_url: str
):
with app.app_context():
result = survey_config_type(base_url=base_url)
Expand Down