Skip to content

Commit

Permalink
tests: improve typing (#144)
Browse files Browse the repository at this point in the history
- Corrected use of TypeVar (should be TypeAlias but only supported in 3.10+)
- Made text fields optional
- Check toastInput type more accurately
  • Loading branch information
DatGuy1 authored May 20, 2024
1 parent b0db49c commit 4a85315
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ combine_as_imports = true
line_length = 120

[tool.pytest.ini_options]
pythonpath = ["src"]
pythonpath = ["src"]

[tool.mypy]
exclude = ["build/", "main.py"]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with open("README.md", "r", encoding="utf-8") as f:
readme = f.read()

about = {}
about: dict[str, str] = {}
with open("src/windows_toasts/_version.py", "r") as f:
exec(f.read(), None, about)

Expand Down
6 changes: 3 additions & 3 deletions src/windows_toasts/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uuid
import warnings
from collections.abc import Iterable
from typing import Callable, Literal, Optional, TypeVar, Union
from typing import Callable, Literal, Optional, Union

from winrt.windows.ui.notifications import ToastDismissedEventArgs, ToastFailedEventArgs

Expand All @@ -23,7 +23,7 @@
ToastSystemButton,
)

ToastInput = TypeVar("ToastInput", ToastInputTextBox, ToastInputSelectionBox)
ToastInput = Union[ToastInputTextBox, ToastInputSelectionBox]


class Toast:
Expand Down Expand Up @@ -68,7 +68,7 @@ class Toast:

def __init__(
self,
text_fields: Union[list[Optional[str]], tuple[Optional[str]], set[Optional[str]]] = None,
text_fields: Union[None, list[Optional[str]], tuple[Optional[str]], set[Optional[str]]] = None,
audio: Optional[ToastAudio] = None,
duration: ToastDuration = ToastDuration.Default,
expiration_time: Optional[datetime.datetime] = None,
Expand Down
12 changes: 5 additions & 7 deletions src/windows_toasts/toast_document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Optional, TypeVar, Union
from typing import Union

from winrt.windows.data.xml.dom import IXmlNode, XmlDocument, XmlElement

Expand All @@ -18,7 +18,7 @@
ToastSystemButtonAction,
)

IXmlType = TypeVar("IXmlType", IXmlNode, XmlElement)
IXmlType = Union[IXmlNode, XmlElement]


class ToastDocument:
Expand Down Expand Up @@ -69,7 +69,7 @@ def GetAttributeValue(nodeAttribute: IXmlType, attributeName: str) -> str:
"""
return nodeAttribute.attributes.get_named_item(attributeName).inner_text

def GetElementByTagName(self, tagName: str) -> Optional[IXmlType]:
def GetElementByTagName(self, tagName: str) -> IXmlType:
"""
Helper function to get the first element by its tag name
Expand Down Expand Up @@ -220,18 +220,16 @@ def AddInput(self, toastInput: Union[ToastInputTextBox, ToastInputSelectionBox])
:type toastInput: Union[ToastInputTextBox, ToastInputSelectionBox]
"""
isTextBox = isinstance(toastInput, ToastInputTextBox)

self._inputFields += 1
inputNode = self.xmlDocument.create_element("input")
self.SetAttribute(inputNode, "id", toastInput.input_id)
self.SetAttribute(inputNode, "title", toastInput.caption)

if isTextBox:
if isinstance(toastInput, ToastInputTextBox):
self.SetAttribute(inputNode, "type", "text")
# noinspection PyUnresolvedReferences
self.SetAttribute(inputNode, "placeHolderContent", toastInput.placeholder)
else:
elif isinstance(toastInput, ToastInputSelectionBox):
self.SetAttribute(inputNode, "type", "selection")
if toastInput.default_selection is not None:
self.SetAttribute(inputNode, "defaultInput", toastInput.default_selection.selection_id)
Expand Down

0 comments on commit 4a85315

Please sign in to comment.