Repository: numba/numba

Issue:
numba 0.55 breaks compatibility with coverage
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->

Since numba 0.55 python files importing numba are causing trouble in combination with coverage xml.

### Example

"example.py":
```python
from numba import jit
import numpy as np


@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def go_fast(a):
    trace = 0.0
    for i in range(a.shape[0]):
        trace += np.tanh(a[i, i])
    return a + trace

def main():
    x = np.arange(100).reshape(10, 10)
    print(go_fast(x))


if __name__ == '__main__':
    main()
```

**Expected behavior** (as with numba 0.54)
```bash
$ python -m coverage run example.py
[[  9.  10.  11.  12.  13.  14.  15.  16.  17.  18.]
 [ 19.  20.  21.  22.  23.  24.  25.  26.  27.  28.]
 [ 29.  30.  31.  32.  33.  34.  35.  36.  37.  38.]
 [ 39.  40.  41.  42.  43.  44.  45.  46.  47.  48.]
 [ 49.  50.  51.  52.  53.  54.  55.  56.  57.  58.]
 [ 59.  60.  61.  62.  63.  64.  65.  66.  67.  68.]
 [ 69.  70.  71.  72.  73.  74.  75.  76.  77.  78.]
 [ 79.  80.  81.  82.  83.  84.  85.  86.  87.  88.]
 [ 89.  90.  91.  92.  93.  94.  95.  96.  97.  98.]
 [ 99. 100. 101. 102. 103. 104. 105. 106. 107. 108.]]
$ python -m coverage xml
Wrote XML report to coverage.xml
```

**Found**
```
$ python -m coverage run example.py
[[  9.  10.  11.  12.  13.  14.  15.  16.  17.  18.]
 [ 19.  20.  21.  22.  23.  24.  25.  26.  27.  28.]
 [ 29.  30.  31.  32.  33.  34.  35.  36.  37.  38.]
 [ 39.  40.  41.  42.  43.  44.  45.  46.  47.  48.]
 [ 49.  50.  51.  52.  53.  54.  55.  56.  57.  58.]
 [ 59.  60.  61.  62.  63.  64.  65.  66.  67.  68.]
 [ 69.  70.  71.  72.  73.  74.  75.  76.  77.  78.]
 [ 79.  80.  81.  82.  83.  84.  85.  86.  87.  88.]
 [ 89.  90.  91.  92.  93.  94.  95.  96.  97.  98.]
 [ 99. 100. 101. 102. 103. 104. 105. 106. 107. 108.]]
$ python -m coverage xml
No source for code: '/path/to/working/directory/__numba_array_expr_synthetic_module_example.py'.
```

coverage version is 6.2.

Many thanks for looking into this.\
Best, Emanuel

numba 0.55 breaks compatibility with coverage
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->

Since numba 0.55 python files importing numba are causing trouble in combination with coverage xml.

### Example

"example.py":
```python
from numba import jit
import numpy as np


@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def go_fast(a):
    trace = 0.0
    for i in range(a.shape[0]):
        trace += np.tanh(a[i, i])
    return a + trace

def main():
    x = np.arange(100).reshape(10, 10)
    print(go_fast(x))


if __name__ == '__main__':
    main()
```

**Expected behavior** (as with numba 0.54)
```bash
$ python -m coverage run example.py
[[  9.  10.  11.  12.  13.  14.  15.  16.  17.  18.]
 [ 19.  20.  21.  22.  23.  24.  25.  26.  27.  28.]
 [ 29.  30.  31.  32.  33.  34.  35.  36.  37.  38.]
 [ 39.  40.  41.  42.  43.  44.  45.  46.  47.  48.]
 [ 49.  50.  51.  52.  53.  54.  55.  56.  57.  58.]
 [ 59.  60.  61.  62.  63.  64.  65.  66.  67.  68.]
 [ 69.  70.  71.  72.  73.  74.  75.  76.  77.  78.]
 [ 79.  80.  81.  82.  83.  84.  85.  86.  87.  88.]
 [ 89.  90.  91.  92.  93.  94.  95.  96.  97.  98.]
 [ 99. 100. 101. 102. 103. 104. 105. 106. 107. 108.]]
$ python -m coverage xml
Wrote XML report to coverage.xml
```

**Found**
```
$ python -m coverage run example.py
[[  9.  10.  11.  12.  13.  14.  15.  16.  17.  18.]
 [ 19.  20.  21.  22.  23.  24.  25.  26.  27.  28.]
 [ 29.  30.  31.  32.  33.  34.  35.  36.  37.  38.]
 [ 39.  40.  41.  42.  43.  44.  45.  46.  47.  48.]
 [ 49.  50.  51.  52.  53.  54.  55.  56.  57.  58.]
 [ 59.  60.  61.  62.  63.  64.  65.  66.  67.  68.]
 [ 69.  70.  71.  72.  73.  74.  75.  76.  77.  78.]
 [ 79.  80.  81.  82.  83.  84.  85.  86.  87.  88.]
 [ 89.  90.  91.  92.  93.  94.  95.  96.  97.  98.]
 [ 99. 100. 101. 102. 103. 104. 105. 106. 107. 108.]]
$ python -m coverage xml
No source for code: '/path/to/working/directory/__numba_array_expr_synthetic_module_example.py'.
```

coverage version is 6.2.

Many thanks for looking into this.\
Best, Emanuel



Discussion:
@emanuel-schmid thank you for reporting this. Could you also list the Python version you have been using, thank you!
Oops, sorry: python 3.8.10
> Oops, sorry: python 3.8.10

No problem, thank you!
Many thanks for the report - I can reproduce the issue.

(I'm adding this to 0.55.1 because we should at least figure out whether to address this for 0.55.1)
I suspect #7177 may have introduced this.
It was #7177: https://github.com/numba/numba/pull/7177/commits/41a7db05b45f501d49decd2d5896ac1f0b5089b5 introduced the issue.
Applying:

```diff
diff --git a/numba/np/ufunc/array_exprs.py b/numba/np/ufunc/array_exprs.py
index 3fe85ba5e..bb8815ddc 100644
--- a/numba/np/ufunc/array_exprs.py
+++ b/numba/np/ufunc/array_exprs.py
@@ -333,7 +333,7 @@ def _lower_array_expr(lowerer, expr):
     '''Lower an array expression built by RewriteArrayExprs.
     '''
     expr_name = "__numba_array_expr_%s" % (hex(hash(expr)).replace("-", "_"))
-    expr_filename = f"__numba_array_expr_synthetic_module_{expr.loc.filename}"
+    expr_filename = expr.loc.filename
     expr_var_list = expr.list_vars()
     # The expression may use a given variable several times, but we
     # should only create one parameter for it.
```

appears to resolve this issue, and doesn't introduce any failures in any of the tests added or modified in #7177.
@emanuel-schmid thank you for reporting this. Could you also list the Python version you have been using, thank you!
Oops, sorry: python 3.8.10
> Oops, sorry: python 3.8.10

No problem, thank you!
Many thanks for the report - I can reproduce the issue.

(I'm adding this to 0.55.1 because we should at least figure out whether to address this for 0.55.1)
I suspect #7177 may have introduced this.
It was #7177: https://github.com/numba/numba/pull/7177/commits/41a7db05b45f501d49decd2d5896ac1f0b5089b5 introduced the issue.
Applying:

```diff
diff --git a/numba/np/ufunc/array_exprs.py b/numba/np/ufunc/array_exprs.py
index 3fe85ba5e..bb8815ddc 100644
--- a/numba/np/ufunc/array_exprs.py
+++ b/numba/np/ufunc/array_exprs.py
@@ -333,7 +333,7 @@ def _lower_array_expr(lowerer, expr):
     '''Lower an array expression built by RewriteArrayExprs.
     '''
     expr_name = "__numba_array_expr_%s" % (hex(hash(expr)).replace("-", "_"))
-    expr_filename = f"__numba_array_expr_synthetic_module_{expr.loc.filename}"
+    expr_filename = expr.loc.filename
     expr_var_list = expr.list_vars()
     # The expression may use a given variable several times, but we
     # should only create one parameter for it.
```

appears to resolve this issue, and doesn't introduce any failures in any of the tests added or modified in #7177.

Resolving patch:
diff --git a/numba/np/ufunc/array_exprs.py b/numba/np/ufunc/array_exprs.py
--- a/numba/np/ufunc/array_exprs.py
+++ b/numba/np/ufunc/array_exprs.py
@@ -333,7 +333,7 @@ def _lower_array_expr(lowerer, expr):
     '''Lower an array expression built by RewriteArrayExprs.
     '''
     expr_name = "__numba_array_expr_%s" % (hex(hash(expr)).replace("-", "_"))
-    expr_filename = f"__numba_array_expr_synthetic_module_{expr.loc.filename}"
+    expr_filename = expr.loc.filename
     expr_var_list = expr.list_vars()
     # The expression may use a given variable several times, but we
     # should only create one parameter for it.


Test patch: