Sublime Forum

Beginner advice for a plugin to hide Python type annotations


I absolutely loathe Python’s abominable new type annotations syntax. Still, it’s increasingly popular and basically unavoidable in the future, but I want to minimize my day to day exposure to it. So I’m thinking, perhaps I could write a Sublime Text plugin to do this.

Using an example from the Python documentation:

def broadcast_message(message: str, servers: Sequence[Server]) -> None: would show up as def broadcast_message(message, servers): instead, with some kind of subtle inline indicator that I could click (or use a keyboard shortcut while the cursor is inside the folded version) to toggle between the ugly and the attractive version.

I’m completely new to ST plugin development, and have not used plugins that work in a similar manner. While the API docs have some bits about regions and folding that I’ll use as a starting point, I’d love some further pointers. Are you familiar with ST4-compatible plugins that do something similar, whose source code I could have a look at? Also, is there maybe a document or forum/blog post detailing plugin dev best practices or pitfalls somewhere?



What you see in the buffer and the contents of the buffer always have a 1:1 correspondence, so there is no API that allows you to arbitrarily hide text other than folding it up or physically deleting the text from the buffer but making sure to put it back in when the file is saved so that the changes don’t persist to disk.

I’m not aware of any plugin that does that since it’s destructive and prone to potential data loss.



OK. It looks like it might be possible to fold it like this (ellipses as the folding indicators here):

def broadcast_message(message…, servers…)…:

Not sure if I like it, but I might try it. Or can you only have one fold per line? The reason I ask is that the Fold Tag Attributes feature folds the attributes of one HTML tag per line, but if there are more tags on the same line, their attributes are not folded. No idea if this is a limitation of that specific feature, or the folding functionality in general.



There’s a plugin at which can do that automatically.

With some improvements from it works for python as well, after adding the required setting to Preferences.sublime-settings

			"fold_selector": "string.quoted.single - punctuation.definition.string, string.quoted.double - punctuation.definition.string",
			"preceding_text": "class,className"
			"fold_selector": "meta.function.parameters.annotation.python, meta.function.annotation.return.python, meta.variable.annotation",


Oh that’s nice. I’ll have to look into this, thanks!