-
Notifications
You must be signed in to change notification settings - Fork 320
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
Check for numeric type using numbers.Real #6802
base: main
Are you sure you want to change the base?
Conversation
isinstance(np.int32(0), int) evaluates to false for example.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6802 +/- ##
==========================================
- Coverage 69.37% 68.31% -1.07%
==========================================
Files 340 341 +1
Lines 31341 31359 +18
==========================================
- Hits 21744 21422 -322
- Misses 9597 9937 +340 ☔ View full report in Codecov by Sentry. |
Thanks @thangleiter I am a bit torn on this. On one hand I fully agree with you. On the other hand the numbers classes don't work well with type checkers. Timing wise this is about 10 times slower than checking just for the build in types In [15]: %timeit isinstance(1, int) In [16]: %timeit isinstance(1, float) In [17]: %timeit isinstance(1, (float, np.float64)) In [18]: %timeit isinstance(1, (float, np.floating)) In [19]: %timeit isinstance(1, np.float64) In [20]: %timeit isinstance(1, (int,float))
|
I see, I wasn't aware of the unclear state of the
I think this is fine in drivers where you know which type you want to pass on. But the most important point where this comes up is Since likely the numpy dtypes are the most relevant, how about introducing a >>> %timeit isinstance(1, (int, float, np.integer, np.floating))
114 ns ± 2.95 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
>>> %timeit isinstance(1, NumbersType)
31.3 ns ± 0.763 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
>>> %timeit isinstance(1, int)
28.3 ns ± 0.556 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each) |
@thangleiter That sounds like a good solution until hopefully a better solution appears upstream. |
Looks like this was still a problem for mypy. The The Tentatively typed |
In several places code tests variables for numeric type using
isinstance(value, (int, float))
. This creates problems for native numpy types (among others I would presume). For example,isinstance(np.int32(0), int)
evaluates to false.Checking instead for
numbers.Real
should resolve this.