Skip to content

Commit ef4ed96

Browse files
committed
od: Track export destination without type checker errors.
Static type checking cannot see the relationship between the opened_here local variable and the type of object passed as dest parameter to export_od(). Switch to storing another reference to the opened file-like object instead of a boolean to avoid a [union-attr] error.
1 parent 9aab2d4 commit ef4ed96

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

canopen/objectdictionary/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def export_od(
2121
od: ObjectDictionary,
2222
dest: Union[str, TextIO, None] = None,
23-
doc_type: Optional[str] = None
23+
doc_type: Optional[str] = None,
2424
) -> None:
2525
"""Export an object dictionary.
2626
@@ -47,7 +47,7 @@ def export_od(
4747
f"supported formats: {supported}"
4848
)
4949

50-
opened_here = False
50+
opened_here: Optional[TextIO] = None
5151
try:
5252
if isinstance(dest, str):
5353
if doc_type is None:
@@ -58,7 +58,7 @@ def export_od(
5858
else:
5959
doc_type = "eds"
6060
dest = open(dest, 'w')
61-
opened_here = True
61+
opened_here = dest
6262

6363
if doc_type == "eds":
6464
from canopen.objectdictionary import eds
@@ -68,8 +68,8 @@ def export_od(
6868
return eds.export_dcf(od, dest)
6969
finally:
7070
# If dest is opened in this fn, it should be closed
71-
if opened_here:
72-
dest.close()
71+
if opened_here is not None:
72+
opened_here.close()
7373

7474

7575
def import_od(

0 commit comments

Comments
 (0)