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
3
votes
1 answer

Pandas Series[Unknown] error when using Pylance

Using VSCode with Pylance, creating a basic series with pandas shown here will show an error. I looked around online and this question hasn't been asked yet, so I'm assuming I have some basic setup done incorrectly. Using conda, python@3.8.3,…
Farris Ismati
  • 176
  • 1
  • 9
3
votes
2 answers

Type mismatch when using subclasses

The following code gives me a type mismatch error under strict typeschecking in Python. class DataClass1(ABC): @abstractmethod def to_int(self) -> int: return 1 class DataClass2(DataClass1): def __init__(self, value: int): …
3
votes
0 answers

How to type hint a function returning a generic type and conformance to a Protocol in Python

If I have a function taking in an instance of type T, and outputting that same instance but modified so it additionally conforms to a Protocol, how should I type hint that? My main goal is to allow my IDE (VSCode) to know that the returned object…
Stavr0s
  • 98
  • 7
3
votes
1 answer

Is there a way to mark variables as constant / unchangeable in pyright?

I tried googling but I haven't come across a way to use a type annotation to mark a variable as constant in pyright. I think the type Constant is more like scalar constant rather than the C/C++ keyword const. Any thoughts appreciated. Thanks.
user49404
  • 732
  • 6
  • 22
3
votes
0 answers

Pyright strict type-checking mode won't work in Sublime Text 4.0

I have installed LSP and LSP-pyright in Sublime Text 4 and included "python.analysis.typeCheckingMode": "strict" within the LSP-pyright.sublime-settings file but Pyright still won't raise any Type error when I wrote the following code: def foo(x,…
marshblocker
  • 83
  • 1
  • 6
3
votes
1 answer

Is it possible to maintain type information when unpacking object attributes?

Imagine I have an object which is an instance of a class such as the following: @dataclass class Foo: bar: int baz: str I'm using dataclasses for convenience, but in the context of this question, there is no requirement that the class be a…
thisisrandy
  • 2,660
  • 2
  • 12
  • 25
3
votes
1 answer

How to handle insufficient typing in external libraries?

I like making use of the typing capabilities in Python along with a static type check e.g. pyright. Unfortunately, not all libraries are completely typed. For example some functions may be generated at runtime (therefore missing) or the function…
TomTom
  • 2,820
  • 4
  • 28
  • 46
3
votes
1 answer

Can I restrict the types of a subclass in Python?

Let's say I want to do define wrapper classes on sets and lists that add some useful methods, like this: from abc import ABC class AbstractGizmo(ABC): def bloviate(self): print(f"Let me tell you more about my {len(self)}…
Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
3
votes
1 answer

vim ALE is "ignoring" pyright

I'm having issues with setting up ale and pyright. According to the documentation on both repos, everything should work out of the box, but that doesn't seem to be the case for me. I'm guessing it's another plugin causing issues, but I'm not…
Farzad
  • 1,770
  • 4
  • 26
  • 48
3
votes
0 answers

Typehinting decorators that alter function arguments

I'm creating a decorator, and I'd like the typehint to be preserved. However, I need to alter the return type a little bit. The Mypy docs contain information on how to do this if your decorated function signature is the same as the original one, but…
Gordon
  • 3,012
  • 2
  • 26
  • 35
2
votes
2 answers

Specify that a TypeVar supports the "-" operator among its values

Basically, I want to say that I have a type T which has a T.__sub__(self, other: T) -> T defined. I can currently make it typecheck as if it has a T.__sub__(self, other: Any) -> Any defined, but only works if T is a class I defined myself. I'm…
Ayhon Iron
  • 23
  • 5
2
votes
1 answer

How to define constants in Python based on if/else without pyright warning

I'm attempting a fairly typical if/else -> CONSTANT = ... pattern in python but getting a pyright error that "... is constant (because it is uppercase) and cannot be redefined" (reportConstantRedefinition). While this is not a syntax error, I'm…
James
  • 21
  • 1
2
votes
0 answers

Is pyright incorrectly generalising types?

I'm playing with some code that statically checks dimensions on linear algebra. I have managed to encode dimensions in a way that mypy is happy to check, but pyright isn't quite as happy about it. A simplified version looks like this: from…
Thomas Mailund
  • 1,674
  • 10
  • 16
2
votes
1 answer

Fix pyrIght warning "Import [module] could not be resolved"?

I have the following Projects folder structure: and the file Tasks/Scripts/test.py (shown below) attaches util.py from Libs/PyLibs: The file test.py executes fine without any issue, but I cannot get rid of the import warning (I am using latest…
Manojit
  • 611
  • 1
  • 8
  • 18
2
votes
0 answers

Type hints for attributes using getattr

I'd like to forward the call of a method to another method, and have the second method's type hints show up in the first method. Probably easier to demonstrate. When an instance of the class below is called, it looks up the method with the name…
joudan
  • 81
  • 5
1 2
3
10 11