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
2
votes
2 answers

How to reference a python class attribute's type in a function signature

I have a common problem of referencing a class attribute type in a functions signature. Take for instance: class Person: name: str def __init__(self, name) -> None: self.name = name def greet(name: str): return "Hello " +…
2
votes
0 answers

vscode python lint problem at the first column of first line

I have python modules that I run in VSCode for development. And everything works fine until today VSCode suddenly said that one of the module had some binding problem and made the file name tab red. VSCode the problem occurs in the first character…
Farn Wang
  • 33
  • 5
2
votes
0 answers

Python type-checker can't tell we're dealing with subclasses

Say I have some code like this: from typing import Any class Parent: def __init__(self, name: Any): self.name = name class Child(Parent): def __init__(self, name: Any): super().__init__(name) def print_name(self) ->…
Nick Reid
  • 91
  • 3
2
votes
0 answers

Pyright couldnt find installed libraries

if I type in a file: import numpy pyright will return me an error: Import "numpy" could not be resolved i've seen This Question but i did set the right python version (3.10) where i have numpy installed (i only have python3.10 installed) runing the…
Haksell
  • 116
  • 4
2
votes
0 answers

Type system giving error when replacing function's argument type with identical type variable

Loving TypeScript, but also math, thus numpy, thus needing python, I'm currently checking out how much typing can be brought to the python world. It's limited, it seems, and I have a particularly weird case, which I hope somebody can explain to…
NoBullsh1t
  • 483
  • 4
  • 14
2
votes
1 answer

Typing a Python dictionary as Dict[key[T], value[K]] where T and K are restricted

Here is the problem: I have a dictionary that keeps track of two different types of values (let's say city and village) using two different types of keys (city_key and village_key). I'd like to annotate this dictionary with generics, so that when…
Ely
  • 59
  • 6
2
votes
0 answers

Can pyright load stubs for global definitions?

I have a python file that gets run in a customized way via a loader, such that it has additional global functions such as BoolVariable and classes such as Environment available. I'd like to create stub type declarations for these so my pyright isn't…
GaryO
  • 5,873
  • 1
  • 36
  • 61
2
votes
1 answer

Make pyright/PyCharm recognize arguments set by decorator

I have the following decorator function (simplified version of serialize_request from api-client-pydantic) that is supposed to be used on a function that takes any number of pydantic models as parameters. It initializes those models with the…
iuvbio
  • 600
  • 6
  • 23
2
votes
0 answers

How to type annotate decorators for classes?

I have the following code: class Mixin: def foo(self) -> int: return 1 def add_mixin(cls): class WithMixin(cls, Mixin): __name__ = cls.__name__ __doc__ = cls.__doc__ return WithMixin @add_mixin class B: def…
ARentalTV
  • 344
  • 1
  • 12
2
votes
1 answer

Pyright import from parent sibling results in missing import

I am trying to run a django app, and this is the following structure (minimal reproduction): root/ business/ __init__.py urls.py monitoring/ __init__.py api.py manage.py In urls.py I have the following import: from…
Devildude4427
  • 892
  • 1
  • 8
  • 28
2
votes
2 answers

Type hint for a module containing constants

I have a config.py file which has a list of constants, such as: config.py NAME = 'John' AGE = 23 In another file, I import this file as a module and then pass it as a parameter to other functions. I used ModuleType as the type for this…
Allen Qin
  • 19,507
  • 8
  • 51
  • 67
2
votes
1 answer

How to set coc-pyright to not warn for unused self parameter in a method?

I'm using the coc-pyright extension of CoC in neovim. There are cases where we have an instance method in a class that doesn't need to access self variables. For example, I may have some variables in __init__ method which do not need to be accessed…
Abhinav
  • 103
  • 2
  • 9
2
votes
1 answer

Pyright/mypy: "expr" has no attribute "id"

Code: def extract_assignment(assignment: ast.Assign) -> Dict[str, LINES_RANGE]: targets = ', '.join(t.id for t in assignment.targets) pyright/mypy: error: "expr" has no attribute "id" From typeshed: class Assign(stmt): targets:…
Yam Mesicka
  • 6,243
  • 7
  • 45
  • 64
1
vote
1 answer

Properly type a callable returning a Generator expression

I have the following snippet of code from contextlib import _GeneratorContextManager, contextmanager GoodWrapperType = Callable[[int, str], _GeneratorContextManager[None]] BadWrapperType = Callable[[int, str], Generator[None, None, None]] def…
IMCoins
  • 3,149
  • 1
  • 10
  • 25
1
vote
2 answers

Is there a way to specify arbitrary extras in a Python TypedDict?

I would like to specify a TypedDict that specifies some fields and types, but also allows for having a "bag" of extra things that are allowed in the dict. A concrete example of this would be for adding typing information to a decorator that would be…
t-r0d
  • 21
  • 5