mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Fix incorrect truthyness for Enum types and literals

Open Daverball opened this issue 1 year ago • 17 comments

Fixes: #17333

This ensures can_be_true and can_be_false on enum literals depends on the specific Enum fallback type behind the Literal, since __bool__ can be overriden like on any other type.

Additionally typeops true_only and false_only now respect the metaclass when looking up the return values of __bool__ and __len__, which ensures that a default Enum that doesn't override __bool__ is still considered always truthy.

Daverball avatar Jun 06 '24 09:06 Daverball

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 06 '24 10:06 github-actions[bot]

I'm not thrilled about the number of [builtins fixtures/tuple.pyi] I've had to add just so that Literal[True] in enum.pyi works. Maybe it would be better to use union.pyi or define a separate literal.pyi that's even more minimal than union.pyi for now.

Daverball avatar Jun 06 '24 11:06 Daverball

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 06 '24 11:06 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 06 '24 11:06 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 11 '24 16:06 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 13 '24 09:06 github-actions[bot]

BTW I found it a little concerning that TypeInfo.get and TypeInfo.get_method only consider the mro and not the metaclass when looking up symbols, but I didn't have the stomach to change this, since there probably is code that depends on this behavior.

I fear however that there are quite a few more places where attribute lookups are incorrect (other than the one I have fixed in this PR), because the attribute is defined on the metaclass and not on the class itself. Should we perhaps create an issue to potentially review call sites of get/clean this up/improve documentation to avoid more errors in the future?

Daverball avatar Jun 18 '24 07:06 Daverball

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jun 24 '24 14:06 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jul 08 '24 06:07 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jul 16 '24 08:07 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jul 22 '24 15:07 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Jul 30 '24 07:07 github-actions[bot]

@hauntsaninja Not asking for a review (although that would be nice too), but for your thoughts on my comment above: https://github.com/python/mypy/pull/17337#issuecomment-2175328305

Daverball avatar Aug 02 '24 07:08 Daverball

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Aug 06 '24 06:08 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Aug 18 '24 10:08 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Sep 06 '24 08:09 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Oct 12 '24 20:10 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Nov 10 '24 08:11 github-actions[bot]

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Nov 10 '24 09:11 github-actions[bot]