Skip to content

Commit 22fc286

Browse files
committed
Replace getattr/hasattr with direct access and isinstance in BaseFile.get_data
Use self._data directly (type known from setter) and isinstance checks that mypy can narrow, removing type: ignore comments.
1 parent f367a10 commit 22fc286

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

emails/store/file.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BaseFile:
2929
Store base "attachment-file" information.
3030
"""
3131

32+
_data: bytes | str | IO[bytes] | None
33+
3234
def __init__(self, **kwargs: Any) -> None:
3335
"""
3436
uri and filename are connected properties.
@@ -52,13 +54,13 @@ def as_dict(self, fields: tuple[str, ...] | None = None) -> dict[str, Any]:
5254
return dict([(k, getattr(self, k)) for k in fields])
5355

5456
def get_data(self) -> bytes | str | None:
55-
_data = getattr(self, '_data', None)
56-
if isinstance(_data, str):
57+
_data = self._data
58+
if isinstance(_data, (str, bytes)):
5759
return _data
58-
elif hasattr(_data, 'read'):
59-
return _data.read() # type: ignore[union-attr, no-any-return]
60+
elif _data is None:
61+
return None
6062
else:
61-
return _data
63+
return _data.read()
6264

6365
def set_data(self, value: bytes | str | IO[bytes] | None) -> None:
6466
self._data = value

0 commit comments

Comments
 (0)