Skip to content

Incorrect code suggestion #505

@alvin-r

Description

@alvin-r

Environment details

  • OS: macOS 14.5
  • Node.js version: v22.7.0
  • npm version: 10.8.2
  • code-suggester version: 4.3.4

Steps to reproduce

You can recreate my PR at https://github.com/alvin-r/code-suggester-bug/pull/1/files.

  1. Create a PR, adding a new file with the name 'common_tags.py' with:
from __future__ import annotations


def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
    if not articles:
        return set()

    common_tags = articles[0]["tags"]
    for article in articles[1:]:
        common_tags = [tag for tag in common_tags if tag in article["tags"]]
    return set(common_tags)
  1. Use the reviewPullRequest. You can use this script:
import { Octokit } from "@octokit/rest"
import { reviewPullRequest } from "code-suggester"

async function reviewPR() {
  // Initialize authenticated Octokit instance
  const octokit = new Octokit({
    auth: <YOUR_GITHUB_TOKEN>,
  })

  // Define the files to review with their changes
  const diffContents = new Map()
  diffContents.set("common_tags.py", {
    oldContent: `from __future__ import annotations


def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
    if not articles:
        return set()

    common_tags = articles[0]["tags"]
    for article in articles[1:]:
        common_tags = [tag for tag in common_tags if tag in article["tags"]]
    return set(common_tags)`,

    newContent: `from __future__ import annotations


def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
    if not articles:
        return set()

    common_tags = set(articles[0]["tags"])
    for article in articles[1:]:
        common_tags.intersection_update(article["tags"])
    return common_tags`,
  })

  // Configure review options
  const options = {
    owner: "alvin-r",
    repo: "code-suggester-bug",
    pullNumber: 1,
    pageSize: 100,
  }

  try {
    const result = await reviewPullRequest(octokit, diffContents, options)
    console.log("Review completed:", result)
  } catch (error) {
    console.error("Review failed:", error)
  }
}

reviewPR()
  1. When the review is created, the suggested code does not remove the 'return set(common_tags)' from the original code. You can see the comment created in https://github.com/alvin-r/code-suggester-bug/pull/1/files.

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions