Skip to content

gh-145868: Suggest names on typos in __future__ imports - #154347

Merged
pablogsal merged 4 commits into
python:mainfrom
sobolevn:issue-145868
Sep 24, 2026
Merged

pablogsal merged 4 commits into
python:mainfrom
sobolevn:issue-145868

Conversation

@sobolevn

@sobolevn sobolevn commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

Comment thread Lib/test/test_future_stmt/test_future.py Outdated
Comment thread Python/future.c Outdated
Comment thread Include/cpython/compile.h Outdated
Comment thread Python/future.c
Comment thread Python/future.c

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments

Comment thread Python/future.c Outdated
FUTURE_GENERATOR_STOP,
FUTURE_ANNOTATIONS,
FUTURE_BRACES);
if (

@pablogsal pablogsal Aug 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe we can hoist the call out of the condition:

PyObject *suggestion = NULL;
PyObject *future_features = Py_BuildValue(...);
if (future_features != NULL) {
    suggestion = _Py_CalculateSuggestions(future_features, name->name);
    Py_DECREF(future_features);
}
if (suggestion != NULL) {
    ...
}

This also lets us drop the Py_XDECREF at the end.

Comment thread Include/cpython/compile.h Outdated
#define FUTURE_BARRY_AS_BDFL "barry_as_FLUFL"
#define FUTURE_GENERATOR_STOP "generator_stop"
#define FUTURE_ANNOTATIONS "annotations"
#define FUTURE_BRACES "braces"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose FUTURE_BRACES in the public header? It is only used in Python/future.c and braces is an easter egg, not a real feature, so maybe we can keep the define local to future.c instead of growing the C API surface.

Comment thread Python/future.c Outdated
FUTURE_BARRY_AS_BDFL,
FUTURE_GENERATOR_STOP,
FUTURE_ANNOTATIONS,
FUTURE_BRACES);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we want to suggest braces here: it is not in __future__.all_feature_names, so following the suggestion just gives not a chance. I am fine keeping it as part of the joke, but wanted to double check this is intentional.

@@ -0,0 +1,2 @@
Calculate suggestions during :exc:`SyntaxError` on typos when importing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: the wording reads a bit odd. Maybe something like "Suggest the closest valid feature name in the :exc:SyntaxError message when an unknown name is imported from :mod:__future__."

Comment thread Python/future.c Outdated
PyErr_Format(PyExc_SyntaxError,
UNDEFINED_FUTURE_FEATURE, feature);
PyObject *suggestion;
PyObject *future_features = Py_BuildValue("[sssssssssss]",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this list duplicates the strcmp chain above, so whoever adds a new future feature can easily forget to update it and the new name will silently get no suggestion. Can you add a small comment saying both need to be kept in sync?

@pablogsal
pablogsal enabled auto-merge (squash) September 24, 2026 16:04
@pablogsal
pablogsal merged commit dbac447 into python:main Sep 24, 2026
55 checks passed
@pablogsal

Copy link
Copy Markdown
Member

These suggestions are a nice touch, @sobolevn. Thank you very much!

@sobolevn

Copy link
Copy Markdown
Member Author

Thank you for the help on getting this merged! I am comletely overwhelmed by other work 🫶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants