Skip to content

Commit b9b0b22

Browse files
authored
Make Message.__getattribute__ invisible to type checkers (#359)
This lets linters know that we shouldn't access fields that aren't actually defined
1 parent 402c212 commit b9b0b22

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/betterproto/__init__.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
timezone,
1919
)
2020
from typing import (
21+
TYPE_CHECKING,
2122
Any,
2223
Callable,
2324
Dict,
@@ -693,18 +694,20 @@ def __repr__(self) -> str:
693694
]
694695
return f"{self.__class__.__name__}({', '.join(parts)})"
695696

696-
def __getattribute__(self, name: str) -> Any:
697-
"""
698-
Lazily initialize default values to avoid infinite recursion for recursive
699-
message types
700-
"""
701-
value = super().__getattribute__(name)
702-
if value is not PLACEHOLDER:
703-
return value
697+
if not TYPE_CHECKING:
704698

705-
value = self._get_field_default(name)
706-
super().__setattr__(name, value)
707-
return value
699+
def __getattribute__(self, name: str) -> Any:
700+
"""
701+
Lazily initialize default values to avoid infinite recursion for recursive
702+
message types
703+
"""
704+
value = super().__getattribute__(name)
705+
if value is not PLACEHOLDER:
706+
return value
707+
708+
value = self._get_field_default(name)
709+
super().__setattr__(name, value)
710+
return value
708711

709712
def __setattr__(self, attr: str, value: Any) -> None:
710713
if attr != "_serialized_on_wire":

0 commit comments

Comments
 (0)