Skip to content

Commit

Permalink
Upgrade syntax with pyupgade in 3.11 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan committed Dec 12, 2024
1 parent d24171f commit ae7e432
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions mreg_cli/api/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ def patch(self, fields: dict[str, Any], validate: bool = True) -> Self:
:param fields: The values to patch.
:param validate: Whether to validate the patched object.
:returns: The object refetched from the server.
"""
patch(self.endpoint().with_id(self.id_for_endpoint()), **fields)
new_object = self.refetch()
Expand Down
2 changes: 1 addition & 1 deletion mreg_cli/api/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def clean_timestamp(self) -> str:
"""Clean up the timestamp for output."""
return self.timestamp.strftime("%Y-%m-%d %H:%M:%S")

def msg(self, basename: str) -> str:
def msg(self, basename: str) -> str: # noqa: ARG002 # arg might be used in the future
"""Attempt to make a history item human readable."""
msg = ""
action = self.action
Expand Down
18 changes: 9 additions & 9 deletions mreg_cli/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from datetime import date, datetime
from functools import cached_property
from typing import Any, Callable, ClassVar, Iterable, List, Literal, Self, cast, overload
from typing import Any, Callable, ClassVar, Iterable, Literal, Self, cast, overload

from pydantic import (
AliasChoices,
Expand Down Expand Up @@ -3570,7 +3570,7 @@ class UserPermission(BaseModel):
group: str
range: str
regex: str
labels: List[str]
labels: list[str]

# NOTE: _needs_ to be a computed field in order to use it in
# OutputManager.add_formatted_table, since we dump the model to a dict
Expand Down Expand Up @@ -3617,7 +3617,7 @@ def endpoint(cls) -> str:
return Endpoint.MetaVersion

@classmethod
def fetch(cls, ignore_errors: bool = True) -> "ServerVersion":
def fetch(cls, ignore_errors: bool = True) -> ServerVersion:
"""Fetch the server version from the endpoint.
:param ignore_errors: Whether to ignore errors.
Expand Down Expand Up @@ -3649,15 +3649,15 @@ class Library(BaseModel):
class ServerLibraries(BaseModel):
"""Model for server libraries metadata."""

libraries: List[Library]
libraries: list[Library]

@classmethod
def endpoint(cls) -> str:
"""Return the endpoint for the class."""
return Endpoint.MetaLibraries

@classmethod
def fetch(cls, ignore_errors: bool = True) -> "ServerLibraries":
def fetch(cls, ignore_errors: bool = True) -> ServerLibraries:
"""Fetch the server libraries from the endpoint.
:param ignore_errors: Whether to ignore errors.
Expand All @@ -3667,7 +3667,7 @@ def fetch(cls, ignore_errors: bool = True) -> "ServerLibraries":
"""
try:
response = get(cls.endpoint())
libraries: List[Library] = []
libraries: list[Library] = []

for name, version in response.json().items():
libraries.append(Library(name=name, version=version))
Expand All @@ -3694,16 +3694,16 @@ class UserInfo(BaseModel):
username: str
django_status: UserDjangoStatus
mreg_status: UserMregStatus
groups: List[str]
permissions: List[UserPermission]
groups: list[str]
permissions: list[UserPermission]

@classmethod
def endpoint(cls) -> str:
"""Return the endpoint for the class."""
return Endpoint.MetaUser

@classmethod
def fetch(cls, ignore_errors: bool = True, user: str | None = None) -> "UserInfo":
def fetch(cls, ignore_errors: bool = True, user: str | None = None) -> UserInfo:
"""Fetch the user information from the endpoint.
:param ignore_errors: Whether to ignore errors.
Expand Down
4 changes: 2 additions & 2 deletions mreg_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
# which lets the type checker understand what kind of classes we are
# trying to instantiate.
class BaseCommandSubclass(Protocol): # noqa: D101 (undocumented-public-class)
def __init__(self, cli: "Command") -> None: ... # noqa: D107 (undocumented-public-init)
def __init__(self, cli: Command) -> None: ... # noqa: D107 (undocumented-public-init)
def register_all_commands(self) -> None: ... # noqa: D102 (undocumented-public-method)


Expand Down Expand Up @@ -305,7 +305,7 @@ def process_command_line(self, line: str) -> None:
command(cli).register_all_commands()


def source(files: list[str], ignore_errors: bool, verbose: bool) -> Generator[str, None, None]:
def source(files: list[str], ignore_errors: bool, verbose: bool) -> Generator[str]:
"""Read commands from one or more source files and yield them.
:param files: List of file paths to read commands from.
Expand Down
2 changes: 1 addition & 1 deletion mreg_cli/commands/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def logging_status(_: argparse.Namespace):
file = MregCliConfig().get_default_logfile()

lines_in_logfile = 0
with open(file, "r") as f:
with open(file) as f:
lines_in_logfile = sum(1 for _ in f)

filesize = sizeof_fmt(os.path.getsize(file))
Expand Down
10 changes: 6 additions & 4 deletions mreg_cli/prompt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Prompt customization for the CLI."""

from __future__ import annotations

import argparse
import functools
import logging
import re
from typing import NamedTuple, Optional
from typing import NamedTuple

from prompt_toolkit import HTML

Expand All @@ -18,9 +20,9 @@ class ConnectionInfo(NamedTuple):
"""Connection information for a server."""

protocol: str
host: Optional[str]
tld: Optional[str]
port: Optional[int]
host: str | None
tld: str | None
port: int | None


@functools.lru_cache()
Expand Down
6 changes: 3 additions & 3 deletions mreg_cli/tokenfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import os
import sys
from typing import ClassVar, Optional, Self
from typing import ClassVar, Self

from pydantic import BaseModel, ValidationError

Expand Down Expand Up @@ -57,7 +57,7 @@ def save(self) -> None:
def load(cls) -> Self:
"""Load tokens from a JSON file, returning a new instance of TokenFile."""
try:
with open(cls.tokens_path, "r") as file:
with open(cls.tokens_path) as file:
data = json.load(file)
return cls.model_validate(data)
except (FileNotFoundError, KeyError, json.JSONDecodeError, ValidationError) as e:
Expand All @@ -72,7 +72,7 @@ def load(cls) -> Self:
return cls()

@classmethod
def get_entry(cls, username: str, url: str) -> Optional[Token]:
def get_entry(cls, username: str, url: str) -> Token | None:
"""Retrieve a token by username and URL."""
tokens_file = cls.load()
for token in tokens_file.tokens:
Expand Down

0 comments on commit ae7e432

Please sign in to comment.