-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'<nothing>' object is not iterable #10233
Comments
Not sure if this is the same issue but I'm getting "TypeError: 'str' object is not callable" for the string s in Your Environment PyCharm 2020.3.4 |
@LyonsDo pycharm errors shouldn't be reported to mypy; it's a different type checker. @DevilXD I think your problem is that NamedTuples can't be generic (#685). If you turn on the |
Sorry. I have the mypy plugin enabled. |
@JelleZijlstra I believe there's a different issue at play here, because getting rid of the NamedTuple and using just a normal tuple instead, generates the same error. from typing import List, Dict, Tuple, Callable, TypeVar
MenuReturn = TypeVar("MenuReturn")
MenuFunction = Callable[[], MenuReturn]
def menu(options: List[Tuple[str, MenuFunction]]) -> MenuReturn:
options_dict: Dict[str, MenuFunction] = {}
for num, entry in enumerate(options, start=1):
print(f"{num}) {entry[0]}")
options_dict[str(num)] = entry[1]
while True:
key = input("Pick an option: ")
if key in options_dict:
break
return options_dict[key]()
def func1() -> Tuple[bool, bool]:
return (False, False)
def func2() -> Tuple[bool, bool]:
return (True, True)
results: Tuple[bool, bool] = menu([ # need type annotation for 'results' - fine, I can just add it here
("Return two False values", func1),
("Return two True values", func2),
])
print(results)
# Define the types explicitly
result1: bool
result2: bool
result1, result2 = menu([ # '<nothing>' object is not iterable - types being defined doesn't matter
("Return two False values", func1),
("Return two True values", func2),
])
print(result1, result2) # the types are defined, but the error above remains |
You're not setting generic parameters fully. Try using |
OK thanks. I'll look at that later.
It seems to be working OK now thanks..
|
Huh, alright, I got it to work. This is what I've got: from typing import List, Dict, Tuple, Callable, TypeVar
MenuReturn = TypeVar("MenuReturn")
MenuFunction = Callable[[], MenuReturn]
def menu(options: List[Tuple[str, MenuFunction[MenuReturn]]]) -> MenuReturn:
options_dict: Dict[str, MenuFunction[MenuReturn]] = {}
for num, entry in enumerate(options, start=1):
print(f"{num}) {entry[0]}")
options_dict[str(num)] = entry[1]
while True:
key = input("Pick an option: ")
if key in options_dict:
break
return options_dict[key]()
def func1() -> Tuple[bool, bool]:
return (False, False)
def func2() -> Tuple[bool, bool]:
return (True, True)
results = menu([
("Return two False values", func1),
("Return two True values", func2),
])
reveal_type(results)
result1, result2 = menu([
("Return two False values", func1),
("Return two True values", func2),
])
reveal_type(result1)
reveal_type(result2) I'm guessing this isn't a real bug then. However, the error message of |
Agree that this is unintuitive! #10241 seems like the underlying issue, so let's keep that one open. |
Bug Report
The internal
<nothing>
object not being an iterable appears to be a problem in a case like the example below.To Reproduce
Expected Behavior
It'd let me at least define the types of
result1
andresult2
before themenu()
usage, silencing the'<nothing>' object is not iterable
error.Actual Behavior
This is not the case, and while
result1
andresult2
do end up with the correct types, I still need to add# type: ignore
on themenu()
usage line.Your Environment
Notes
It seems that MyPy ends up with the
MenuReturn
type var being assigned to a list of whatever each function returns, those beingTuple[bool, bool]
fromfunc1
andTuple[bool, bool]
fromfunc2
. Normally, I'd expect it to go with a union of those two types, that'd collapse to justTuple[bool, bool]
- if the types would be different, for examplestr
andint
, it'd end up asUnion[str, int]
.The text was updated successfully, but these errors were encountered: