Skip to content

Commit bce38b4

Browse files
Merge pull request #534 from jorenham/static-typing
Static typing support
2 parents cd730c8 + 8ba43af commit bce38b4

File tree

17 files changed

+837
-510
lines changed

17 files changed

+837
-510
lines changed

.github/workflows/typecheck.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Validate static types
2+
permissions: read-all
3+
4+
on:
5+
pull_request:
6+
paths:
7+
- .github/workflows/typecheck.yml
8+
- numexpr/*
9+
- pyproject.toml
10+
workflow_dispatch:
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
typecheck_quaddtype:
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 2
20+
21+
steps:
22+
- uses: actions/checkout@v5.0.0
23+
24+
- uses: astral-sh/setup-uv@v6.7.0
25+
with:
26+
activate-environment: true
27+
python-version: "3.10"
28+
29+
- name: install
30+
run: uv pip install mypy pyright pytest .
31+
32+
- name: pyright
33+
run: pyright
34+
35+
- name: mypy
36+
run: mypy --no-incremental --cache-dir=/dev/null .

.pre-commit-config.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ repos:
1818
hooks:
1919
- id: isort
2020

21-
# Too many things to fix, let's just ignore it for now
22-
#- repo: https://github.com/pre-commit/mirrors-mypy
23-
# rev: v1.8.0
24-
# hooks:
25-
# - id: mypy
26-
# exclude: ^(docs/|setup.py)
21+
- repo: https://github.com/pre-commit/mirrors-mypy
22+
rev: v1.18.2
23+
hooks:
24+
- id: mypy
25+
args: [--config-file=pyproject.toml]
26+
exclude: ^(bench/|build/|doc/|issues/|setup.py)
27+
additional_dependencies: [numpy, pytest]

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include MANIFEST.in VERSION
22

33
include *.rst *.txt *.cfg site.cfg.example
44

5-
recursive-include numexpr *.cpp *.hpp *.py
5+
recursive-include numexpr *.cpp *.hpp *.py *.pyi py.typed
66
recursive-include numexpr/win32 *.c *.h
77
exclude numexpr/__config__.py RELEASING.txt site.cfg
88

RELEASE_NOTES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ Changes from 2.13.1 to 2.13.2
77

88
* **Under development.**
99

10+
* Static typing support has been added, making NumExpr compatible with
11+
static type checkers like `mypy` and `pyright`.
12+
Thanks to Joren Hammudoglu (@jorenham) for the work.
13+
14+
1015
Changes from 2.13.0 to 2.13.1
1116
-----------------------------
1217

1318
* Patch to maximum/minimum functions in order to match NumPy NaN handling
1419
* Patch to convert '+'->'|' and '*'->'&' for booleans
1520
21+
1622
Changes from 2.12.1 to 2.13.0
1723
-----------------------------
1824

numexpr/__init__.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,62 @@
2121
2222
"""
2323

24-
from numexpr.interpreter import __BLOCK_SIZE1__, MAX_THREADS, use_vml
24+
from typing import TYPE_CHECKING, Final
2525

26-
is_cpu_amd_intel = False # DEPRECATION WARNING: WILL BE REMOVED IN FUTURE RELEASE
26+
if TYPE_CHECKING:
27+
import unittest
28+
29+
# the `import _ as _` are needed for mypy to understand these are re-exports
30+
31+
from numexpr.interpreter import __BLOCK_SIZE1__ as __BLOCK_SIZE1__
32+
from numexpr.interpreter import MAX_THREADS as MAX_THREADS
33+
from numexpr.interpreter import use_vml as use_vml
34+
35+
is_cpu_amd_intel: Final = False # DEPRECATION WARNING: WILL BE REMOVED IN FUTURE RELEASE
2736

2837
# cpuinfo imports were moved into the test submodule function that calls them
2938
# to improve import times.
3039

31-
from numexpr.expressions import E
32-
from numexpr.necompiler import (NumExpr, disassemble, evaluate, re_evaluate,
33-
validate)
34-
from numexpr.utils import (_init_num_threads, detect_number_of_cores,
35-
detect_number_of_threads, get_num_threads,
36-
get_vml_version, set_num_threads,
37-
set_vml_accuracy_mode, set_vml_num_threads)
40+
from numexpr.expressions import E as E
41+
from numexpr.necompiler import NumExpr as NumExpr
42+
from numexpr.necompiler import disassemble as disassemble
43+
from numexpr.necompiler import evaluate as evaluate
44+
from numexpr.necompiler import re_evaluate as re_evaluate
45+
from numexpr.necompiler import validate as validate
46+
from numexpr.utils import _init_num_threads
47+
from numexpr.utils import detect_number_of_cores as detect_number_of_cores
48+
from numexpr.utils import detect_number_of_threads as detect_number_of_threads
49+
from numexpr.utils import get_num_threads as get_num_threads
50+
from numexpr.utils import get_vml_version as get_vml_version
51+
from numexpr.utils import set_num_threads as set_num_threads
52+
from numexpr.utils import set_vml_accuracy_mode as set_vml_accuracy_mode
53+
from numexpr.utils import set_vml_num_threads as set_vml_num_threads
3854

3955
# Detect the number of cores
40-
ncores = detect_number_of_cores()
56+
ncores: Final = detect_number_of_cores()
4157
# Initialize the number of threads to be used
42-
nthreads = _init_num_threads()
58+
nthreads: Final = _init_num_threads()
4359
# The default for VML is 1 thread (see #39)
4460
# set_vml_num_threads(1)
4561

46-
from . import version
62+
from . import version as version
4763

48-
__version__ = version.version
64+
__version__: Final = version.version
4965

50-
def print_versions():
66+
def print_versions() -> None:
5167
"""Print the versions of software that numexpr relies on."""
5268
try:
5369
import numexpr.tests
54-
return numexpr.tests.print_versions()
70+
return numexpr.tests.print_versions() # type: ignore[no-untyped-call]
5571
except ImportError:
5672
# To maintain Python 2.6 compatibility we have simple error handling
5773
raise ImportError('`numexpr.tests` could not be imported, likely it was excluded from the distribution.')
5874

59-
def test(verbosity=1):
75+
def test(verbosity: int = 1) -> "unittest.result.TestResult":
6076
"""Run all the tests in the test suite."""
6177
try:
6278
import numexpr.tests
63-
return numexpr.tests.test(verbosity=verbosity)
79+
return numexpr.tests.test(verbosity=verbosity) # type: ignore[no-untyped-call]
6480
except ImportError:
6581
# To maintain Python 2.6 compatibility we have simple error handling
6682
raise ImportError('`numexpr.tests` could not be imported, likely it was excluded from the distribution.')

0 commit comments

Comments
 (0)