1

Although I searched my problem before, I couldn't find any solution.

I need to find the file in which default/built-in code snippets are, and edit it. Indeed I don't know exactly is it from Python extension or the language server which is currently set to "Pylance" in my setting.

As an example, if want to implement __call__ in my class, in suggestion pop-up there is no indication that it is a snippet: (btw, I haven't defined any custom snippet)

enter image description here

After tab it changes to:

enter image description here

Which is obviously not what I want. I have to either import Any from typing module or manually delete those type hints.

clarification:

  1. OS: Pop!_OS 21.10 x86_64

  2. I've checked ./ms-python.vscode-pylance2022.4.2/dist/typeshedfallback/stubs/whatthepatch/whatthepatch/snippets.pyi file but it doesn't contain those.

  3. Also my /usr/share/code/resources/app/extensions/python directory doesn't have snippets directory as mentioned here

  4. Python extension version : v2022.4.1

  5. Pylance version : v2022.4.2

One possible solution would be to define custom code snippets and set them to be on top in the suggestion list. But I hope I could change the original one.

2
  • This is determined by the IntelliCode extension. Internal modification is not supported for the time being. The easiest way is still to override with custom code snippets. Of course. you could file a issue on github:github.com/MicrosoftDocs/intellicode/issues Commented Apr 26, 2022 at 2:54
  • @MingJie-MSFT AFAIK IntelliCode is a separate extension and is not automatically installed by Python extension. I don't have that extension installed so I don't think it is from IntelliCode. Commented Apr 26, 2022 at 7:28

3 Answers 3

4

The simplest solution is probably to define a custom snippet that overrides the existing snippet for __call__ following the VS Code snippets documentation

In this case, open the command palette and locate/type Preferences: Configure User Snippets, open python.json if it already exists under Existing Snippets, otherwise type in python and select New Snippets file for 'Python'...

In your new snippets file, define a custom snippet for __call__ that's formatted the way you want, e.g.:

{
    "call": {
        "prefix": "__call__",
        "body": ["insert your snippet guts here"],
        "description": "custom __call__ snippet"
    },
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I already have this solution in my hand in case I don't figure out how to edit those files. However thanks for the response(+1).
@SorousHBakhtiary Might I suggest this online snippet writer? Supports multiple editors. Then you can copy the generated code. Then (on Mac): menu Code, Preferences, User Snippets. Create or open python.json and paste in what you got. Definitely has been a huge help getting me up to speed with the special json formatting needed in the snippets files.
just tested works fine for me too. about overriding the delivered snippets... keep in mind you would lose your customizations every time vscode updated anything in those snippets since this is a monolithic one-per-language format. unlike say sublime which has one snippet per file.
This is the answer I see everywhere ... yet, it's not an answer to the question. I just can't believe there's no simple way to actually change the default snippets other than overwriting them.
Using the Snippets Manager extension can quickly edit User snippets.
2

As per JRiggles, but re editing, go to snippet generator

enter image description here

Comments

1

“You can hide specific snippets from showing in IntelliSense (completion list) by selecting the Hide from IntelliSense button to the right of snippet items in the Insert Snippet command dropdown.” See: Can I remove snippets from IntelliSense?.

2 Comments

Thanks for the answer. I just noticed that there is no such option for Python files. I checked it with .JS and .HTML and yes it works perfectly.
Unfortunately, I’m not generally a Python user. Sorry this wasn’t more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.