我已经为python安装了lsp server,并设置了如下定义:
def get_info_about_file(db: Session, name_of_file: str) -> schema.File:
return db.query(models.File).filter(models.File.name == name_of_file).first()
之后我得到了错误:
Diagnostics:
1. Expression of type "Any | None" cannot be assigned to return type "File"
Type "Any | None" cannot be assigned to type "File"
Type "None" cannot be assigned to type "File"
但是,在vscode中我没有这样的错误,一切都很好。更准确地说,它看起来不像错误,但我可以开始,它正在工作,但这真的很烦人。以及关于我的NullLsInfo:
的附加信息Logging
* current level: warn
* path: /home/user/.cache/nvim/null-ls.log
Active source(s)
* name: black
* filetypes: python
* methods: formatting
* name: autoflake
* filetypes: python
* methods: formatting
和LspInfo:
Language client log: /home/user/.local/state/nvim/lsp.log
Detected filetype: python
2 client(s) attached to this buffer:
Client: null-ls (id: 2, bufnr: [1, 16])
filetypes: scss, vue, javascriptreact, javascript, jsonc, yaml, less, typescript, graphql, typescriptreact, json, markdown.mdx, markdown, handlebars, css, html, rust, lua, luau, python
autostart: false
root directory: /home/user/dir
cmd: <function>
Client: pyright (id: 3, bufnr: [1, 16])
filetypes: python
autostart: true
root directory: Running in single file mode.
cmd: pyright-langserver --stdio
Configured servers list: rust_analyzer, clangd, tsserver, pyright
对我来说,看起来你的LSP服务器没有任何问题。我认为这只是给你一个诊断错误,你指定的函数的类型签名是不正确的。
.first
的类型签名为Any | None
,因为它可能找不到结果。
你的函数的类型签名是schema.File
,你可能不总是返回schema.File
。导入typing
模块并使用typing.Optional[schema.File]
作为函数的类型签名。