Skip to content

Commit

Permalink
more types
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 30, 2024
1 parent 6a3e0a4 commit 850e8e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
19 changes: 13 additions & 6 deletions lib/python/pyflyby/_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import subprocess
import sys

from typing import List, Any, Dict
from typing import List, Any, Dict, Union, Literal


from pyflyby._autoimp import (LoadSymbolError, ScopeStack, auto_eval,
Expand Down Expand Up @@ -2269,7 +2269,9 @@ def disable(self):

def _safe_call(self, function, *args, **kwargs):
on_error = kwargs.pop("on_error", None)
raise_on_error = kwargs.pop("raise_on_error", "if_debug")
raise_on_error: Union[bool, Literal["if_debug"]] = kwargs.pop(
"raise_on_error", "if_debug"
)
if self._errored:
# If we previously errored, then we should already have
# unregistered the hook that led to here. However, in some corner
Expand All @@ -2295,7 +2297,7 @@ def _safe_call(self, function, *args, **kwargs):
logger.error("Error trying to disable: %s: %s",
type(e2).__name__, e2)
# Raise or print traceback in debug mode.
if raise_on_error == True:
if raise_on_error is True:
raise
elif raise_on_error == 'if_debug':
if logger.debug_enabled:
Expand All @@ -2305,7 +2307,7 @@ def _safe_call(self, function, *args, **kwargs):
import traceback
traceback.print_exc()
raise
elif raise_on_error == False:
elif raise_on_error is False:
if logger.debug_enabled:
import traceback
traceback.print_exc()
Expand All @@ -2328,8 +2330,13 @@ def reset_state_new_cell(self):
sorted([k for k,v in autoimported.items() if not v]))
self._autoimported_this_cell = {}

def auto_import(self, arg, namespaces=None,
raise_on_error='if_debug', on_error=None):
def auto_import(
self,
arg,
namespaces=None,
raise_on_error: Union[bool, Literal["if_debug"]] = "if_debug",
on_error=None,
):
if namespaces is None:
namespaces = get_global_namespaces(self._ip)

Expand Down
26 changes: 13 additions & 13 deletions lib/python/pyflyby/_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,6 @@
from pyflyby._util import cmp
from shlex import quote as shquote

usage = """
py --- command-line python multitool with automatic importing
$ py [--file] filename.py arg1 arg2 Execute file
$ py [--apply] function arg1 arg2 Call function
$ py [--eval] 'function(arg1, arg2)' Evaluate code
$ py [--module] modname arg1 arg2 Run a module
$ py --debug file/code... args... Debug code
$ py --debug PID Attach debugger to PID
$ py IPython shell
""".strip()

# TODO: add --tidy-imports, etc

Expand Down Expand Up @@ -377,6 +364,19 @@
from pyflyby._parse import PythonBlock
from pyflyby._util import indent, prefixes

usage = """
py --- command-line python multitool with automatic importing
$ py [--file] filename.py arg1 arg2 Execute file
$ py [--apply] function arg1 arg2 Call function
$ py [--eval] 'function(arg1, arg2)' Evaluate code
$ py [--module] modname arg1 arg2 Run a module
$ py --debug file/code... args... Debug code
$ py --debug PID Attach debugger to PID
$ py IPython shell
""".strip()

# Default compiler flags (feature flags) used for all user code. We include
# "print_function" here, but we also use auto_flags=True, which means
Expand Down
3 changes: 1 addition & 2 deletions lib/python/pyflyby/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@



from contextlib import contextmanager
from contextlib import contextmanager, ExitStack
import inspect
import os
import sys
Expand Down Expand Up @@ -451,7 +451,6 @@ def cmp(a, b):


# Create a context manager with an arbitrary number of contexts.
from contextlib import ExitStack
@contextmanager
def nested(*mgrs):
with ExitStack() as stack:
Expand Down

0 comments on commit 850e8e9

Please sign in to comment.