Questions tagged [pyright]

Pyright is a static type checker for Python, developed by Microsoft and implemented in TypeScript. It supports PEP 484, PEP 526, PEP 544 and has smart type inference.

Pyright is a static type checker for Python, developed by Microsoft and implemented in TypeScript. It supports PEP 484, PEP 526, PEP 544 and has smart type inference. It supports the Language Server Protocol and is available in a Visual Studio Code plugin.

154 questions
48
votes
2 answers

Is it possible to ignore pyright checking for one line?

I need to ignore pyright checking for one line. Is there any special comment for it? def create_slog(group: SLogGroup, data: Optional[dict] = None): SLog.insert_one(SLog(group=group, data=data)) # pyright: disable # pyright: disable -- doesn't…
Max Block
  • 1,134
  • 3
  • 16
  • 23
29
votes
7 answers

"Import could not be resolved" reported by Pyright

I've just started using Pyright. Running it on files that run perfectly well I get plenty of errors. This question is similar, but refers to one's own modules. For example Import "numpy" could not be resolved. What does it mean, and how do I…
Ben
  • 12,614
  • 4
  • 37
  • 69
21
votes
2 answers

Python: How to write typing.overload decorator for bool arguments by value

The example code of what I am trying to ask is below. None of the examples on the internet try to overload argument value as such. One of the argument is a bool value and I want to overload a method based on the bool value rather than the usual,…
dumbPy
  • 1,379
  • 1
  • 6
  • 19
17
votes
2 answers

How to declare a Protocol with a field which supports both a simple type and property?

(Related, but not duplicated: How to annotate attribute that can be implemented as property?) I want to create a Protocol, in which a field can be implemented by both a simple type and property. For example: class P(Protocol): v:…
tamuhey
  • 2,904
  • 3
  • 21
  • 50
14
votes
2 answers

How to solve "type is partially unknown" warning from pyright?

I'm using strict type checks via pyright. When I have a method that returns a pytorch DataLoader, then pyright complains about my type definition: Declared return type, "DataLoader[Unknown]", is partially unknown Pyright…
Spenhouet
  • 6,556
  • 12
  • 51
  • 76
14
votes
4 answers

How can I add python type annotations to the flask global context g?

I have a decorator which adds a user onto the flask global context g: class User: def __init__(self, user_data) -> None: self.username: str = user_data["username"] self.email: str = user_data["email"] def login_required(f): …
Chris St Pierre
  • 141
  • 2
  • 4
12
votes
3 answers

How to set python interpreter in neovim for python language server depending on pyenv / virtualenv

I am using the pyright LSP in neovim (0.5). It works, but seems to only pick up on packages available in the standard python installation. It does not autocomplete for packages not in the base python, but in my pyenv environment. In VSCode this is…
Mike
  • 3,775
  • 8
  • 39
  • 79
12
votes
2 answers

Can I have an optional parameter in dataclasses that is omitted when transformed to dict?

I wish to perform static type checking (pylance in vscode) on some dictionaries. The "tricky" part is the I want some of the parameters to be optional and not show up at all in the dictionary. I've tried using dataclasses and TypedDict but without…
mr.bjerre
  • 2,384
  • 2
  • 24
  • 37
11
votes
1 answer

How do you ignore specific Pyright type checks by project, file, line?

I cannot quite find clear documentation on how to ignore one or more specific Pyright checks: Using a config file at the root of your project. At the top of a file, function, or method. Each ligne as a trailing comment. Thanks in advance for…
8
votes
1 answer

"at" sign (@) in Python type hints (suggested by Pylance / Pyright)

The July 2022 release of the Python extension for Visual Studio Code introduced "Inlay Type Hints", which automatically suggests return types of functions that don't have an explicit annotation. To enable it, you can set…
scūriolus
  • 657
  • 2
  • 5
  • 15
8
votes
1 answer

How to set a root directory for PyRight?

I have a Python project in VSCode. Its structure root +-- docs +-- some_other_folder +-- src +-- app | +-- main.py +-- tests +-- conftest.py conftest.py has import from app.main import app My task is to set src as root…
GhostKU
  • 1,898
  • 6
  • 23
  • 32
8
votes
3 answers

Fixing 'Import [module] could not be resolved' in pyright

I'm using pyright for type checking and I'm also using pytest for testing inside Visual Studio Code. The folder structure for my tests is to have a 'test' subfolder in the package root . For example | MyPackage |-- __init__.py |--…
Anti-Distinctlyminty
  • 2,615
  • 5
  • 22
  • 24
8
votes
1 answer

How do I install and run Pyright from the CLI instead of using VS Code?

I would like to begin using Pyright with my team's projects. I have installed the Visual Studio Code plugin, and can see the type errors in my editor as I work. But I would like to be able to run it automatically as part of our test suite, including…
Jeremy
  • 1
  • 85
  • 340
  • 366
7
votes
1 answer

Python type-checking: Literal[False], overload, and NoReturn

I have the following (typed) Python function: from typing import overload, Literal, NoReturn @overload def check(condition: Literal[False], msg: str) -> NoReturn: pass @overload def check(condition: Literal[True], msg: str) -> None: …
Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66
7
votes
1 answer

How to deal with unknown types of functions/methods from imported modules

I'm wondering what's the best approach to deal with unknown types of functions/methods associated with other modules. Note that I'm using strict mode For example, I have the following: rooms: List[str] =…
Adrian Pop
  • 1,879
  • 5
  • 28
  • 40
1
2 3
10 11