Skip to content

Commit

Permalink
Updated type hints to use built-ins where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
conqp committed Jan 27, 2021
1 parent 99900f6 commit 88fb3b2
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions mcipc/query/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from configparser import ConfigParser, SectionProxy
from os import getenv, name
from pathlib import Path
from typing import Dict, Iterator, NamedTuple, Tuple
from typing import Iterator, NamedTuple

from mcipc.query.exceptions import InvalidConfig

Expand Down Expand Up @@ -53,14 +53,14 @@ def from_config_section(cls, section: SectionProxy) -> Config:
return cls(host, port)


def entries(config_parser: ConfigParser) -> Iterator[Tuple[str, Config]]:
def entries(config_parser: ConfigParser) -> Iterator[tuple[str, Config]]:
"""Yields config entries."""

for section in config_parser.sections():
yield (section, Config.from_config_section(config_parser[section]))


def servers() -> Dict[str, Config]:
def servers() -> dict[str, Config]:
"""Returns a dictionary of servers."""

CONFIG.read(CONFIG_FILE)
Expand Down
4 changes: 2 additions & 2 deletions mcipc/query/proto/full_stats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Full statistics protocol."""

from __future__ import annotations
from typing import IO, Iterator, List, NamedTuple
from typing import IO, Iterator, NamedTuple

from mcipc.functions import json_serializable
from mcipc.query.proto.common import MAGIC
Expand Down Expand Up @@ -134,7 +134,7 @@ class FullStats(NamedTuple):
max_players: int
host_port: int
host_ip: IPAddressOrHostname
players: List[str]
players: list[str]

@classmethod
def read(cls, file: IO) -> FullStats:
Expand Down
3 changes: 1 addition & 2 deletions mcipc/query/queryclt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from logging import DEBUG, INFO, basicConfig, getLogger
from socket import timeout
from sys import exit, stdout # pylint: disable=W0622
from typing import Tuple

from mcipc.query.exceptions import InvalidConfig
from mcipc.query.client import Client
Expand Down Expand Up @@ -89,7 +88,7 @@ def get_args() -> Namespace:
return parser.parse_args()


def get_credentials(server: str) -> Tuple[str, int]:
def get_credentials(server: str) -> tuple[str, int]:
"""Get the credentials for a server from the respective server name."""

try:
Expand Down
3 changes: 1 addition & 2 deletions mcipc/rcon/commands/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from functools import partial
from re import fullmatch
from typing import List

from mcipc.rcon.client import Client
from mcipc.rcon.functions import boolmap, parsed
Expand All @@ -20,7 +19,7 @@
LIST = 'There are (\\d+) whitelisted players: (.*)'


def parse_list(response: str) -> List[str]:
def parse_list(response: str) -> list[str]:
"""Returns a list of whitelisted players."""

if match := fullmatch(LIST, response):
Expand Down
6 changes: 4 additions & 2 deletions mcipc/rcon/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import wraps
from json import dumps
from re import fullmatch
from typing import Any, Callable, Iterable, Iterator, Tuple
from typing import Any, Callable, Iterable, Iterator


__all__ = [
Expand Down Expand Up @@ -34,7 +34,7 @@ def boolmap(text: str, true: str = None, false: str = None, *,
return default


def ensure_one(**kwargs: Any) -> Tuple[str, Any]: # pylint: disable=R1710
def ensure_one(**kwargs: Any) -> tuple[str, Any]:
"""Ensures that only one argument is set."""

if sum(value is not None for value in kwargs.values()) != 1:
Expand All @@ -44,6 +44,8 @@ def ensure_one(**kwargs: Any) -> Tuple[str, Any]: # pylint: disable=R1710
if value is not None:
return (key, value)

raise ValueError('Not-none value disappeared.')


def parsed(parser: Callable[[str], Any]) -> Callable[[Callable], Callable]:
"""Updates a function to parse its result with a parser."""
Expand Down
4 changes: 2 additions & 2 deletions mcipc/rcon/nbt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Named binary tags."""

from typing import Any, Dict, Union
from typing import Any, Union

from mcipc.rcon.enumerations import Item, TargetSelector
from mcipc.rcon.functions import stringify
Expand All @@ -9,7 +9,7 @@
__all__ = ['NBT']


def tags_to_str(tags: Dict[str, Any]) -> str:
def tags_to_str(tags: dict[str, Any]) -> str:
"""Returns the tags as a string."""

return ', '.join(f'{key}={stringify(val)}' for key, val in tags.items())
Expand Down
6 changes: 3 additions & 3 deletions mcipc/rcon/response_types/players.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Information about online players."""

from re import Match, fullmatch
from typing import Iterator, List, NamedTuple, Optional
from typing import Iterator, NamedTuple, Optional
from uuid import UUID

from mcipc.functions import json_serializable
Expand Down Expand Up @@ -31,10 +31,10 @@ class Players(NamedTuple):

online: int
max: int
players: List[Player]
players: list[Player]

@property
def names(self) -> List[str]:
def names(self) -> list[str]:
"""Returns a list of the players' names
for backward compatibility.
"""
Expand Down
8 changes: 4 additions & 4 deletions mcipc/rcon/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Types for type hints."""

from ipaddress import IPv4Address, IPv6Address
from typing import Tuple, Union
from typing import Union

from mcipc.rcon.enumerations import Ability
from mcipc.rcon.enumerations import Action
Expand Down Expand Up @@ -73,9 +73,9 @@
IntRange = Union[range, str]
IPAddressOrHostname = Union[IPv4Address, IPv6Address, str]
JSON = Union[bool, dict, float, int, list, str]
Rotation = Union[Tuple[IntOrStr, IntOrStr], str]
Vec2 = Union[Tuple[Coordinate, Coordinate], str]
TargetValue = Vec3 = Union[Tuple[Coordinate, Coordinate, Coordinate], str]
Rotation = Union[tuple[IntOrStr, IntOrStr], str]
Vec2 = Union[tuple[Coordinate, Coordinate], str]
TargetValue = Vec3 = Union[tuple[Coordinate, Coordinate, Coordinate], str]
# Enumeration / str unions.
Ability = Union[Ability, str]
Action = Union[Action, str]
Expand Down

0 comments on commit 88fb3b2

Please sign in to comment.