#๐Ÿ”’ Why does pyautogui's locate function return None instead of ImageNotFoundException?

33 messages ยท Page 1 of 1 (latest)

ashen flame
#

My packages are up to date

boreal glenBOT
#

@ashen flame

Python help channel opened

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.

versed nymph
ashen flame
chilly crag
#

I wonder if the typehints are directly in the pyautogui source code, or if it's a .pyi from somewhere

carmine depot
#

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 ^^

ashen flame
chilly crag
versed nymph
ashen flame
versed nymph
#

"None" is not iterable

chilly crag
#

according to the docs, it shouldn't be equal to None, but instead raise an error

chilly crag
ashen flame
chilly crag
#

wdym "works"

#

it was "does it raise, or does it return None"

ashen flame
chilly crag
#
GitHub

Collection of library stubs for Python, with static types - python/typeshed

GitHub

Collection of library stubs for Python, with static types - python/typeshed

#

and this still says that it can return None

carmine depot
#

So see if adding # type: ignore to the end of the line with the complaint. I'd be interested to know the result.

chilly crag
#

I would personally use typing.cast()

carmine depot
#

Maybe. Can you? For this? My knowledge of that is slight.

#

I was just going for shutting up pylance.

chilly crag
#
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

boreal glenBOT
#
Python help channel closed for inactivity

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.