Skip to content
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

Let SupportsDunderLT return SupportsBool instead of bool. #12939

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Nov 1, 2024

Fixes #12562, alternative to #12573. Introduces a new protocol in _typeshed/__init__.pyi

class SupportsBool(Protocol):
    def __bool__(self) -> bool: ...

Changes SupportsDunderLT and variants to return SupportsBool instead of bool.

Note that an earlier comment by JelleZijlstra suggested that SupportsBool is equivalent to object in practice. This does not seem to quite be the case: Code sample in pyright playground

from typing import Protocol, runtime_checkable

@runtime_checkable
class SupportsBool(Protocol):
    def __bool__(self) -> bool: ...

x: SupportsBool = True
y: SupportsBool = object()  # mypy: ❌ pyright:  ❌

assert isinstance(x, SupportsBool)  # ✅
assert not isinstance(y, SupportsBool)  # ✅

Indeed, despite the documentation mentioning object.__bool__, this method is not defined on object. Rather, the truthiness of an object is defined roughly like:

if hasattr(obj, "__bool__"):
    return obj.__bool__()
if hasattr(obj, "__len__"):
   return bool(len(obj))
return True

This comment has been minimized.

@randolf-scholz randolf-scholz marked this pull request as ready for review November 1, 2024 13:57
@randolf-scholz randolf-scholz changed the title added SupportsBool Let SupportsDunderLT return SupportsBool instead of bool. Nov 1, 2024
@jorenham
Copy link
Contributor

jorenham commented Nov 1, 2024

Even though this technically doesn't cover all cases, I like this "explicit is better than implicit" approach, and I'm a big fan of structural typing solutions like these 👌🏻.

@randolf-scholz
Copy link
Contributor Author

randolf-scholz commented Nov 1, 2024 via email

@jorenham
Copy link
Contributor

jorenham commented Nov 1, 2024

@randolf-scholz ahh good to know 👍🏻

This comment has been minimized.

@randolf-scholz
Copy link
Contributor Author

randolf-scholz commented Nov 5, 2024

Ibis can easily fix the issue by adding __bool__ to the BooleanValue class (via Value.__ge__, the superclass of NumericValue)

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/relations.py:2924: error: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "NumericValue | float"  [type-var]

jax (https://github.com/google/jax)
+ jax/_src/pallas/mosaic/pipeline.py:981: error: Unused "type: ignore" comment  [unused-ignore]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive with builtins.min(T, T) if T.__lt__ doesn't return a builtins.bool
2 participants