TypeError is thrown when `pyproject.toml` contains options not used by `black.Mode`
rldotai opened this issue · comments
- Operating system and version: Linux
- Python version: 3.11
- jupyter-black version: 'v0.3.3'
My Issue
Loading this extension in a Jupyter notebook throws an error if your pyproject.toml
contains keys/values not recognized by black.Mode
but are valid options for black
.
For example:
Configured with:
#pyproject.toml
[...]
[tool.black]
line-length = 88
target-version = ['py311']
include = '\.pyi?$'
[...]
On running jupyter_black.load(lab=False)
, the following traceback is produced:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 jupyter_black.load(lab=False)
File ~/.mambaforge/envs/py311/lib/python3.11/site-packages/jupyter_black/jupyter_black.py:220, in load(ip, lab, line_length, target_version, verbosity, **black_config)
217 black_config.update({"target_versions": set([target_version])})
219 if formatter is None:
--> 220 formatter = BlackFormatter(ip, is_lab=lab, black_config=black_config)
221 ip.events.register("pre_run_cell", formatter._format_cell)
File ~/.mambaforge/envs/py311/lib/python3.11/site-packages/jupyter_black/jupyter_black.py:65, in BlackFormatter.__init__(self, ip, is_lab, black_config)
63 valid_options = set(t.get_type_hints(black.Mode))
64 # config = {k: v for k, v in config.items() if k in valid_options}
---> 65 mode = black.Mode(**config)
66 mode.is_ipynb = True
67 self.mode = mode
TypeError: Mode.__init__() got an unexpected keyword argument 'include'
WHYT
I'd suggest filtering the options first prior to passing them to the mode
object in jupyter_black.py
.
For example, the following modification fixes the aforementioned problem:
# jupyter_black.py
class BlackFormatter:
[...]
# Override with passed-in config
config.update(black_config)
LOGGER.debug(f"config: {config}")
valid_options = set(t.get_type_hints(black.Mode))
config = {k: v for k, v in config.items() if k in valid_options}
mode = black.Mode(**config)
mode.is_ipynb = True
[...]
Incidentally, the WHYT
link is broken; looking up an archived copy yields a long blog post that the author themselves is ambivalent of and now no longer makes available even on their technical blog and should probably be removed from your GitHub Issue Template.
Please make sure you've taken these steps before submitting a new issue:
- Include the Python and jupyter-black version in your issue
- Ensure you're running a supported version of Python
- Run jupyter-black in debug mode if applicable and include
relevant output - Search the existing (including closed) issues
- Please use codeblocks for any code, config, program output, etc.
Thanks for a great issue!
Should be fixed in the issue_7
branch, which I'll plan to merge soon if all is well in CI. Added a regression test as well.
Also thanks for the note about WHYT. Interestingly, even the post about the author regretting it is also now gone (but available in wayback at https://web.archive.org/web/20210417141624/https://mattgemmell.com/hindsight/). Having read it, it doesn't seem to give a very clear reason for the author's apparently deep regret about the piece, other than some people using it in bad faith. I found the article useful when it was presented to me, so I'll update links and hope that readers / users / contributors take it in the spirit it's given.
Also, https://www.rl.ai/ is a great domain! RL is fascinating -- I keep meaning to dip my toes in, if only there were more hours in the day.
Thanks again, feel free try out the issue_7
branch, or will hopefully be merged soon.