0xActor
0xActor

Reputation: 692

Incorrect diagnostics in Nvim LSP (Python)

I have installed lsp server for python and have def like this one:

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()

After that I got error:

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"

But, in vscode I don't have error like this, everything fine. To be more precise it isn't looks like error, but I can start and it is working, but this really annoying.And additional info about my 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

And 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

Upvotes: 0

Views: 831

Answers (1)

s810
s810

Reputation: 36

To me, it doesn't look like there is anything wrong with your LSP servers. I think this is just pyright giving you a diagnostic error that the type signature of the function you specified is incorrect.

The type signature of .first is Any | None because it may not find a result.

The type signature of your function is schema.File you may not always return a schema.File. Import the typing module and use typing.Optional[schema.File] for your function's type signature.

Upvotes: 0

Related Questions