#๐ Why does pyautogui's locate function return None instead of ImageNotFoundException?
33 messages ยท Page 1 of 1 (latest)
@ashen flame
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what version do you have installed?
of pyautogui? 0.9.54
I wonder if the typehints are directly in the pyautogui source code, or if it's a .pyi from somewhere
Yeah, it looks like pylance has the idea that it could still return None, and thus complains.
You can test this: run it with some image known not to be found, see if it raises or returns None.
@ashen flame ^^
Tried it, do i just make pylance ignore it ?
or how about... tell us what happened?
seems fine to me. Are you looping or iterating the result of the locateCenterOnScreen?
Itโs not that itโs not working, itโs just that my editor is raising an error
the error is saying that its not iterable. hence, my question
"None" is not iterable
according to the docs, it shouldn't be equal to None, but instead raise an error
what was the outcome?
Yea it works
imagenotfound exception, yes
ok, so it is an issue with pylance not knowing the current typehints
https://github.com/python/typeshed/blob/main/stubs/PyAutoGUI/pyautogui/__init__.pyi
leads to
https://github.com/python/typeshed/blob/f8cdc0bd526301e873cd952eb0d457bdf2554e57/stubs/PyScreeze/pyscreeze/__init__.pyi#L140
and this still says that it can return None
@ashen flame you can suppress specific complaints as described here: https://microsoft.github.io/pyright/#/comments?id=line-level-diagnostic-suppression
(Pylance seems to use pyright for the type checking. I think. From following some links.)
Description
So see if adding # type: ignore to the end of the line with the complaint. I'd be interested to know the result.
I would personally use typing.cast()
Maybe. Can you? For this? My knowledge of that is slight.
I was just going for shutting up pylance.
from typing import cast
def f() -> str | None: ...
print(cast(str, f()).lower())
similar to this
it depends on what the full line of code is i guess
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.