Skip to content

Commit 06a81a9

Browse files
committed
feat: #27-work ✨ Work around mypy wanting the private attributes passed to __init__
1 parent f6f53c0 commit 06a81a9

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pydantic_changedetect/changedetect.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .utils import is_pydantic_change_detect_annotation
2323

2424
if TYPE_CHECKING: # pragma: no cover
25+
from pydantic import PrivateAttr
2526
from pydantic.typing import AbstractSetIntStr, MappingIntStrAny
2627
if PYDANTIC_V1:
2728
from pydantic.typing import DictStrAny, SetStr
@@ -53,9 +54,17 @@ class Something(ChangeDetectionMixin, pydantic.BaseModel):
5354
"""
5455

5556
if TYPE_CHECKING: # pragma: no cover
56-
model_original: Dict[str, Any]
57-
model_self_changed_fields: Set[str]
58-
model_changed_markers: set[str]
57+
# Note: Private attributes normally need to start with "_", but this code here
58+
# will only be seen by type checkers. So this is never an issue. The
59+
# usage of ` = PrivateAttr(...)` is also just here to make those type
60+
# checkers happy. Otherwise, they might "require" you to pass those
61+
# parameters to `__init__`, which is just plainly wrong. The private
62+
# attributes ChangeDetectionMixin uses are defined as __slots__ and
63+
# thus just new attributes the class has - not something you need to pass
64+
# anywhere.
65+
model_original: Dict[str, Any] = PrivateAttr(...)
66+
model_self_changed_fields: Set[str] = PrivateAttr(...)
67+
model_changed_markers: set[str] = PrivateAttr(...)
5968

6069
__slots__ = ("model_original", "model_self_changed_fields", "model_changed_markers")
6170

0 commit comments

Comments
 (0)