Skip to content

Commit

Permalink
Migrate typing to python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtryb committed Feb 14, 2024
1 parent 18a3fb7 commit b0ed226
Show file tree
Hide file tree
Showing 86 changed files with 311 additions and 344 deletions.
3 changes: 1 addition & 2 deletions shortcut_composer/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""

import templates
from typing import List

from PyQt5.QtGui import QColor

Expand All @@ -28,7 +27,7 @@
infinity = float("inf")


def create_actions() -> List[templates.RawInstructions]: return [
def create_actions() -> list[templates.RawInstructions]: return [
# Switch between FREEHAND BRUSH and the MOVE tool
templates.TemporaryKey(
name="Temporary move tool",
Expand Down
7 changes: 3 additions & 4 deletions shortcut_composer/api_krita/actions/transform_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# SPDX-FileCopyrightText: © 2022-2024 Wojciech Trybus <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Dict, Optional
from functools import partial, partialmethod

from PyQt5.QtCore import QTimer
Expand All @@ -27,7 +26,7 @@ class TransformModeActions:

def __init__(self, window) -> None:
self._finder = TransformModeFinder()
self._actions: Dict[TransformMode, QWidgetAction] = {}
self._actions: dict[TransformMode, QWidgetAction] = {}
self._create_actions(window)

def _create_actions(self, window) -> None:
Expand Down Expand Up @@ -84,7 +83,7 @@ class TransformModeFinder:
"""

def __init__(self) -> None:
self._mode_buttons: Dict[TransformMode, QToolButton] = {}
self._mode_buttons: dict[TransformMode, QToolButton] = {}
self._transform_options: QWidget
self._apply_button: QPushButton

Expand All @@ -111,7 +110,7 @@ def activate_mode(self, mode: TransformMode, apply: bool) -> None:
self._apply_button.click()
self._mode_buttons[mode].click()

def get_active_mode(self) -> Optional[TransformMode]:
def get_active_mode(self) -> TransformMode | None:
for mode, button in self._mode_buttons.items():
if button.isChecked():
return mode
Expand Down
10 changes: 5 additions & 5 deletions shortcut_composer/api_krita/core_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from krita import Krita as Api, Extension, qApp
from typing import Callable, Protocol, Any, Dict, Optional
from typing import Callable, Protocol, Any

from PyQt5.QtWidgets import (
QMainWindow,
Expand Down Expand Up @@ -36,7 +36,7 @@ def get_active_view(self) -> View:
"""Return wrapper of krita `View`."""
return View(self.instance.activeWindow().activeView())

def get_active_document(self) -> Optional[Document]:
def get_active_document(self) -> Document | None:
"""Return wrapper of krita `Document`."""
document = self.instance.activeDocument()
if document is None:
Expand All @@ -60,7 +60,7 @@ def get_action_shortcut(self, action_name: str) -> QKeySequence:
"""Return shortcut of krita action called `action_name`."""
return self.instance.action(action_name).shortcut()

def get_presets(self) -> Dict[str, Any]:
def get_presets(self) -> dict[str, Any]:
"""Return a list of unwrapped preset objects"""
return self.instance.resources('preset')

Expand All @@ -79,7 +79,7 @@ def read_setting(
group: str,
name: str,
default: str = "Not stored"
) -> Optional[str]:
) -> str | None:
"""
Read a setting from kritarc file.
Expand Down Expand Up @@ -123,7 +123,7 @@ def add_theme_change_callback(self, callback: Callable[[], None]) -> Any:
Method is delayed with a timer to allow running it on plugin
initialization phase.
"""
def connect_callback():
def connect_callback() -> None:
self.main_window = self.instance.activeWindow()
if self.main_window is not None:
self.main_window.themeChanged.connect(callback)
Expand Down
2 changes: 1 addition & 1 deletion shortcut_composer/api_krita/enums/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ class Action(EnumGroup):
CUT_COLUMNS = "cut_columns_to_clipboard"
PASTE_COLUMNS = "paste_columns_from_clipboard"

def activate(self):
def activate(self) -> None:
"""Activate the action."""
try:
Api.instance().action(self.value).trigger()
Expand Down
10 changes: 5 additions & 5 deletions shortcut_composer/api_krita/enums/helpers/enum_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# SPDX-License-Identifier: GPL-3.0-or-later

import sys
from typing import Dict, List, Tuple, TypeVar, Optional
from typing import Tuple, TypeVar
from enum import Enum, EnumMeta
T = TypeVar("T", bound=Enum)


class EnumGroupMeta(EnumMeta):
"""Metaclass for creating enum groups. See EnumGroup documentation."""

_groups_: Dict[str, 'Group']
_groups_: dict[str, 'Group']
"""Maps enum groups to their pretty names."""

def __new__(
Expand All @@ -20,11 +20,11 @@ def __new__(
attrs
) -> 'EnumGroupMeta':
# Filter out class attributes provided by Python.
items: List[Tuple[str, Group]]
items: list[Tuple[str, Group]]
items = [i for i in attrs.items() if not i[0].startswith("__")]

# Add keys (which will become enum members) to correct groups
current_group: Optional[Group] = None
current_group: Group | None = None
for key, value in items:
if isinstance(value, Group):
current_group = value
Expand Down Expand Up @@ -67,7 +67,7 @@ def __new__(
return new_class


class Group(List[Enum]):
class Group(list[Enum]):
"""List of enum members belonging to one Enum."""

def __init__(self, name: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion shortcut_composer/api_krita/enums/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def pretty_name(self) -> str:
return PRETTY_NAMES[self]
return f"{self.name.replace('_', ' ').title()} Tool"

def activate(self):
def activate(self) -> None:
Api.instance().action(self.value).trigger()

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions shortcut_composer/api_krita/pyqt/painter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later

import math
from typing import Optional

from PyQt5.QtGui import QPainter, QPainterPath, QColor, QPixmap, QPaintEvent
from PyQt5.QtCore import QPoint, QRectF
Expand Down Expand Up @@ -31,7 +30,7 @@ def paint_wheel(
center: QPoint,
outer_radius: float,
color: QColor,
thickness: Optional[float] = None,
thickness: float | None = None,
) -> None:
"""
Paint a wheel at center providing its radius, color and thickness.
Expand All @@ -52,7 +51,7 @@ def paint_pie(
angle: int,
span: int,
color: QColor,
thickness: Optional[float] = None,
thickness: float | None = None,
) -> None:
"""Paint part of wheel a, that spans left and right by span/2."""
angle = -angle + 90
Expand Down
4 changes: 2 additions & 2 deletions shortcut_composer/api_krita/pyqt/round_button.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: © 2022-2024 Wojciech Trybus <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional, Callable
from typing import Callable

from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.QtGui import QColor, QIcon
Expand All @@ -20,7 +20,7 @@ def __init__(
active_color_callback: Callable[[], QColor],
icon: QIcon = QIcon(),
icon_scale: float = 1,
parent: Optional[QWidget] = None,
parent: QWidget | None = None,
) -> None:
QPushButton.__init__(self, icon, "", parent)
self.setCursor(Qt.ArrowCursor)
Expand Down
4 changes: 1 addition & 3 deletions shortcut_composer/api_krita/pyqt/safe_confirm_button.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# SPDX-FileCopyrightText: © 2022-2024 Wojciech Trybus <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional

from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSignal, QEvent
from PyQt5.QtWidgets import QWidget, QPushButton
Expand All @@ -26,7 +24,7 @@ def __init__(
icon: QIcon = QIcon(),
text: str = "",
confirm_text: str = "Confirm?",
parent: Optional[QWidget] = None
parent: QWidget | None = None
) -> None:
super().__init__(icon, text, parent)
super().clicked.connect(self._clicked)
Expand Down
2 changes: 1 addition & 1 deletion shortcut_composer/api_krita/wrappers/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Canvas:

canvas: KritaCanvas

def __post_init__(self):
def __post_init__(self) -> None:
self._zoom_scale = Document(self.canvas.view().document()).dpi/7200

@property
Expand Down
8 changes: 4 additions & 4 deletions shortcut_composer/api_krita/wrappers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from krita import Krita as Api
import os.path
from typing import List, Any
from typing import Any

from PyQt5.QtSql import QSqlDatabase, QSqlQuery

Expand All @@ -27,7 +27,7 @@ def connect_if_needed(cls) -> None:
path = os.path.join(path, "resourcecache.sqlite")
cls.database.setDatabaseName(path)

def _single_column_query(self, sql_query: str, value: str) -> List[Any]:
def _single_column_query(self, sql_query: str, value: str) -> list[Any]:
"""Use SQL query to get single column in a form of a list."""
if not self.database.open():
return []
Expand All @@ -43,7 +43,7 @@ def _single_column_query(self, sql_query: str, value: str) -> List[Any]:
query_handler.finish()
return return_list

def get_preset_names_from_tag(self, tag_name: str) -> List[str]:
def get_preset_names_from_tag(self, tag_name: str) -> list[str]:
"""Return list of all preset names that belong to given tag."""
tag_name = tag_name.replace("\"", "\"\"")
sql_query = f'''
Expand All @@ -59,7 +59,7 @@ def get_preset_names_from_tag(self, tag_name: str) -> List[str]:
'''
return self._single_column_query(sql_query, "preset")

def get_brush_tags(self) -> List[str]:
def get_brush_tags(self) -> list[str]:
"Return list of all tag names."
sql_query = '''
SELECT DISTINCT t.name AS tag
Expand Down
12 changes: 6 additions & 6 deletions shortcut_composer/api_krita/wrappers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from dataclasses import dataclass
from typing import List, Protocol
from typing import Protocol
from PyQt5.QtCore import QByteArray
from ..enums import NodeType
from .node import Node, KritaNode
Expand All @@ -14,13 +14,13 @@ class KritaDocument(Protocol):
def activeNode(self) -> KritaNode: ...
def setActiveNode(self, node: KritaNode): ...
def createNode(self, name: str, node_type: str) -> KritaNode: ...
def topLevelNodes(self) -> List[KritaNode]: ...
def topLevelNodes(self) -> list[KritaNode]: ...
def resolution(self) -> int: ...
def currentTime(self) -> int: ...
def setCurrentTime(self, time: int) -> None: ...
def refreshProjection(self) -> None: ...
def annotation(self, type: str) -> QByteArray: ...
def annotationTypes(self) -> List[str]: ...
def annotationTypes(self) -> list[str]: ...

def setAnnotation(
self,
Expand Down Expand Up @@ -71,13 +71,13 @@ def current_time(self, time: int) -> None:
"""Set current time using frame number"""
self.document.setCurrentTime(round(time))

def get_top_nodes(self) -> List[Node]:
def get_top_nodes(self) -> list[Node]:
"""Return a list of `Nodes` without a parent."""
return [Node(node) for node in self.document.topLevelNodes()]

def get_all_nodes(self, include_collapsed: bool = False) -> List[Node]:
def get_all_nodes(self, include_collapsed: bool = False) -> list[Node]:
"""Return a list of all `Nodes` in this document bottom to top."""
def recursive_search(nodes: List[Node], found_so_far: List[Node]):
def recursive_search(nodes: list[Node], found_so_far: list[Node]):
for node in nodes:
if include_collapsed or not node.collapsed:
recursive_search(node.get_child_nodes(), found_so_far)
Expand Down
6 changes: 3 additions & 3 deletions shortcut_composer/api_krita/wrappers/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from dataclasses import dataclass
from typing import List, Protocol
from typing import Protocol
from ..enums import BlendingMode


Expand All @@ -25,7 +25,7 @@ def collapsed(self) -> bool: ...
def setCollapsed(self, value: bool) -> None: ...
def animated(self) -> bool: ...
def uniqueId(self) -> str: ...
def childNodes(self) -> List['KritaNode']: ...
def childNodes(self) -> list['KritaNode']: ...
def parentNode(self) -> 'KritaNode': ...


Expand Down Expand Up @@ -121,7 +121,7 @@ def is_animated(self) -> bool:
"""Read-only property telling if this node has animation frames."""
return self.node.animated()

def get_child_nodes(self) -> List['Node']:
def get_child_nodes(self) -> list['Node']:
"""Return a list of wrapped Nodes that are children of this one."""
return [Node(node) for node in self.node.childNodes()]

Expand Down
8 changes: 4 additions & 4 deletions shortcut_composer/api_krita/wrappers/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from krita import Krita as Api
from dataclasses import dataclass
from typing import Protocol, Dict
from typing import Protocol
from functools import cached_property

from ..enums import BlendingMode
Expand Down Expand Up @@ -39,7 +39,7 @@ class View:
view: KritaView

@cached_property
def preset_map(self) -> Dict[str, _KritaPreset]:
def preset_map(self) -> dict[str, _KritaPreset]:
"""Return dictionary mapping preset names to krita preset objects."""
return Api.instance().resources('preset')

Expand Down Expand Up @@ -95,11 +95,11 @@ def brush_size(self, brush_size: float) -> None:
self.view.setBrushSize(brush_size)

@property
def brush_rotation(self):
def brush_rotation(self) -> float:
"""Settable property with brush rotation in deg between 0 and 360."""
return self.view.brushRotation()

@brush_rotation.setter
def brush_rotation(self, rotation: float):
def brush_rotation(self, rotation: float) -> None:
"""Set brush rotation with float representing angle in degrees."""
self.view.setBrushRotation(rotation % 360)
Loading

0 comments on commit b0ed226

Please sign in to comment.