From 9446e6641ebc5cf5f2ba1467e15f62dfe299dc07 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Mon, 4 Sep 2023 19:33:22 +0100 Subject: [PATCH 1/2] Type hint sybil/example.py This makes `mypy --strict sybil/example.py` pass. --- sybil/example.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sybil/example.py b/sybil/example.py index a9cee60..d391ad5 100644 --- a/sybil/example.py +++ b/sybil/example.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Dict from .region import Region @@ -26,7 +26,7 @@ class Example: """ def __init__( - self, document: 'Document', line: int, column: int, region: Region, namespace: dict + self, document: 'Document', line: int, column: int, region: Region, namespace: Dict[str, Any] ) -> None: #: The :class:`~sybil.document.Document` from which this example came. self.document: 'Document' = document @@ -51,7 +51,7 @@ def __init__( self.parsed: Any = region.parsed #: The :attr:`~sybil.Document.namespace` of the document from #: which this example came. - self.namespace: dict = namespace + self.namespace: Dict[str, Any] = namespace def __repr__(self) -> str: return ''.format( @@ -59,7 +59,7 @@ def __repr__(self) -> str: ) def evaluate(self) -> None: - evaluator = self.document.evaluator or self.region.evaluator + evaluator = self.document.evaluator if self.document.evaluator is not None else self.region.evaluator result = evaluator(self) if result: raise SybilFailure(self, result) From 424c6e6a06a6842bbc92a52c5cb112e77482a2ee Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Mon, 4 Sep 2023 20:08:49 +0100 Subject: [PATCH 2/2] Correctly type self.document.evaluator --- sybil/document.py | 2 +- sybil/example.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sybil/document.py b/sybil/document.py index 0137517..c097070 100644 --- a/sybil/document.py +++ b/sybil/document.py @@ -31,7 +31,7 @@ class Document: #: including not executing the example at all, modifying the :class:`~sybil.example.Example` #: or the :class:`~sybil.document.Document`, or calling the original evaluator on the example. #: This last case should always take the form of ``example.region.evaluator(example)``. - evaluator: Evaluator = None + evaluator: Optional[Evaluator] = None def __init__(self, text: str, path: str) -> None: #: This is the text of the documentation source file. diff --git a/sybil/example.py b/sybil/example.py index d391ad5..26e41da 100644 --- a/sybil/example.py +++ b/sybil/example.py @@ -59,7 +59,7 @@ def __repr__(self) -> str: ) def evaluate(self) -> None: - evaluator = self.document.evaluator if self.document.evaluator is not None else self.region.evaluator + evaluator = self.document.evaluator or self.region.evaluator result = evaluator(self) if result: raise SybilFailure(self, result)