Repository: numba/numba

Issue:
cuda stubs raising mypy SyntaxError
- [ ] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
- [ ] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.


We're seeing:

```
    import cudf
/conda/envs/rapids/lib/python3.7/site-packages/cudf/__init__.py:4: in <module>
    validate_setup()
/conda/envs/rapids/lib/python3.7/site-packages/cudf/utils/gpu_utils.py:18: in validate_setup
    from rmm._cuda.gpu import (
/conda/envs/rapids/lib/python3.7/site-packages/rmm/__init__.py:16: in <module>
    from rmm import mr
/conda/envs/rapids/lib/python3.7/site-packages/rmm/mr.py:2: in <module>
    from rmm._lib.memory_resource import (
/conda/envs/rapids/lib/python3.7/site-packages/rmm/_lib/__init__.py:3: in <module>
    from .device_buffer import DeviceBuffer
rmm/_lib/device_buffer.pyx:1: in init rmm._lib.device_buffer
    ???
rmm/_cuda/stream.pyx:26: in init rmm._cuda.stream
    ???
/conda/envs/rapids/lib/python3.7/site-packages/numba/cuda/__init__.py:7: in <module>
    from .device_init import *
/conda/envs/rapids/lib/python3.7/site-packages/numba/cuda/device_init.py:2: in <module>
    from .stubs import (threadIdx, blockIdx, blockDim, gridDim, laneid,
E     File "/conda/envs/rapids/lib/python3.7/site-packages/numba/cuda/stubs.py", line 449
E       """  # noqa: W605
E                      ^
E   SyntaxError: invalid escape sequence \|
```

Seemingly corresponding to the `\|` in https://github.com/numba/numba/blob/468647dddde27ee8af124c97dfcd20c35c4a2bc6/numba/cuda/stubs.py#L468 

Our mypy config is:

```
[mypy-numba.*]
ignore_missing_imports = True
ignore_errors = True
```

Versions:

```
+ python --version
Python 3.7.10
+ mypy --version
mypy 0.950 (compiled: yes)
```



Discussion:
I've not looked into this yet, but I'm adding it to the 0.56 milestone to make sure it gets investigated by then as this sounds a bit annoying and should hopefully be straightforward to fix.
Also, testing a bit more... `mypy` is fine, it's `pytest`, both python 3.7 + 3.8, and pytest 6 + 7.

Checking more if live code as well or just pytest..

EDIT:  Seems like pytest fails while python itself is fine, but confusingly, call tree is in pytest's exec, which I think is an import, not a custom parse:

```
docker run --rm -it --entrypoint=/bin/bash graphistry/graphistry-nvidia:v2.39.13-11.4 -c 'python -c "import cudf; print(\"ok\")"'
ok
```

=> `ok`

vs pytest:

```
=== Testing ===
+ echo '=== Testing ==='
+ [[ 1 != \0 ]]
+ ./bin/test.sh --maxfail=1
+ python --version
Python 3.8.13
+ python3 --version
Python 3.8.13
+ python -m pytest --version
pytest 7.1.2
+ python -B -m pytest -vv --maxfail=1
============================= test session starts ==============================
platform linux -- Python 3.8.13, pytest-7.1.2, pluggy-1.0.0 -- /opt/conda/envs/rapids/bin/python
cachedir: .pytest_cache
rootdir: /opt/pygraphistry, configfile: pytest.ini
plugins: anyio-3.5.0, timeout-1.4.2, asyncio-0.14.0
collecting ... collected 0 items / 1 error

==================================== ERRORS ====================================
_________ ERROR collecting graphistry/tests/test_ArrowFileUploader.py __________
../conda/envs/rapids/lib/python3.8/site-packages/_pytest/python.py:608: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
../conda/envs/rapids/lib/python3.8/site-packages/_pytest/pathlib.py:533: in import_path
    importlib.import_module(module_name)
../conda/envs/rapids/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
../conda/envs/rapids/lib/python3.8/site-packages/_pytest/assertion/rewrite.py:168: in exec_module
    exec(co, module.__dict__)
graphistry/tests/test_ArrowFileUploader.py:3: in <module>
    from graphistry.arrow_uploader import ArrowUploader
graphistry/__init__.py:2: in <module>
    from graphistry.pygraphistry import (  # noqa: E402, F401
graphistry/pygraphistry.py:14: in <module>
    from .plotter import Plotter
graphistry/plotter.py:1: in <module>
    from .PlotterBase import PlotterBase
graphistry/PlotterBase.py:39: in <module>
    import cudf
../conda/envs/rapids/lib/python3.8/site-packages/cudf/__init__.py:5: in <module>
    validate_setup()
../conda/envs/rapids/lib/python3.8/site-packages/cudf/utils/gpu_utils.py:20: in validate_setup
    from rmm._cuda.gpu import (
../conda/envs/rapids/lib/python3.8/site-packages/rmm/__init__.py:16: in <module>
    from rmm import mr
../conda/envs/rapids/lib/python3.8/site-packages/rmm/mr.py:14: in <module>
    from rmm._lib.memory_resource import (
../conda/envs/rapids/lib/python3.8/site-packages/rmm/_lib/__init__.py:15: in <module>
    from .device_buffer import DeviceBuffer
device_buffer.pyx:1: in init rmm._lib.device_buffer
    ???
stream.pyx:26: in init rmm._cuda.stream
    ???
../conda/envs/rapids/lib/python3.8/site-packages/numba/cuda/__init__.py:7: in <module>
    from .device_init import *
../conda/envs/rapids/lib/python3.8/site-packages/numba/cuda/device_init.py:2: in <module>
    from .stubs import (threadIdx, blockIdx, blockDim, gridDim, laneid,
E     File "/opt/conda/envs/rapids/lib/python3.8/site-packages/numba/cuda/stubs.py", line 466
E       """or_(ary, idx, val)
E       ^
E   SyntaxError: invalid escape sequence \|
=========================== short test summary info ============================
ERROR graphistry/tests/test_ArrowFileUploader.py
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 1.76s ===============================
```
Hmm I'm still confused on root cause . `git blame` shows no changes over 2 years: https://github.com/numba/numba/blame/468647dddde27ee8af124c97dfcd20c35c4a2bc6/numba/cuda/stubs.py#L465

Will try rolling pytest back further..

EDIT: pytest 6.2 is from 2020, so guessing about python itself (ultimate base here is coming from nvidia's official RAPIDS 2022.04 docker containers)
Found it: It's a `DeprecationWarning`, but we treat those as errors, so PyTest turns into a full `SyntaxError`. Unclear when the targeted sunset is: https://github.com/python/cpython/issues/77093 
This issue is marked as stale as it has had no activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with any updates and confirm that this issue still needs to be addressed.
Still pretty real for us :) (stale bot)
Sorry this got missed for 0.56 - I'm not sure how it's ended up in 0.57RC as I only see myself adding it to 0.56RC a few weeks ago.
Ah I see @sklam moved it on 1 Jun.

Resolving patch:
diff --git a/numba/cuda/stubs.py b/numba/cuda/stubs.py
--- a/numba/cuda/stubs.py
+++ b/numba/cuda/stubs.py
@@ -439,7 +439,7 @@ class atomic(Stub):
     class add(Stub):
         """add(ary, idx, val)
 
-        Perform atomic ary[idx] += val. Supported on int32, float32, and
+        Perform atomic ``ary[idx] += val``. Supported on int32, float32, and
         float64 operands only.
 
         Returns the old value at the index location as if it is loaded
@@ -449,7 +449,7 @@ class add(Stub):
     class sub(Stub):
         """sub(ary, idx, val)
 
-        Perform atomic ary[idx] -= val. Supported on int32, float32, and
+        Perform atomic ``ary[idx] -= val``. Supported on int32, float32, and
         float64 operands only.
 
         Returns the old value at the index location as if it is loaded
@@ -459,8 +459,8 @@ class sub(Stub):
     class and_(Stub):
         """and_(ary, idx, val)
 
-        Perform atomic ary[idx] &= val. Supported on int32, int64, uint32 and
-        uint64 operands only.
+        Perform atomic ``ary[idx] &= val``. Supported on int32, int64, uint32
+        and uint64 operands only.
 
         Returns the old value at the index location as if it is loaded
         atomically.
@@ -469,18 +469,18 @@ class and_(Stub):
     class or_(Stub):
         """or_(ary, idx, val)
 
-        Perform atomic ary[idx] \|= val. Supported on int32, int64, uint32 and
-        uint64 operands only.
+        Perform atomic ``ary[idx] |= val``. Supported on int32, int64, uint32
+        and uint64 operands only.
 
         Returns the old value at the index location as if it is loaded
         atomically.
-        """  # noqa: W605
+        """
 
     class xor(Stub):
         """xor(ary, idx, val)
 
-        Perform atomic ary[idx] ^= val. Supported on int32, int64, uint32 and
-        uint64 operands only.
+        Perform atomic ``ary[idx] ^= val``. Supported on int32, int64, uint32
+        and uint64 operands only.
 
         Returns the old value at the index location as if it is loaded
         atomically.
@@ -489,7 +489,7 @@ class xor(Stub):
     class inc(Stub):
         """inc(ary, idx, val)
 
-        Perform atomic ary[idx] += 1 up to val, then reset to 0. Supported
+        Perform atomic ``ary[idx] += 1`` up to val, then reset to 0. Supported
         on uint32, and uint64 operands only.
 
         Returns the old value at the index location as if it is loaded
@@ -499,8 +499,10 @@ class inc(Stub):
     class dec(Stub):
         """dec(ary, idx, val)
 
-        Perform ary[idx] = (value if (array[idx] == 0) or
-        (array[idx] > value) else array[idx] - 1).
+        Performs::
+
+           ary[idx] = (value if (array[idx] == 0) or
+                       (array[idx] > value) else array[idx] - 1)
 
         Supported on uint32, and uint64 operands only.
 
@@ -511,7 +513,7 @@ class dec(Stub):
     class exch(Stub):
         """exch(ary, idx, val)
 
-        Perform atomic ary[idx] = val. Supported on int32, int64, uint32 and
+        Perform atomic ``ary[idx] = val``. Supported on int32, int64, uint32 and
         uint64 operands only.
 
         Returns the old value at the index location as if it is loaded
@@ -521,7 +523,7 @@ class exch(Stub):
     class max(Stub):
         """max(ary, idx, val)
 
-        Perform atomic ary[idx] = max(ary[idx], val).
+        Perform atomic ``ary[idx] = max(ary[idx], val)``.
 
         Supported on int32, int64, uint32, uint64, float32, float64 operands
         only.
@@ -533,7 +535,7 @@ class max(Stub):
     class min(Stub):
         """min(ary, idx, val)
 
-        Perform atomic ary[idx] = min(ary[idx], val).
+        Perform atomic ``ary[idx] = min(ary[idx], val)``.
 
         Supported on int32, int64, uint32, uint64, float32, float64 operands
         only.
@@ -545,7 +547,7 @@ class min(Stub):
     class nanmax(Stub):
         """nanmax(ary, idx, val)
 
-        Perform atomic ary[idx] = max(ary[idx], val).
+        Perform atomic ``ary[idx] = max(ary[idx], val)``.
 
         NOTE: NaN is treated as a missing value such that:
         nanmax(NaN, n) == n, nanmax(n, NaN) == n
@@ -560,7 +562,7 @@ class nanmax(Stub):
     class nanmin(Stub):
         """nanmin(ary, idx, val)
 
-        Perform atomic ary[idx] = min(ary[idx], val).
+        Perform atomic ``ary[idx] = min(ary[idx], val)``.
 
         NOTE: NaN is treated as a missing value, such that:
         nanmin(NaN, n) == n, nanmin(n, NaN) == n


Test patch: