Skip to content

bug: nbqa + ruff check with rule I001 #846

@fvonbergen

Description

@fvonbergen

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions