From 62f3203c3fabbab5729d6548c588e12001d94c30 Mon Sep 17 00:00:00 2001 From: A_D Date: Tue, 25 Aug 2020 12:00:30 +0200 Subject: [PATCH] Added type checker hint for unbound names Due to the fact that cmdr and entry are only assigned if item exists, a situation can arise where any access to the names will raise an UnboundLocalException, this tells the type checker to ignore that possibility by using a TYPE_CHECKING guarded assignment to those names. This does not fix the issue at runtime, it just tells the type checker that its fine. As this remains a bug, I have left TODOs in to note its existence. --- plugins/edsm.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/edsm.py b/plugins/edsm.py index 15999fca..f13517a4 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -468,13 +468,14 @@ def worker() -> None: retrying = 0 while retrying < 3: try: - # TODO: Technically entry can be unbound here. - if item and entry['event'] in ('CarrierJump', 'FSDJump', 'Location', 'Docked'): - logger.debug(f'{entry["event"]}') + if TYPE_CHECKING: + # Tell the type checker that these two are bound. + # TODO: While this works because of the item check below, these names are still technically unbound + # TODO: in some cases, therefore this should be refactored. + cmdr: str = "" + entry: Mapping[str, Any] = {} - if item and entry['event'] not in this.discardedEvents: - if entry['event'] in ('CarrierJump', 'FSDJump', 'Location', 'Docked'): - logger.debug(f'{entry["event"]} event not in discarded list') + if item and entry['event'] not in this.discardedEvents: # TODO: Technically entry can be unbound here. pending.append(entry) # Get list of events to discard