Getting reportOptionalCall error when assigning a variable to a bound method with if statements? #2251
-
First, here's my code snippet: def _check_instance(
var: Any, name: str, types: Union[Type, tuple[Type, ...]], can_be_none=True
) -> None:
if not isinstance(var, types) and var is None and not can_be_none:
raise TypeError("'{}' object is not of type {}.".format(name, types))
class amped:
def __init__(self, log=None):
...
if log is not None and isinstance(log, logging.RootLogger):
self._log = log
else:
self._log = None
def _log_exception(self, ex):
if self._log is not None:
self._log.error(ex)
raise type(ex)(ex)
....
def create(
self,
library: str = "concurrent",
pool_type: str = "thread",
group: str = "",
name: str = "",
affinity: Union[Sequence[int], Set[int]] = (),
initializer: Optional[Callable[..., None]] = None,
initargs: Tuple[Any, ...] = (),
):
"""
Create a pool of threads or processes.
"""
# Make sure the 'library' argument is a string
_check_instance(library, "library", str, can_be_none=False)
# Make sure the 'pool_type' argument is a string
_check_instance(pool_type, "pool_type", str, can_be_none=False)
# Defines what class to use based on given call to this function
pool_class = None
# 'multiprocessing' will only support a pool of processes
# No reason to create a thread pool using the multiprocessing library
# Incorrect 'pool_type' values will raise an exception
if library == "multiprocessing":
if pool_type == "process":
pool_class = _amped_process_pool_mp
else:
msg = "'multiprocessing' library only supports 'process' pool_type."
self._log_exception(ValueError(msg))
# 'concurrent' library supports both process and thread pools
elif library == "concurrent":
if pool_type == "process":
pool_class = _amped_process_pool_cf
elif pool_type == "thread":
pool_class = _amped_thread_pool_cf
else:
msg = "'pool_type' has an incorrect value - it should either be 'process' or 'thread'."
self._log_exception(ValueError(msg))
...
# Create the pool with a given name under the given group with the
# related data filled in.
self._pools[group][name] = {}
self._pools[group][name]["parent"] = psutil.Process().parent()
self._pools[group][name]["affinity"] = tuple(affinity)
self._pools[group][name]["initializer"] = initializer
self._pools[group][name]["initargs"] = tuple(initargs)
# Attempt to create the pool with the given variables
# Otherwise raise an error
try:
self._pools[group][name]["pool"] = pool_class(
name=name,
affinity=self._pools[group][name]["affinity"],
# mp_context=self._mp_ctx,
initializer=self._pools[group][name]["initializer"],
initargs=self._pools[group][name]["initargs"],
)
return self._pools[group][name]["pool"]
except Exception as e:
self._log_exception(e) It might be a lot to read, but the general gist is that this creates a process/thread pool from either I check that the input arguments for the self._pools[group][name]["pool"] = pool_class(...) where I get the error: if pool_type == "process":
pool_class = _amped_process_pool_cf
elif pool_type == "thread":
pool_class = _amped_thread_pool_cf
else:
msg = "'pool_type' has an incorrect value - it should either be 'process' or 'thread'."
self._log_exception(ValueError(msg)) So my question is this: is there a different way I should be going about assigning a class initializer to the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The problem is that you're not handling the case where There are a couple of ways you could fix this.
|
Beta Was this translation helpful? Give feedback.
The problem is that you're not handling the case where
library
contains a value other than "multiprocessing" or "concurrent".There are a couple of ways you could fix this.
library
parameter with the typeLiteral["concurrent", "multiprocessing"]
. This tells the type checker that it will never contain a value other than these two literal values.else
clause that handles the case wherelibrary
contains an unexpected value and callself._log_exception
in that case.