Skip to content

Commit 5712441

Browse files
committed
Changed base type of Dictable to be Iterable and not ABC, to indicate to mypy that __iter__ is implemented
1 parent c30be84 commit 5712441

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

domdf_python_tools/bases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#
2626

2727
# stdlib
28-
from abc import ABC, abstractmethod
28+
from abc import abstractmethod
2929
from collections import UserList
3030
from pprint import pformat
3131
from typing import Any, Callable, Dict, Iterable, Tuple
@@ -34,7 +34,7 @@
3434
import pydash # type: ignore
3535

3636

37-
class Dictable(ABC):
37+
class Dictable(Iterable):
3838
"""
3939
The basic structure of a class that can be converted into a dictionary
4040
"""
@@ -46,7 +46,7 @@ def __init__(self, *args, **kwargs):
4646
def __str__(self) -> str:
4747
return self.__repr__()
4848

49-
def __iter__(self) -> Iterable[Tuple[str, Any]]:
49+
def __iter__(self) -> Iterable[Tuple[str, Any]]: # type: ignore[override]
5050
for key, value in self.__dict__.items():
5151
yield key, value
5252

tests/mypy_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# stdlib
2+
from typing import Any, Dict
3+
4+
# this package
5+
from domdf_python_tools.bases import Dictable
6+
7+
8+
class MyDictable(Dictable):
9+
def __init__(self, foo: str, bar: int):
10+
super().__init__()
11+
12+
self.foo: str = foo
13+
self.bar: float = float(bar)
14+
15+
@property
16+
def __dict__(self):
17+
return dict(
18+
foo=self.foo,
19+
bar=self.bar
20+
)
21+
22+
23+
def myfunc() -> Dict[str, Any]:
24+
a = MyDictable("foo", 12)
25+
return dict(a)
26+
27+
28+
myfunc()

0 commit comments

Comments
 (0)