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
289
questions
0
votes
1
answer
28
views
Given a type `ScalarOrList` defined `ScalarOrList[T] = T | list[T]`, static check refuses `input: list[str]` for `arg: ScalarOrList[str | int]`?
I had:
from typing import Optional
type ScalarOrList[T] = T | list[T]
def as_list[T](xs: Optional[ScalarOrList[T]], length: int = 1) -> list[T]:
if isinstance(xs, Sequence) and not isinstance(...
1
vote
1
answer
46
views
How to tell type checker the type of an external function output
I am trying to add type hints to a package I am maintaining for more effective checks. However, we are using some external packages like numpy, scipy, etc., which do not have type hints everywhere. ...
0
votes
2
answers
72
views
How to assign a type hint from the return type hint of a function
I'm using an API that uses type hints returned from functions to store information (I know that this is not recommended, I have no control over the API):
my_hint: StringProperty()
This works, but it ...
1
vote
1
answer
74
views
Read-only class attributes that pass type checking, the modern way (3.11+)
Relevant older questions:
Class properties in Python 3.11+
What are some similar alternatives to using classmethod and property decorators together?
Out of all of them, the following answer was most ...
0
votes
1
answer
37
views
How to make Pylance recognize future scope variables in type expressions without using from __future__ import annotations
I'm using the Pylance extension in VS Code, and I encountered a warning Variable not allowed in type expression. It seems that Pylance is not recognizing an imported class when used in a type ...
1
vote
0
answers
35
views
VSCode says 'Cannot access attribute "[attribute]" for class "FunctionType"' for Keras model even though it runs without error
I'm trying to import a saved Keras model from disk:
from keras._tf_keras.keras.models import load_model
model = load_model('data/model.keras')
print(model.summary())
(Note: keras is imported like ...
0
votes
1
answer
56
views
How to type hint an attribute that can be assigned with a value of super type?
I am working with a C++ library wrapped for Python.
The library defines classes, each with many attributes that are like Python standard types but with a few extra methods. eg in the Python stubs &...
2
votes
1
answer
49
views
How to type hint a `pl.date`?
Suppose we create some dates:
import polars as pl
df = pl.DataFrame(
[
pl.Series("start", ["2023-01-01"], dtype=pl.Date).str.to_date(),
pl.Series("end&...
0
votes
1
answer
101
views
Create overloaded functions ignoring irrelevant parameters in the signature
How can I change the signature of the overloads to signal to type checkers that only a single parameter is responsible for the return type, ignoring all the other parameters?
Starting from this code (...
0
votes
1
answer
53
views
Handling Python Type Checker Errors with Return Type Declarations
I'm encountering an issue with my Python code using Pyright type checker, specifically when I try to remove Any from the return type declaration.
Look/read this question first: Handling Python Type ...
1
vote
1
answer
70
views
Handling Python Type Checker Errors with Return Type Declarations (fields default values = None)
I'm encountering an issue with my Python code using Pyright type checker, specifically when I try to remove Any from the return type declaration.
Error message:
Function with declared return type &...
0
votes
1
answer
41
views
What's the difference between Pint's "Quantity" and "PlainQuantity"?
My IDE (type-checking provided by the Pyright language server) doesn't like me passing pint.Quantity instances to functions type-hinted with pint.Quantity:
from pint import Quantity
def get_units(...
0
votes
1
answer
44
views
Airflow decorated task type hinting
Take this simple dag, in which one task takes the output of another:
import datetime
from airflow.decorators import dag, task
@task
def task_that_returns_a_string() -> str:
return "a ...
1
vote
1
answer
50
views
Can you do type checking in a called function? [duplicate]
I want to be able to call a function and have the caller be aware that the type of parameter has been checked. My type checker is Pyright through the Pylance extension in vscode with "basic" ...
0
votes
1
answer
67
views
Pydantic model field with default value does not pass type checking
The following code shows a pylance (pyright) error for AModel() of Argument missing for parameter "field_b":
from pydantic import BaseModel, Field
from typing import Optional, Any
class ...