jupyter / jupyter/notebook

Strange decimal.getcontext().prec behavior

Open
#5,260 5 comments 5 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
13.3k
Forks
5.8k
Avg merge
6d 11h
Merged PRs (30d)
7

Description

Hi,

I'm using Python's `decimal.getcontext().prec` to set decimal precision for operations in a notebook.

In Jupyter, the precision seems to (somewhat intermittently) get reset to 28 between manual executions of different cells. The strangest part is - running the shell command `!jupyter notebook --version` fixes the issue. (I haven't tested other shell commands.)

Additionally, on a just-started version of JupyterLab, `decimal.getcontext().prec` behavior is normal; the bug only appears to surface after restarting the kernel in the JupyterLab UI.

"Run all cells" also appears to apply precision correctly across different cells.

Example & version details -
https://github.com/evan-burke/juptyer_getcontext_bug/blob/master/getcontext.ipynb

Same thing run in a console behaves normally:

```
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

import sys
from decimal import Decimal, getcontext

sys.version
'3.7.4 (default, Aug 13 2019, 20:35:49) \n[GCC 7.3.0]'

getcontext().prec = 4
r = Decimal(2) / Decimal(3)
r
Decimal('0.6667')

getcontext().prec
4
```

Contributor guide

Open the contributing guide

Research direction

Start by running the referenced getcontext.ipynb in JupyterLab and reproduce the precision change after restarting the kernel. Compare separate cell execution with Run all cells and the console behavior, including the effect of !jupyter notebook --version. Done means decimal.getcontext().prec remains consistent across cell executions after a kernel restart.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, jupyter-notebook, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.