Skip to content

Conversation

dsandersllvm
Copy link
Collaborator

Pyright is an MIT-licensed static type checker and can be found at
https://github.com/microsoft/pyright
there are also various integrations to use it as an LSP server in various editors which is the main way I use it.

It's useful on our python scripts to detect issues such as where functions are called with unexpected types or it's possible to access obj.attr on an object that doesn't have that attribute. It can be used without any configuration this config setting causes it to also report issues with type hints that do not meet our python 3.8 minimum such as this one from dap_server.py:

        init_commands: list[str],

subscripting the builtin type like that requires python 3.9 while the 3.8 equivalent is:

from typing import List
...
        init_commands: List[str],

In practice these scripts still work on 3.8 because type hints aren't normally evaluated during normal execution but since we have a minimum, we should fully comply with it.

Note: The error pyright reports for this particular issue isn't great:

error: Subscript for class "list" will generate runtime exception; enclose type expression in quotes

This is technically correct as it is possible to evaluate type hints at runtime but I believe anything that would do so would also evaluate the string form as well and still hit the runtime exception. A better suggestion in this case would have been the 3.8 compatible List[str]. However, it is better than silently passing code that doesn't confirm to the minimum.

Pyright is an MIT-licensed static type checker and can be found at
    https://github.com/microsoft/pyright
there are also various integrations to use it as an LSP server in various
editors which is the main way I use it.

It's useful on our python scripts to detect issues such as where functions
are called with unexpected types or it's possible to access obj.attr on an
object that doesn't have that attribute. It can be used without any
configuration this config setting causes it to also report issues with
type hints that do not meet our python 3.8 minimum such as this one from
dap_server.py:
```
        init_commands: list[str],
```
subscripting the builtin type like that requires python 3.9 while the 3.8
equivalent is:
```
from typing import List
...
        init_commands: List[str],
```
In practice these scripts still work on 3.8 because type hints aren't
normally evaluated during normal execution but since we have a minimum, we
should fully comply with it.

Note: The error pyright reports for this particular issue isn't great:
```
error: Subscript for class "list" will generate runtime exception; enclose type expression in quotes
```
This is technically correct as it is possible to evaluate type hints at
runtime but I believe anything that would do so would also evaluate the
string form as well and still hit the runtime exception. A better suggestion
in this case would have been the 3.8 compatible `List[str]`. However, it is
better than silently passing code that doesn't confirm to the minimum.
@DavidSpickett
Copy link
Collaborator

So as I understand this, it configures pyright if I am already using it. Rather than saying you must use it.

I don't think llvm has made any recommendations of tools for this. We don't necessarily need to, given they all consume the same hints, but this minimum version setting is interesting for pyright specifically.

It's probably fine to add this, but let me do a bit of research on type checking first. Last I worked on Python seriously, it wasn't a thing.

In the meantime, a PR to change the 3.9 hints to the 3.8 compatible kind would be welcome. If you could get LLDB to build (verify?) cleanly with pyright set to 3.8 that'd be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants