Questions tagged [type-hinting]
Type hinting binds function arguments to specific objects or strongly types them.
type-hinting
1,568
questions
0
votes
1
answer
55
views
What are the differences between using mutiple or'ed typehints vs abc and an inheritance hierachy in Python?
Python is a dynamic language. This means that types are dynamic at runtime and Python makes use of the concept of Ducktyping.
What this means is that for any object x was can do
x.some_function()
x....
0
votes
0
answers
30
views
Class extends \ArrayIterator, how to typehint the object coming out from itereting on it?
I have a class extending \ArrayIterator
class ProductDtoCollection extends \ArrayIterator
{
}
when iterating on it it will return an ProductDto object. How to let both PHPStan and VScode Intelephense ...
0
votes
0
answers
21
views
How to typehint an intermediate value in chained code using PHPDoc
I have the following code:
$id = $ui->getWidget('import_id')->value;
I know the class type of $ui. The getWidget() method can return one or more types of widget classes based on the parameter ...
1
vote
1
answer
127
views
PHPStan keeps saying "No value type specified in iterable type" but it is specified
When using php type declarations, PHPStan seems to think that I should specify the array members in both the PHPDoc as well as the actual declaration, but correct me if I'm wrong PHP (at least PHP 7) ...
1
vote
0
answers
40
views
PhpStorm inspections on __call magic methods without knowing the name of the dynamic functions
The short version of this is, I want PhpStorm to know the datatype returned by a dynamic function call that is handled by __call. There is no way to know the name of the dynamic functions reasonably ...
1
vote
0
answers
58
views
PHP IDE hint for classes instantiated by __call() function
I am using VSCode and PHP 8.1+. In standard way, we can get hints when we type $myClass->??? and we will get a list of functions within the class.
However, recently I have a class that could ...
4
votes
0
answers
935
views
How do I correctly typehint `cv2` and `numpy` interaction?
I'm trying to typehint the following code which is supposed to convert PNG images (as bytes) to NumPy arrays:
import numpy as np
import cv2
def png_to_numpy(blob: bytes) -> np.typing.NDArray[np....
0
votes
1
answer
100
views
Which is best for defining a complex type: CustomType(TypedDict) or CustomType = NewType(...)?
from typing import TypedDict
class CustomType(TypedDict):
id: str
path: str
targets: list[str]
or
from typing import NewType
CustomType = NewType("CustomType", dict[str, str, ...
0
votes
1
answer
171
views
In Python, how would you properly annotate a function to show the same return type as a function of a class member?
I'm using Python 3.11 with mypy as my type checker. Here's an example of what I'm trying to do.
from dataclasses import dataclass
from abc import ABC, abstractmethod
from typing import Any, TypeVar, ...
0
votes
1
answer
80
views
Using slice notation yields wrong type hint
I am building a neural network for a project but the type hinting is misleading
Code as follows:
import typing
class Layer:
pass
class Network:
def __init__(self, *layers: Layer):
...
1
vote
1
answer
92
views
Type Validation Based on the Contents of a JSON file
This is similar to this question, except here the valid arguments are contained in a json file.
In Python using VSCode, I'd like to define a type which is based on the contents of a JSON file. Is such ...
2
votes
1
answer
152
views
Type hints for mypy for a decorator over staticmethod/classmethod
I'm writing a helper for logger library that has decorators with specific logging for trace (debugging).
The code itself is correct (it is partially based on existing library), but i struggle to find ...
1
vote
0
answers
203
views
Python typehinting class decorator
I am trying to implement the equivalent of the @contextlib.contextmanager decorator for classes. When I researched this topic, it became apparent that I was not the first needing these and used the ...
0
votes
0
answers
58
views
Type hints with Memoization through decorator
I have implemented memoization through a function decorator like this:
class A:
def __init__(self, f):
self.foo = 1337
class Memoize:
def __init__(self, f):
self.f = f
...
2
votes
1
answer
33
views
Type hint for a special argument
Consider the following function:
from datetime import date
def days_between(start_date: date, end_date: date) -> int:
if start_date == "initial":
start_date = date(2023, 9, 1)...