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
1
vote
0 answers

Why isn't Mypy inferring a 3rd-part library function's type?

Considering this code: class ExportView(IndexView): model_admin: Optional[ModelAdmin] = None def export_csv(self) -> HttpResponse | StreamingHttpResponse: fields = getattr(self.model_admin, "csv_export_fields", []) return…
Adrien Lemaire
  • 1,744
  • 2
  • 20
  • 29
1
vote
1 answer

How can I check the type of a NewType value?

How do you check the value of NewType against its basic type str without casting and mixing up type checking? I declared a new type: BoardId = NewType("BoardId", str) that I use as a method parameter either as a single value or as an Iteration…
Lajos
  • 2,549
  • 6
  • 31
  • 38
1
vote
0 answers

"Overloaded implementation not consistent with signature of overload" even though it is?

With the code from typing import overload import numpy as np import numpy.typing as npt class BoundedArray: @overload def __init__( self, array: npt.ArrayLike, *, upper_bounds: npt.ArrayLike, …
cymin
  • 83
  • 8
1
vote
1 answer

Why are unused parameters only greyed out but not reported?

I use VS Code and the unused parameters of functions are only greyed out, but never reported. How to report those greyed out warning in the problem explorer? Currently, thanks to the parameter reportUnusedVariable in pyright, we can report unused…
1
vote
0 answers

How to typecheck real numbers, and type-safely compare them to ints

I have a function that inputs a real number. In practice it might be an int or a float. and I want it to check if it is >0 For example def f(x): return x > 0 Now I want that function to be type checked. Since I wanted x to be any real number, I…
tbrugere
  • 755
  • 7
  • 17
1
vote
1 answer

Python Generics: cannot be assigned to declared type "T@func"

I'm trying to introduce Generic typing to a function of mine, but am getting a pylance error of: Expression of type "A" cannot be assigned to declared type "T@func" Type "A" cannot be assigned to type "T@func" I've reduce my problem in my code to…
dvreed77
  • 2,217
  • 2
  • 27
  • 42
1
vote
0 answers

Pyright type error with Flask error handler

I'm working my way towards strict type checking using Pyright, and I'm running into an issue with a Flask error handler. I'm not yet familiar enough with type hints to know if this is an error in my code, in Pyright, or in Flask's type hints. This…
Freddy The Horse
  • 335
  • 2
  • 12
1
vote
0 answers

Can `pyright` LSP navigate to different module?

Is pyright goto definition command supposed to jump into the different modules? I import python class like this from a.b.c import Foo. When my cursor is on a = Foo() and I call lua vim.lsp.buf.definition() nvim jumps only to the top of the file but…
Lajos
  • 2,549
  • 6
  • 31
  • 38
1
vote
0 answers

Python Typing: How to handle List type hinting with different element type in subclasses?

I am trying to understand how type hinting works in Python - especially in children classes. I had this issue while working on a project and was able to recreate the issue in an example, presented below. Environment: Python: 3.9.7 mypy: 0.910 OS:…
Eduardo Davalos
  • 161
  • 2
  • 8
1
vote
2 answers

Make a Union of strings to be used as possible dictionary keys

I have some Python 3.7 code and I am trying to add types to it. One of the types I want to add is actually an Union of several possible strings: from typing import Union, Optional, Dict PossibleKey = Union["fruits", "cars",…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
1
vote
1 answer

Python Type Hinting After exec() Assignment

I often have a situation where I make the assignment with exec, like so: ... exec( "someClassInstance = someMapper({someInfo})".format(someInfo=someInfo), globals(), ) Where someClassInfo is an instance of SomeClass. and then I use…
Mohsen Banan
  • 85
  • 1
  • 5
1
vote
2 answers

Vscode shows function docstrings twice when using jedi + pyright

I am using VSCodium (an open source version of vscode) with jedi and pyright installed. This leads to function docstrings getting displayed twice (see attached picture), as jedi and pyright both show them. Is there a way to disable this single…
T-Dawg
  • 83
  • 6
1
vote
1 answer

Is it possible to statically type a generic C[T] as T in Python?

I am working with SQLite where type hints are still far from perfect, and I ended up with a MCVE of what I think I want to do, and it may be related to statically-typing ABCs. Example: T = TypeVar('T') class ColumnarValue(Generic[T]): …
ssice
  • 3,564
  • 1
  • 26
  • 44
1
vote
0 answers

How to properly type beam.Map with pyright

I have beam code like: def foo(i: int) -> Foo: ... class IntToFoo(beam.PTransform): def expand(self, pcoll: PCollection[int]) -> PCollection[Foo]: return pcoll | beam.Map(foo) Running pyright it complains for the returning line: Expression…
abergmeier
  • 13,224
  • 13
  • 64
  • 120
1
vote
1 answer

Recursive type aliases with Pyright

Checking the following code with Pyright: from typing import Union, TypeVar T = TypeVar('T') X_or_RecurListOf = Union[T, list['X_or_RecurListOf']] x: X_or_RecurListOf[str] = ['asd'] produces the error: 5:28 - error: Expression of type…
Min-Soo Pipefeet
  • 2,208
  • 4
  • 12
  • 31