FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

enable the mypy "truthy-bool" error code by jorenham · Pull Request #9409 · python-pillow/Pillow · GitHub

enable the mypy "truthy-bool" error code - #9409

Closed
jorenham wants to merge 4 commits into
python-pillow:mainfrom
jorenham:typing/truthy-bool
Closed

enable the mypy "truthy-bool" error code#9409
jorenham wants to merge 4 commits into
python-pillow:mainfrom
jorenham:typing/truthy-bool

Conversation

Copy link
Copy Markdown
Contributor

This is one of the additional checks that are required by repo-review (MY106): https://learn.scientific-python.org/development/guides/style/#MY106
IMO it's more of a linting rule than a type-checking rule, but either way, I think it's a pretty decent one to enable. FWIW; In the projects I maintain (numpy, scipy-stubs, ...), I always enable it.

mypy docs: https://mypy.readthedocs.io/en/stable/error_code_list2.html#check-that-expression-is-not-implicitly-true-in-boolean-context-truthy-bool

Comment thread src/PIL/Image.py

try:
from . import BmpImagePlugin
from . import BmpImagePlugin as BmpImagePlugin

jorenham Jan 27, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This might look a bit weird (unless you're used to writing type stubs), but it avoids a # noqa. I wouldn't mind using # noqa here instead if that's preferred

Comment thread src/PIL/ImageFont.py
pass
else:
if image and image.mode in ("1", "L"):
if image.mode in ("1", "L"):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

at this point image cannot be None anymore

Comment thread Tests/test_pyarrow.py Outdated

reloaded = Image.fromarrow(arr, mode, img.size)

assert reloaded

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I could also change this to an assert isinstance or something, but I figured that'd also be kinda obvious.

# Segfault test
app: QApplication | None = QApplication([])
ex = Example()
ex = Example() # noqa: F841

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm not sure what the idea here is, but it looked as if the assert was purely there to avoid this F841 error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is it not preferable to avoid noqa where possible? My solution would have been assert ex is not None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Personally I prefer being explicit about ignored errors. And assert statements aren't "free" (the runtime impact is probably negligible, but still).
And, if at some point, some change causes this so that ex could actually become None, then ruff will complain about an unused # noqa, which would prevent a potential bug.

hugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think these changes do much to improve the readability of the code.

Some of the existing hints make core less readable and were to silence mypy. Pillow is an old code base (31 years old!), so I think we need to careful about introducing more strictness.

This is one of the additional checks that are required by repo-review (MY106): learn.scientific-python.org/development/guides/style#MY106 IMO it's more of a linting rule than a type-checking rule, but either way, I think it's a pretty decent one to enable. FWIW; In the projects I maintain (numpy, scipy-stubs, ...), I always enable it.

That link says:

The extra strict options shown above, like warn_unreachable MY103, and ignore-without-code MY104, redundant-expr MY105, and truthy-bool MY106 can trigger too often (like on sys.platform checks) and have to be ignored occasionally, but can find some significant logic errors in your typing.

It seems to be triggering too often in this case, let's skip this one.

jorenham commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Seems fair; closing

jorenham closed this Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL