Skip to content

Commit

Permalink
churn: pyupgrade pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Feb 13, 2024
1 parent 8e420e0 commit f58f689
Show file tree
Hide file tree
Showing 54 changed files with 250 additions and 264 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 24.1.1
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
Expand All @@ -32,6 +32,11 @@ repos:
hooks:
- id: mypy
additional_dependencies: [types-requests, types-aiofiles]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py310-plus"]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.6
hooks:
Expand Down
4 changes: 2 additions & 2 deletions otterdog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import importlib.metadata
import sys
import traceback
from typing import Any, Optional
from typing import Any

import click
from click.shell_completion import CompletionItem
Expand Down Expand Up @@ -43,7 +43,7 @@

_CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"], max_content_width=120)

_CONFIG: Optional[OtterdogConfig] = None
_CONFIG: OtterdogConfig | None = None


def complete_organizations(ctx, param, incomplete):
Expand Down
6 changes: 3 additions & 3 deletions otterdog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import json
import os
import re
from typing import Any, Optional
from typing import Any

import jq # type: ignore

Expand Down Expand Up @@ -121,7 +121,7 @@ def of(


class OtterdogConfig:
def __init__(self, config_file: str, local_mode: bool, working_dir: Optional[str] = None):
def __init__(self, config_file: str, local_mode: bool, working_dir: str | None = None):
if not os.path.exists(config_file):
raise RuntimeError(f"configuration file '{config_file}' not found")

Expand Down Expand Up @@ -185,7 +185,7 @@ def organization_configs(self) -> dict[str, OrganizationConfig]:
def organization_names(self) -> list[str]:
return list(map(lambda config: config.name, self._organizations))

def get_project_name(self, github_id: str) -> Optional[str]:
def get_project_name(self, github_id: str) -> str | None:
organization = self._organizations_map.get(github_id)
if organization is not None:
return organization.name
Expand Down
14 changes: 7 additions & 7 deletions otterdog/credentials/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import dataclasses
import time
from abc import abstractmethod
from typing import Any, Optional, Protocol
from typing import Any, Protocol

import mintotp # type: ignore

Expand All @@ -22,12 +22,12 @@ class Credentials:
A simple data class to hold credential information to access GitHub.
"""

_username: Optional[str]
_password: Optional[str]
_totp_secret: Optional[str]
_github_token: Optional[str]
_username: str | None
_password: str | None
_totp_secret: str | None
_github_token: str | None

_last_totp: Optional[str] = None
_last_totp: str | None = None

@property
def username(self) -> str:
Expand Down Expand Up @@ -67,7 +67,7 @@ def github_token(self) -> str:
return self._github_token

def __str__(self) -> str:
return "Credentials(username={})".format(self.username)
return f"Credentials(username={self.username})"


class CredentialProvider(Protocol):
Expand Down
37 changes: 19 additions & 18 deletions otterdog/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import dataclasses
import os
from abc import ABC, abstractmethod
from collections.abc import Callable, Iterator, Sequence
from enum import Enum
from typing import Any, Callable, Iterator, Optional, Protocol, Sequence, TypeVar, cast
from typing import Any, Protocol, TypeVar, cast

from jsonbender import bend # type: ignore

Expand Down Expand Up @@ -41,7 +42,7 @@ class FailureType(Enum):


@dataclasses.dataclass
class ValidationContext(object):
class ValidationContext:
root_object: Any
template_dir: str
validation_failures: list[tuple[FailureType, str]] = dataclasses.field(default_factory=list)
Expand Down Expand Up @@ -71,22 +72,22 @@ async def __call__(self, patch: LivePatch, org_id: str, provider: GitHubProvider
@dataclasses.dataclass(frozen=True)
class LivePatch:
patch_type: LivePatchType
expected_object: Optional[ModelObject]
current_object: Optional[ModelObject]
changes: Optional[dict[str, Change]]
parent_object: Optional[ModelObject]
expected_object: ModelObject | None
current_object: ModelObject | None
changes: dict[str, Change] | None
parent_object: ModelObject | None
forced_update: bool
fn: LivePatchApplyFn

@classmethod
def of_addition(
cls, expected_object: ModelObject, parent_object: Optional[ModelObject], fn: LivePatchApplyFn
cls, expected_object: ModelObject, parent_object: ModelObject | None, fn: LivePatchApplyFn
) -> LivePatch:
return LivePatch(LivePatchType.ADD, expected_object, None, None, parent_object, False, fn)

@classmethod
def of_deletion(
cls, current_object: ModelObject, parent_object: Optional[ModelObject], fn: LivePatchApplyFn
cls, current_object: ModelObject, parent_object: ModelObject | None, fn: LivePatchApplyFn
) -> LivePatch:
return LivePatch(LivePatchType.REMOVE, None, current_object, None, parent_object, False, fn)

Expand All @@ -96,7 +97,7 @@ def of_changes(
expected_object: ModelObject,
current_object: ModelObject,
changes: dict[str, Change],
parent_object: Optional[ModelObject],
parent_object: ModelObject | None,
forced_update: bool,
fn: LivePatchApplyFn,
) -> LivePatch:
Expand Down Expand Up @@ -127,13 +128,13 @@ def __repr__(self) -> str:


@dataclasses.dataclass
class PatchContext(object):
class PatchContext:
org_id: str
org_settings: ModelObject


@dataclasses.dataclass
class LivePatchContext(object):
class LivePatchContext:
org_id: str
update_webhooks: bool
update_secrets: bool
Expand Down Expand Up @@ -305,7 +306,7 @@ def is_nested_model(field: dataclasses.Field) -> bool:
def get_model_objects(self) -> Iterator[tuple[ModelObject, ModelObject]]:
yield from []

def get_model_header(self, parent_object: Optional[ModelObject] = None) -> str:
def get_model_header(self, parent_object: ModelObject | None = None) -> str:
header = style(self.model_object_name, bright=True)

if self.is_keyed():
Expand Down Expand Up @@ -381,7 +382,7 @@ def include_for_live_patch(self) -> bool:
"""
return True

def include_existing_object_for_live_patch(self, org_id: str, parent_object: Optional[ModelObject]) -> bool:
def include_existing_object_for_live_patch(self, org_id: str, parent_object: ModelObject | None) -> bool:
"""
Indicates if this live ModelObject should be considered when generating a live patch.
Expand Down Expand Up @@ -446,7 +447,7 @@ def copy_secrets(self, other_object: ModelObject) -> None:
pass

@abstractmethod
def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> Optional[str]: ...
def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> str | None: ...

def to_jsonnet(
self,
Expand All @@ -472,9 +473,9 @@ def to_jsonnet(
@classmethod
def generate_live_patch(
cls,
expected_object: Optional[ModelObject],
current_object: Optional[ModelObject],
parent_object: Optional[ModelObject],
expected_object: ModelObject | None,
current_object: ModelObject | None,
parent_object: ModelObject | None,
context: LivePatchContext,
handler: LivePatchHandler,
) -> None:
Expand Down Expand Up @@ -510,7 +511,7 @@ def generate_live_patch_of_list(
cls,
expected_objects: Sequence[MT],
current_objects: Sequence[MT],
parent_object: Optional[MT],
parent_object: MT | None,
context: LivePatchContext,
handler: LivePatchHandler,
) -> None:
Expand Down
6 changes: 3 additions & 3 deletions otterdog/models/branch_protection_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import dataclasses
import re
from typing import Any, Optional, cast
from typing import Any, cast

from jsonbender import Forall, K, OptionalS, S, bend # type: ignore

Expand Down Expand Up @@ -50,7 +50,7 @@ class BranchProtectionRule(ModelObject):
requires_pull_request: bool
# the following settings are only taken into account
# when requires_pull_request is True
required_approving_review_count: Optional[int]
required_approving_review_count: int | None
dismisses_stale_reviews: bool
requires_code_owner_reviews: bool
require_last_push_approval: bool
Expand Down Expand Up @@ -391,7 +391,7 @@ async def get_mapping_to_provider(

return mapping

def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> Optional[str]:
def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> str | None:
return f"orgs.{jsonnet_config.create_branch_protection_rule}"

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions otterdog/models/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from __future__ import annotations

import dataclasses
from typing import Any, Optional
from typing import Any

from jsonbender import F, Filter, Forall, If, K, OptionalS, S, bend # type: ignore

Expand Down Expand Up @@ -79,7 +79,7 @@ def include_field_for_diff_computation(self, field: dataclasses.Field) -> bool:
def include_field_for_patch_computation(self, field: dataclasses.Field) -> bool:
return True

def include_existing_object_for_live_patch(self, org_id: str, parent_object: Optional[ModelObject]) -> bool:
def include_existing_object_for_live_patch(self, org_id: str, parent_object: ModelObject | None) -> bool:
from .repository import Repository

assert isinstance(parent_object, Repository)
Expand Down Expand Up @@ -193,7 +193,7 @@ async def get_mapping_to_provider(

return mapping

def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> Optional[str]:
def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> str | None:
return f"orgs.{jsonnet_config.create_environment}"

@classmethod
Expand Down
17 changes: 9 additions & 8 deletions otterdog/models/github_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import dataclasses
import json
import os
from collections.abc import Callable, Iterator
from datetime import datetime
from io import StringIO
from typing import Any, Callable, Iterator, Optional
from typing import Any

import jsonschema
from importlib_resources import as_file, files
Expand Down Expand Up @@ -77,7 +78,7 @@ def secrets_resolved(self) -> bool:
def add_webhook(self, webhook: OrganizationWebhook) -> None:
self.webhooks.append(webhook)

def get_webhook(self, url: str) -> Optional[OrganizationWebhook]:
def get_webhook(self, url: str) -> OrganizationWebhook | None:
return next(filter(lambda x: x.url == url, self.webhooks), None) # type: ignore

def set_webhooks(self, webhooks: list[OrganizationWebhook]) -> None:
Expand All @@ -86,7 +87,7 @@ def set_webhooks(self, webhooks: list[OrganizationWebhook]) -> None:
def add_secret(self, secret: OrganizationSecret) -> None:
self.secrets.append(secret)

def get_secret(self, name: str) -> Optional[OrganizationSecret]:
def get_secret(self, name: str) -> OrganizationSecret | None:
return next(filter(lambda x: x.name == name, self.secrets), None) # type: ignore

def set_secrets(self, secrets: list[OrganizationSecret]) -> None:
Expand All @@ -95,7 +96,7 @@ def set_secrets(self, secrets: list[OrganizationSecret]) -> None:
def add_variable(self, variable: OrganizationVariable) -> None:
self.variables.append(variable)

def get_variable(self, name: str) -> Optional[OrganizationVariable]:
def get_variable(self, name: str) -> OrganizationVariable | None:
return next(filter(lambda x: x.name == name, self.variables), None) # type: ignore

def set_variables(self, variables: list[OrganizationVariable]) -> None:
Expand All @@ -104,7 +105,7 @@ def set_variables(self, variables: list[OrganizationVariable]) -> None:
def add_repository(self, repo: Repository) -> None:
self.repositories.append(repo)

def get_repository(self, repo_name: str) -> Optional[Repository]:
def get_repository(self, repo_name: str) -> Repository | None:
return next(filter(lambda x: x.name == repo_name, self.repositories), None) # type: ignore

def set_repositories(self, repos: list[Repository]) -> None:
Expand Down Expand Up @@ -132,7 +133,7 @@ def _validate_org_config(data: dict[str, Any]) -> None:
resolver = jsonschema.validators.RefResolver(base_uri=f"{schema_root}/", referrer=data)
jsonschema.validate(instance=data, schema=_ORG_SCHEMA, resolver=resolver)

def get_model_objects(self) -> Iterator[tuple[ModelObject, Optional[ModelObject]]]:
def get_model_objects(self) -> Iterator[tuple[ModelObject, ModelObject | None]]:
yield self.settings, None
yield from self.settings.get_model_objects()

Expand Down Expand Up @@ -338,7 +339,7 @@ async def load_from_provider(
jsonnet_config: JsonnetConfig,
provider: GitHubProvider,
no_web_ui: bool = False,
printer: Optional[IndentingPrinter] = None,
printer: IndentingPrinter | None = None,
) -> GitHubOrganization:
start = datetime.now()
if printer is not None and is_info_enabled():
Expand Down Expand Up @@ -514,7 +515,7 @@ async def _process_single_repo(


async def _load_repos_from_provider(
github_id: str, provider: GitHubProvider, jsonnet_config: JsonnetConfig, printer: Optional[IndentingPrinter] = None
github_id: str, provider: GitHubProvider, jsonnet_config: JsonnetConfig, printer: IndentingPrinter | None = None
) -> list[Repository]:
start = datetime.now()
if printer is not None and is_info_enabled():
Expand Down
4 changes: 2 additions & 2 deletions otterdog/models/organization_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from __future__ import annotations

import dataclasses
from typing import Any, Optional, cast
from typing import Any, cast

from jsonbender import Forall, If, K, OptionalS, S, bend # type: ignore

Expand Down Expand Up @@ -102,7 +102,7 @@ async def get_mapping_to_provider(

return mapping

def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> Optional[str]:
def get_jsonnet_template_function(self, jsonnet_config: JsonnetConfig, extend: bool) -> str | None:
return f"orgs.{jsonnet_config.create_org_secret}"

@classmethod
Expand Down
Loading

0 comments on commit f58f689

Please sign in to comment.