nbQA-dev / nbQA

Run ruff, isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks
https://nbqa.readthedocs.io/en/latest/index.html
MIT License
1.01k stars 39 forks source link

bug: nbqa + ruff check with rule I001 #846

Open fvonbergen opened 5 months ago

fvonbergen commented 5 months ago

Hi! I encountered the following issue when using nbqa with ruff check and the rule I001. It seems that something odd happens when imports are not defined on the same cell. I was able to build a short example that reproduces it. The way to reproduce it is:

nbqa 'ruff check' bug.ipynb --select=I001 --fix

Found 1 error (1 fixed, 0 remaining).

bug.ipynb:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f145ef6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f145ef7",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(Dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

The following example works flawlessly:

nbqa 'ruff check' correct.ipynb --select=I001 --fix

correct.ipynb:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f145ef6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Dict\n",
    "\n",
    "print(Dict)"
   ]
  }
],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}