Question

Copilot keeps completing Python 2 style code in a Python 3.12 project

Solved · 103 viewsasked by marco_py

print "x", dict.iteritems(), except Exception, e. It happens most in older files.

What I’ve tried

Added a comment at the top saying Python 3.12.

Comment

2 answers

Marked as helpful by the asker
marco_py

Copilot mirrors the file. If the file is old code, it completes old code. Run pyupgrade --py312-plus over the repo once; after that the nearby code is modern and so are the completions. Comments do almost nothing; surrounding code does everything.

Comment
rune_a

If you already use ruff, you don't need pyupgrade as a separate tool: the UP rules are the same checks. ruff check --select UP --target-version py312 --fix once, then keep UP in the config so old syntax can't come back through a suggestion someone accepts.

Comment
Even better, one less tool in the pre-commit config. marco_py