-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
False positive with builtins.min(T, T)
if T.__lt__
doesn't return a builtins.bool
#12562
Comments
builtins.min(T, T)
if T.__lt__
doesn't return a builtins.bool
builtins.min(T, T)
if T.__lt__
doesn't return a builtins.bool
I think this ultimately boils down to the definition of typeshed/stdlib/_typeshed/__init__.pyi Lines 87 to 97 in 86e7416
These expect the dunders to return a real |
Unfortunately |
This should be solvable by returning class SupportsBool(Protocol):
def __bool__(self) -> bool: ... |
That protocol is equivalent to |
@JelleZijlstra At runtime yes, but currently the type hints for from typing import Protocol
class SupportsBool(Protocol):
def __bool__(self) -> bool: ...
x: SupportsBool = object() # raises reportAssignmentType I'm not sure why |
OK class Foo:
def __lt__(self, other): return self
def __gt__(self, other): return self
def __le__(self, other): return self
def __ge__(self, other): return self
x = Foo()
y = Foo()
assert min(x, y) is y # ✅
x.__bool__ # ❌ AttributeError |
There is also a very similar definition in Lines 21 to 33 in 8307b3c
|
PR #12573 replaces these However, it makes me wonder whether the |
caused by the more precise type annotation of ``numpy.number`` comparison operators introduced in NumPy 2.1, see: python/typeshed#12562 Fix the following errors when running test_galaxy_packages on Python >=3.10: ``` galaxy/tool_util/verify/__init__.py:503: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "floating[Any]" [type-var] iou_list.append(max(cc1_iou_list)) ^~~~~~~~~~~~~~~~~ galaxy/tool_util/verify/__init__.py:532: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "floating[Any]" [type-var] return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
caused by the more precise type annotation of ``numpy.number`` comparison operators introduced in NumPy 2.1, see: python/typeshed#12562 Fix the following errors when running test_galaxy_packages on Python >=3.10: ``` galaxy/tool_util/verify/__init__.py:503: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "floating[Any]" [type-var] iou_list.append(max(cc1_iou_list)) ^~~~~~~~~~~~~~~~~ galaxy/tool_util/verify/__init__.py:532: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "floating[Any]" [type-var] return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
caused by the more precise type annotation of ``numpy.number`` comparison operators introduced in NumPy 2.1, see: python/typeshed#12562 Fix the following errors when running test_galaxy_packages on Python >=3.10: ``` galaxy/tool_util/verify/__init__.py:503: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "floating[Any]" [type-var] iou_list.append(max(cc1_iou_list)) ^~~~~~~~~~~~~~~~~ galaxy/tool_util/verify/__init__.py:532: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "floating[Any]" [type-var] return min(_multiobject_intersection_over_union(mask1, mask2, pin_labels)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
Since NumPy 2.1 all subtypes of
numpy.number
implement__lt__
that returns anumpy.bool
(which isn't abuiltins.bool
), matching the runtime behavior (numpy/numpy#26942).But as was noticed in numpy/numpy#27251, the consequence of this is that
builtins.min
with e.g.numpy.int32
input is now rejected (tested with the latest mypy and pyright).So at runtime we have
But mypy 1.11.1 rejects
min(np.int32(8), np.int32(9))
as:Similarly, Pyright 1.1.376 reports
and
The text was updated successfully, but these errors were encountered: