Language specifications for strings
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
In PyCharm you can set the programming/markup language of strings like this:
# language=html
foo = '<div>hello there!</div>'
I find this extremely useful and use it all over the place. A pattern I noticed is that a large proportion of such uses are in fact more like:
# language=html
foo = format_html("<div>{}</div", bar)
In this case the format_html function doesn’t take any random string as the first argument, it takes a string that is supposed to be html. Reading through my usage of # language= I see that I have CSS, HTML, and JavaScript.
There are many places in my codebases that also have very different types of “languages” that unfortunately isn’t supported as a language injection in PyCharm. Some examples:
fully qualified name (module.module.symbol, for example view functions in Django)
module names (module.module, for example app names in Django)
- file paths
- host names (www.example.com)
- urls (https://example.com/)
- time zone (UTC)
- language code (en)
- regexes
- date formats
- django app names
There are probably more, but I think this gets the point across.
PyCharm has some (presumably hardcoded) rules about some of these strings, for example in settings.py it knows that strings in the list INSTALLED_APPS are module names, so you can jump to the definitions of those modules, and PyCharm will resolve and check them for you. But this is a closed system where any introduced variables I create myself can’t be validated in this way.
I think it would be good if python typing could have a facility for this type of thing. When this gets some traction we could see support for it in Python language servers, PyCharm, static analysis tools, etc.
What do you guys think?
(originally posted at https://discuss.python.org/t/language-specifications-for-strings/21826/1 where it was suggested that this is the right place for this discussion)
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读提案及其链接的 discuss.python.org 讨论串,然后比较所请求的字符串类别,例如 HTML、模块名称、URL、正则表达式和日期格式。完整的结果应当足够清晰地定义一种 Python typing 功能,使语言服务器、PyCharm 和静态分析工具能够支持它。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100