Use correct type hint for generators

This commit is contained in:
Veronica Berglyd Olsen
2024-03-18 18:37:21 +01:00
parent 239ca55ba9
commit 7247d4dc76
5 changed files with 12 additions and 12 deletions
+4 -4
View File
@@ -33,7 +33,7 @@ import logging
from time import time from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pathlib import Path from pathlib import Path
from collections.abc import ItemsView, Iterable, Iterator from collections.abc import Generator, ItemsView, Iterable
from novelwriter import SHARED from novelwriter import SHARED
from novelwriter.enum import nwComment, nwItemClass, nwItemType, nwItemLayout from novelwriter.enum import nwComment, nwItemClass, nwItemType, nwItemLayout
@@ -523,7 +523,7 @@ class NWIndex:
return tItem[sTitle] return tItem[sTitle]
return None return None
def iterItemHeadings(self, tHandle: str) -> Iterator[str, IndexHeading]: def iterItemHeadings(self, tHandle: str) -> Generator[str, IndexHeading]:
"""Get all headings for a specific item.""" """Get all headings for a specific item."""
if tItem := self._itemIndex[tHandle]: if tItem := self._itemIndex[tHandle]:
yield from tItem.items() yield from tItem.items()
@@ -531,7 +531,7 @@ class NWIndex:
def novelStructure( def novelStructure(
self, rootHandle: str | None = None, activeOnly: bool = True self, rootHandle: str | None = None, activeOnly: bool = True
) -> Iterator[tuple[str, str, str, IndexHeading]]: ) -> Generator[tuple[str, str, str, IndexHeading]]:
"""Iterate over all titles in the novel, in the correct order as """Iterate over all titles in the novel, in the correct order as
they appear in the tree view and in the respective document they appear in the tree view and in the respective document
files, but skipping all note files. files, but skipping all note files.
@@ -673,7 +673,7 @@ class NWIndex:
def getTagsData( def getTagsData(
self, activeOnly: bool = True self, activeOnly: bool = True
) -> Iterator[tuple[str, str, str, IndexItem | None, IndexHeading | None]]: ) -> Generator[tuple[str, str, str, IndexItem | None, IndexHeading | None]]:
"""Return all known tags.""" """Return all known tags."""
for tag, data in self._tagsIndex.items(): for tag, data in self._tagsIndex.items():
iItem = self._itemIndex[data.get("handle")] iItem = self._itemIndex[data.get("handle")]
+2 -2
View File
@@ -31,7 +31,7 @@ from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pathlib import Path from pathlib import Path
from functools import partial from functools import partial
from collections.abc import Iterator from collections.abc import Generator
from PyQt5.QtCore import QCoreApplication from PyQt5.QtCore import QCoreApplication
@@ -517,7 +517,7 @@ class NWProject:
# Class Methods # Class Methods
## ##
def iterProjectItems(self) -> Iterator[NWItem]: def iterProjectItems(self) -> Generator[NWItem]:
"""This function ensures that the item tree loaded is sent to """This function ensures that the item tree loaded is sent to
the GUI tree view in such a way that the tree can be built. That the GUI tree view in such a way that the tree can be built. That
is, the parent item must be sent before its child. In principle, is, the parent item must be sent before its child. In principle,
+2 -2
View File
@@ -29,7 +29,7 @@ import logging
from time import time from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pathlib import Path from pathlib import Path
from collections.abc import Iterator from collections.abc import Generator
from novelwriter.error import logException from novelwriter.error import logException
from novelwriter.common import formatTimeStamp from novelwriter.common import formatTimeStamp
@@ -110,7 +110,7 @@ class NWSessionLog:
return True return True
def iterRecords(self) -> Iterator[dict]: def iterRecords(self) -> Generator[dict]:
"""Iterate through all records in the log.""" """Iterate through all records in the log."""
sessFile = self._project.storage.getMetaFile(nwFiles.SESS_FILE) sessFile = self._project.storage.getMetaFile(nwFiles.SESS_FILE)
if isinstance(sessFile, Path) and sessFile.is_file(): if isinstance(sessFile, Path) and sessFile.is_file():
+2 -2
View File
@@ -28,7 +28,7 @@ import random
import logging import logging
from typing import TYPE_CHECKING, Literal from typing import TYPE_CHECKING, Literal
from collections.abc import ItemsView, Iterator, KeysView, ValuesView from collections.abc import Generator, ItemsView, Iterator, KeysView, ValuesView
from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QPixmap, QColor from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QPixmap, QColor
from PyQt5.QtCore import QRectF, Qt from PyQt5.QtCore import QRectF, Qt
@@ -193,7 +193,7 @@ class NWStatus:
self._store[key]["count"] += 1 self._store[key]["count"] += 1
return return
def pack(self) -> Iterator[tuple[str, dict]]: def pack(self) -> Generator[tuple[str, dict]]:
"""Pack the status entries into a dictionary.""" """Pack the status entries into a dictionary."""
for key, data in self._store.items(): for key, data in self._store.items():
yield (data["name"], { yield (data["name"], {
+2 -2
View File
@@ -28,7 +28,7 @@ import logging
from typing import TYPE_CHECKING, Literal, overload from typing import TYPE_CHECKING, Literal, overload
from pathlib import Path from pathlib import Path
from collections.abc import Iterator from collections.abc import Generator, Iterator
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType
from novelwriter.error import logException from novelwriter.error import logException
@@ -387,7 +387,7 @@ class NWTree:
rootClasses.add(nwItem.itemClass) rootClasses.add(nwItem.itemClass)
return rootClasses return rootClasses
def iterRoots(self, itemClass: nwItemClass | None) -> Iterator[tuple[str, NWItem]]: def iterRoots(self, itemClass: nwItemClass | None) -> Generator[tuple[str, NWItem]]:
"""Iterate over all root items of a given class in order.""" """Iterate over all root items of a given class in order."""
for tHandle in self._order: for tHandle in self._order:
nwItem = self.__getitem__(tHandle) nwItem = self.__getitem__(tHandle)