Skip to content

Treat swapped adjacent letters as a single edit in "Did you mean" suggestions #161498

Open

Description

@moontc

Feature or enhancement

Proposal:

Name suggestions use a Levenshtein distance without transpositions, so swapping two adjacent letters costs two substitutions (4). This misses very common typos, and sometimes suggests an unrelated name instead:

>>> Ture
NameError: name 'Ture' is not defined
>>> compelx
NameError: name 'compelx' is not defined. Did you mean: 'compile'?

I propose counting a swap of two adjacent characters as one edit (cost 2, like a substitution), i.e. the optimal string alignment distance. Thresholds stay the same.

If this sounds reasonable, I'd be happy to open a PR.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Activity

  1. picnixz commented on Nov 6, 2026

    @picnixz
    Member
  2. pochmann commented on Nov 7, 2026

    @pochmann
    Contributor

    For Flase I already get:

    NameError: name 'Flase' is not defined. Did you mean: 'False'?
    
  3. moontc commented on Nov 7, 2026

    @moontc
    ContributorAuthor

    @pochmann thanks for checking, but in my description, I wrote flase, instead of Flase

    • ture -> tuple
    • Ture -> no suggestion
    • flase -> no suggestion
    • Flase -> False (it's correct)
  4. pochmann commented on Nov 7, 2026

    @pochmann
    Contributor

    Yes, I know you did.

    So your description isn't right, is it? Transpositions are already handled like you want. Do you want to change your proposal to making "Did you mean" case insensitive?

  5. moontc commented on Nov 7, 2026

    @moontc
    ContributorAuthor

    Sorry, the example I gave in the description seem wasn't good—it involved both swapping two letters and changing case, two kinds of transformations. I'll revise my description.
    Ture is a good example. When I type Ture, it doesn't ask me whether I meant to type True; it just shows NameError: name 'Ture' is not defined.
    In the current implementation, whether NameError: name 'A' is not defined. Did you mean: 'B'? is displayed is determined by whether the cost of turning A into B is less than a threshold. Changing case costs 1, changing a character costs 2. Swapping two characters means changing two characters, which costs 4, whereas my idea is to count swapping two characters as a single operation costing 2
    I think reversing the order of two letters in a word is an action with a probability similar to that of missing or adding an extra letter, so their costs should also be the same

  6. changed the title [-]Count adjacent transpositions in "Did you mean" suggestions[/-] [+]Treat swapped adjacent letters as a single edit in "Did you mean" suggestions[/+] on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions