8000 Merge pull request #23764 from BvB93/mypy · numpy/numpy@4b849d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b849d4

Browse files
authored
Merge pull request #23764 from BvB93/mypy
CI,TYP: Bump mypy to 1.4.1
2 parents 105cc8d + 8de2ed5 commit 4b849d4

34 files changed

+245
-258
lines changed

environment.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ dependencies:
2525
- pytest-xdist
2626
- hypothesis
2727
# For type annotations
28-
- mypy=0.981
29-
- typing_extensions>=4.2.0
28+
- typing_extensions>=4.2.0 # needed for python < 3.10
29+
- mypy=1.4.1
3030
# For building docs
3131
- sphinx>=4.5.0
3232
- sphinx-design

numpy/core/numeric.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ from typing import (
88
SupportsIndex,
99
NoReturn,
1010
)
11-
from typing_extensions import TypeGuard
11+
if sys.version_info >= (3, 10):
12+
from typing import TypeGuard
13+
else:
14+
from typing_extensions import TypeGuard
1215

1316
from numpy import (
1417
ComplexWarning as ComplexWarning,

numpy/lib/shape_base.pyi

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import sys
12
from collections.abc import Callable, Sequence
23
from typing import TypeVar, Any, overload, SupportsIndex, Protocol
34

5+
if sys.version_info >= (3, 10):
6+
from typing import ParamSpec, Concatenate
7+
else:
8+
from typing_extensions import ParamSpec, Concatenate
9+
410
from numpy import (
511
generic,
612
integer,
@@ -28,6 +34,7 @@ from numpy._typing import (
2834

2935
from numpy.core.shape_base import vstack
3036

37+
_P = ParamSpec("_P")
3138
_SCT = TypeVar("_SCT", bound=generic)
3239

3340
# The signatures of `__array_wrap__` and `__array_prepare__` are the same;
@@ -73,23 +80,21 @@ def put_along_axis(
7380
axis: None | int,
7481
) -> None: ...
7582

76-
# TODO: Use PEP 612 `ParamSpec` once mypy supports `Concatenate`
77-
# xref python/mypy#8645
7883
@overload
7984
def apply_along_axis(
80-
func1d: Callable[..., _ArrayLike[_SCT]],
85+
func1d: Callable[Concatenate[NDArray[Any], _P], _ArrayLike[_SCT]],
8186
axis: SupportsIndex,
8287
arr: ArrayLike,
83-
*args: Any,
84-
**kwargs: Any,
88+
*args: _P.args,
89+
**kwargs: _P.kwargs,
8590
) -> NDArray[_SCT]: ...
8691
@overload
8792
def apply_along_axis(
88-
func1d: Callable[..., ArrayLike],
93+
func1d: Callable[Concatenate[NDArray[Any], _P], ArrayLike],
8994
axis: SupportsIndex,
9095
arr: ArrayLike,
91-
*args: Any,
92-
**kwargs: Any,
96+
*args: _P.args,
97+
**kwargs: _P.kwargs,
9398
) -> NDArray[Any]: ...
9499

95100
def apply_over_axes(

numpy/testing/_private/utils.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ from typing import (
2020
Final,
2121
SupportsIndex,
2222
)
23-
from typing_extensions import ParamSpec
23+
if sys.version_info >= (3, 10):
24+
from typing import ParamSpec
25+
else:
26+
from typing_extensions import ParamSpec
2427

2528
from numpy import generic, dtype, number, object_, bool_, _FloatValue
2629
from numpy._typing import (

numpy/typing/tests/data/fail/modules.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ np.bob # E: Module has no attribute
66
# Stdlib modules in the namespace by accident
77
np.warnings # E: Module has no attribute
88
np.sys # E: Module has no attribute
9-
np.os # E: Module has no attribute
9+
np.os # E: Module "numpy" does not explicitly export
1010
np.math # E: Module has no attribute
1111

1212
# Public sub-modules that are not imported to their parent module by default;

numpy/typing/tests/data/fail/npyio.pyi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ AR_i8: npt.NDArray[np.int64]
1313
np.load(str_file) # E: incompatible type
1414

1515
np.save(bytes_path, AR_i8) # E: incompatible type
16-
np.save(str_file, AR_i8) # E: incompatible type
1716

1817
np.savez(bytes_path, AR_i8) # E: incompatible type
19-
np.savez(str_file, AR_i8) # E: incompatible type
2018

2119
np.savez_compressed(bytes_path, AR_i8) # E: incompatible type
22-
np.savez_compressed(str_file, AR_i8) # E: incompatible type
2320

2421
np.loadtxt(bytes_path) # E: incompatible type
2522

numpy/typing/tests/data/mypy.ini

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,3 @@
22
plugins = numpy.typing.mypy_plugin
33
show_absolute_path = True
44
implicit_reexport = False
5-
6-
[mypy-numpy]
7-
ignore_errors = True
8-
9-
[mypy-numpy.*]
10-
ignore_errors = True

numpy/typing/tests/data/pass/lib_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
FILE = StringIO()
88
AR = np.arange(10, dtype=np.float64)
99

10-
def func(a: int) -> bool: ...
10+
11+
def func(a: int) -> bool:
12+
return True
13+
1114

1215
np.byte_bounds(AR)
1316
np.byte_bounds(np.float64())

numpy/typing/tests/data/pass/ufunc_config.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22

33
import numpy as np
44

5-
def func1(a: str, b: int) -> None: ...
6-
def func2(a: str, b: int, c: float = ...) -> None: ...
7-
def func3(a: str, b: int) -> int: ...
5+
6+
def func1(a: str, b: int) -> None:
7+
return None
8+
9+
10+
def func2(a: str, b: int, c: float = 1.0) -> None:
11+
return None
12+
13+
14+
def func3(a: str, b: int) -> int:
15+
return 0
16+
817

918
class Write1:
10-
def write(self, a: str) -> None: ...
19+
def write(self, a: str) -> None:
20+
return None
21+
1122

1223
class Write2:
13-
def write(self, a: str, b: int = ...) -> None: ...
24+
def write(self, a: str, b: int = 1) -> None:
25+
return None
26+
1427

1528
class Write3:
16-
def write(self, a: str) -> int: ...
29+
def write(self, a: str) -> int:
30+
return 0
1731

1832

1933
_err_default = np.geterr()

numpy/typing/tests/data/reveal/arithmetic.pyi

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -343,184 +343,168 @@ reveal_type(c8 / b_) # E: {complex64}
343343

344344
# Complex
345345

346-
reveal_type(c16 + f16) # E: {complex256}
346+
reveal_type(c16 + f16) # E: complexfloating[Union[_64Bit, _128Bit], Union[_64Bit, _128Bit]]
347347
reveal_type(c16 + c16) # E: {complex128}
348348
reveal_type(c16 + f8) # E: {complex128}
349349
reveal_type(c16 + i8) # E: {complex128}
350-
reveal_type(c16 + c8) # E: {complex128}
351-
reveal_type(c16 + f4) # E: {complex128}
352-
reveal_type(c16 + i4) # E: {complex128}
350+
reveal_type(c16 + c8) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
351+
reveal_type(c16 + f4) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
352+
reveal_type(c16 + i4) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
353353
reveal_type(c16 + b_) # E: {complex128}
354354
reveal_type(c16 + b) # E: {complex128}
355355
reveal_type(c16 + c) # E: {complex128}
356356
reveal_type(c16 + f) # E: {complex128}
357-
reveal_type(c16 + i) # E: {complex128}
358357
reveal_type(c16 + AR_f) # E: Any
359358

360-
reveal_type(f16 + c16) # E: {complex256}
359+
reveal_type(f16 + c16) # E: complexfloating[Union[_64Bit, _128Bit], Union[_64Bit, _128Bit]]
361360
reveal_type(c16 + c16) # E: {complex128}
362361
reveal_type(f8 + c16) # E: {complex128}
363362
reveal_type(i8 + c16) # E: {complex128}
364-
reveal_type(c8 + c16) # E: {complex128}
365-
reveal_type(f4 + c16) # E: {complex128}
366-
reveal_type(i4 + c16) # E: {complex128}
363+
reveal_type(c8 + c16) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
364+
reveal_type(f4 + c16) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
365+
reveal_type(i4 + c16) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
367366
reveal_type(b_ + c16) # E: {complex128}
368367
reveal_type(b + c16) # E: {complex128}
369368
reveal_type(c + c16) # E: {complex128}
370369
reveal_type(f + c16) # E: {complex128}
371-
reveal_type(i + c16) # E: {complex128}
372370
reveal_type(AR_f + c16) # E: Any
373371

374-
reveal_type(c8 + f16) # E: {complex256}
375-
reveal_type(c8 + c16) # E: {complex128}
376-
reveal_type(c8 + f8) # E: {complex128}
377-
reveal_type(c8 + i8) # E: {complex128}
372+
reveal_type(c8 + f16) # E: complexfloating[Union[_32Bit, _128Bit], Union[_32Bit, _128Bit]]
373+
reveal_type(c8 + c16) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
374+
reveal_type(c8 + f8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
375+
reveal_type(c8 + i8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
378376
reveal_type(c8 + c8) # E: {complex64}
379377
reveal_type(c8 + f4) # E: {complex64}
380378
reveal_type(c8 + i4) # E: {complex64}
381379
reveal_type(c8 + b_) # E: {complex64}
382380
reveal_type(c8 + b) # E: {complex64}
383-
reveal_type(c8 + c) # E: {complex128}
384-
reveal_type(c8 + f) # E: {complex128}
385-
reveal_type(c8 + i) # E: complexfloating[{_NBitInt}, {_NBitInt}]
381+
reveal_type(c8 + c) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
382+
reveal_type(c8 + f) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
386383
reveal_type(c8 + AR_f) # E: Any
387384

388-
reveal_type(f16 + c8) # E: {complex256}
389-
reveal_type(c16 + c8) # E: {complex128}
390-
reveal_type(f8 + c8) # E: {complex128}
391-
reveal_type(i8 + c8) # E: {complex128}
385+
reveal_type(f16 + c8) # E: complexfloating[Union[_32Bit, _128Bit], Union[_32Bit, _128Bit]]
386+
reveal_type(c16 + c8) # E: complexfloating[Union[_64Bit, _32Bit], Union[_64Bit, _32Bit]]
387+
reveal_type(f8 + c8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
388+
reveal_type(i8 + c8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
392389
reveal_type(c8 + c8) # E: {complex64}
393390
reveal_type(f4 + c8) # E: {complex64}
394391
reveal_type(i4 + c8) # E: {complex64}
395392
reveal_type(b_ + c8) # E: {complex64}
396393
reveal_type(b + c8) # E: {complex64}
397-
reveal_type(c + c8) # E: {complex128}
398-
reveal_type(f + c8) # E: {complex128}
399-
reveal_type(i + c8) # E: complexfloating[{_NBitInt}, {_NBitInt}]
394+
reveal_type(c + c8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
395+
reveal_type(f + c8) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
400396
reveal_type(AR_f + c8) # E: Any
401397

402398
# Float
403399

404-
reveal_type(f8 + f16) # E: {float128}
400+
reveal_type(f8 + f16) # E: floating[Union[_64Bit, _128Bit]]
405401
reveal_type(f8 + f8) # E: {float64}
406402
reveal_type(f8 + i8) # E: {float64}
407-
reveal_type(f8 + f4) # E: {float64}
408-
reveal_type(f8 + i4) # E: {float64}
403+
reveal_type(f8 + f4) # E: floating[Union[_64Bit, _32Bit]]
404+
reveal_type(f8 + i4) # E: floating[Union[_64Bit, _32Bit]]
409405
reveal_type(f8 + b_) # E: {float64}
410406
reveal_type(f8 + b) # E: {float64}
411407
reveal_type(f8 + c) # E: {complex128}
412408
reveal_type(f8 + f) # E: {float64}
413-
reveal_type(f8 + i) # E: {float64}
414409
reveal_type(f8 + AR_f) # E: Any
415410

416-
reveal_type(f16 + f8) # E: {float128}
411+
reveal_type(f16 + f8) # E: floating[Union[_128Bit, _64Bit]]
417412
reveal_type(f8 + f8) # E: {float64}
418413
reveal_type(i8 + f8) # E: {float64}
419-
reveal_type(f4 + f8) # E: {float64}
420-
reveal_type(i4 + f8) # E: {float64}
414+
reveal_type(f4 + f8) # E: floating[Union[_32Bit, _64Bit]]
415+
reveal_type(i4 + f8) # E: floating[Union[_64Bit, _32Bit]]
421416
reveal_type(b_ + f8) # E: {float64}
422417
reveal_type(b + f8) # E: {float64}
423418
reveal_type(c + f8) # E: {complex128}
424419
reveal_type(f + f8) # E: {float64}
425-
reveal_type(i + f8) # E: {float64}
426420
reveal_type(AR_f + f8) # E: Any
427421

428-
reveal_type(f4 + f16) # E: {float128}
429-
reveal_type(f4 + f8) # E: {float64}
430-
reveal_type(f4 + i8) # E: {float64}
422+
reveal_type(f4 + f16) # E: floating[Union[_32Bit, _128Bit]]
423+
reveal_type(f4 + f8) # E: floating[Union[_32Bit, _64Bit]]
424+
reveal_type(f4 + i8) # E: floating[Union[_32Bit, _64Bit]]
431425
reveal_type(f4 + f4) # E: {float32}
432426
reveal_type(f4 + i4) # E: {float32}
433427
reveal_type(f4 + b_) # E: {float32}
434428
reveal_type(f4 + b) # E: {float32}
435-
reveal_type(f4 + c) # E: {complex128}
436-
reveal_type(f4 + f) # E: {float64}
437-
reveal_type(f4 + i) # E: floating[{_NBitInt}]
429+
reveal_type(f4 + c) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
430+
reveal_type(f4 + f) # E: floating[Union[_32Bit, _64Bit]]
438431
reveal_type(f4 + AR_f) # E: Any
439432

440-
reveal_type(f16 + f4) # E: {float128}
441-
reveal_type(f8 + f4) # E: {float64}
442-
reveal_type(i8 + f4) # E: {float64}
433+
reveal_type(f16 + f4) # E: floating[Union[_128Bit, _32Bit]]
434+
reveal_type(f8 + f4) # E: floating[Union[_64Bit, _32Bit]]
435+
reveal_type(i8 + f4) # E: floating[Union[_32Bit, _64Bit]]
443436
reveal_type(f4 + f4) # E: {float32}
444437
reveal_type(i4 + f4) # E: {float32}
445438
reveal_type(b_ + f4) # E: {float32}
446439
reveal_type(b + f4) # E: {float32}
447-
reveal_type(c + f4) # E: {complex128}
448-
reveal_type(f + f4) # E: {float64}
449-
reveal_type(i + f4) # E: floating[{_NBitInt}]
440+
reveal_type(c + f4) # E: complexfloating[Union[_32Bit, _64Bit], Union[_32Bit, _64Bit]]
441+
reveal_type(f + f4) # E: floating[Union[_32Bit, _64Bit]]
450442
reveal_type(AR_f + f4) # E: Any
451443

452444
# Int
453445

454446
reveal_type(i8 + i8) # E: {int64}
455447
reveal_type(i8 + u8) # E: Any
456-
reveal_type(i8 + i4) # E: {int64}
448+
reveal_type(i8 + i4) # E: signedinteger[Union[_64Bit, _32Bit]]
457449
reveal_type(i8 + u4) # E: Any
458450
reveal_type(i8 + b_) # E: {int64}
459451
reveal_type(i8 + b) # E: {int64}
460452
reveal_type(i8 + c) # E: {complex128}
461453
reveal_type(i8 + f) # E: {float64}
462-
reveal_type(i8 + i) # E: {int64}
463454
reveal_type(i8 + AR_f) # E: Any
464455

465456
reveal_type(u8 + u8) # E: {uint64}
466457
reveal_type(u8 + i4) # E: Any
467-
reveal_type(u8 + u4) # E: {uint64}
458+
reveal_type(u8 + u4) # E: signedinteger[Union[_64Bit, _32Bit]]
468459
reveal_type(u8 + b_) # E: {uint64}
469460
reveal_type(u8 + b) # E: {uint64}
470461
reveal_type(u8 + c) # E: {complex128}
471462
reveal_type(u8 + f) # E: {float64}
472-
reveal_type(u8 + i) # E: Any
473463
reveal_type(u8 + AR_f) # E: Any
474464

475465
reveal_type(i8 + i8) # E: {int64}
476466
reveal_type(u8 + i8) # E: Any
477-
reveal_type(i4 + i8) # E: {int64}
467+
reveal_type(i4 + i8) # E: signedinteger[Union[_32Bit, _64Bit]]
478468
reveal_type(u4 + i8) # E: Any
479469
reveal_type(b_ + i8) # E: {int64}
480470
reveal_type(b + i8) # E: {int64}
481471
reveal_type(c + i8) # E: {complex128}
482472
reveal_type(f + i8) # E: {float64}
483-
reveal_type(i + i8) # E: {int64}
484473
reveal_type(AR_f + i8) # E: Any
485474

486475
reveal_type(u8 + u8) # E: {uint64}
487476
reveal_type(i4 + u8) # E: Any
488-
reveal_type(u4 + u8) # E: {uint64}
477+
reveal_type(u4 + u8) # E: unsignedinteger[Union[_32Bit, _64Bit]]
489478
reveal_type(b_ + u8) # E: {uint64}
490479
reveal_type(b + u8) # E: {uint64}
491480
reveal_type(c + u8) # E: {complex128}
492481
reveal_type(f + u8) # E: {float64}
493-
reveal_type(i + u8) # E: Any
494482
reveal_type(AR_f + u8) # E: Any
495483

496-
reveal_type(i4 + i8) # E: {int64}
484+
reveal_type(i4 + i8) # E: signedinteger[Union[_32Bit, _64Bit]]
497485
reveal_type(i4 + i4) # E: {int32}
498-
reveal_type(i4 + i) # E: {int_}
499486
reveal_type(i4 + b_) # E: {int32}
500487
reveal_type(i4 + b) # E: {int32}
501488
reveal_type(i4 + AR_f) # E: Any
502489

503490
reveal_type(u4 + i8) # E: Any
504491
reveal_type(u4 + i4) # E: Any
505-
reveal_type(u4 + u8) # E: {uint64}
492+
reveal_type(u4 + u8) # E: unsignedinteger[Union[_32Bit, _64Bit]]
506493
reveal_type(u4 + u4) # E: {uint32}
507-
reveal_type(u4 + i) # E: Any
508494
reveal_type(u4 + b_) # E: {uint32}
509495
reveal_type(u4 + b) # E: {uint32}
510496
reveal_type(u4 + AR_f) # E: Any
511497

512-
reveal_type(i8 + i4) # E: {int64}
498+
reveal_type(i8 + i4) # E: signedinteger[Union[_64Bit, _32Bit]]
513499
reveal_type(i4 + i4) # E: {int32}
514-
reveal_type(i + i4) # E: {int_}
515500
reveal_type(b_ + i4) # E: {int32}
516501
reveal_type(b + i4) # E: {int32}
517502
reveal_type(AR_f + i4) # E: Any
518503

519504
reveal_type(i8 + u4) # E: Any
520505
reveal_type(i4 + u4) # E: Any
521-
reveal_type(u8 + u4) # E: {uint64}
506+
reveal_type(u8 + u4) # E: unsignedinteger[Union[_64Bit, _32Bit]]
522507
reveal_type(u4 + u4) # E: {uint32}
523508
reveal_type(b_ + u4) # E: {uint32}
524509
reveal_type(b + u4) # E: {uint32}
525-
reveal_type(i + u4) # E: Any
526510
reveal_type(AR_f + u4) # E: Any

0 commit comments

Comments
 (0)
0