You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a class which takes a port number (int) and hostname (str):
class X(object):
def __init__(self, port=6600, host="x.y.com"):
...
elsewhere, it can be instantiated using values extracted from a dictionary:
if x is not None:
self.x = X(port=x['port'], host=x['host'])
That dictionary is itself a parameter declared as as mapping strings to strings or ints:
# type: Dict[str, Union[str, int]]
mypy complains about the call to X(...) because the values in x can be either strings or ints, but the port and host parameters are declared to be one or the other, not the union of the two.
What's the correct way to work around this issue?
The text was updated successfully, but these errors were encountered:
Short-term, your best bet is # type: ignore. You could also use casts but those have runtime costs (it's a Python function call so probably costs way more than than the dict lookups). In the future we'll have a "TypedDict" type that you can use for the type of that dictionary; see python/mypy#985 and search the tracker for TypedDict to find various tasks still open before that's a viable solution.
I've got a class which takes a port number (int) and hostname (str):
elsewhere, it can be instantiated using values extracted from a dictionary:
That dictionary is itself a parameter declared as as mapping strings to strings or ints:
mypy complains about the call to X(...) because the values in x can be either strings or ints, but the port and host parameters are declared to be one or the other, not the union of the two.
What's the correct way to work around this issue?
The text was updated successfully, but these errors were encountered: