NumPy
numpy.org › devdocs › reference › typing.html
Typing (numpy.typing) — NumPy v2.6.dev0 Manual
Consequently, the likes of float16, float32 and float64 are still sub-types of floating, but, contrary to runtime, they’re not necessarily considered as sub-classes. Deprecated since version 2.3: The NBitBase helper is deprecated and will be removed in a future release. Prefer expressing precision relationships via typing.overload or TypeVar definitions bounded by concrete scalar classes. For example: from typing import TypeVar import numpy as np S = TypeVar("S", bound=np.floating) def func(a: S, b: S) -> S: ...
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.6.dev0 Manual
There are 5 basic numerical types ... is the number of bits that are needed to represent a single value in memory. For example, numpy.float64 is a 64 bit floating point data type....
NumPy
numpy.org › doc › 2.3 › reference › typing.html
Typing (numpy.typing) — NumPy v2.3 Manual
Deprecated since version 2.3: Use @typing.overload or a TypeVar with a scalar-type as upper bound, instead. ... Try it in your browser! Below is a typical usage example: NBitBase is herein used for annotating a function that takes a float and integer of arbitrary precision as arguments and returns a new float of whichever precision is largest (e.g.
Jack Atkinson
jackatkinson.net › post › numpy_typing
Typing in numpy - Jack Atkinson's Website
April 27, 2025 - If a function that returns an NDArray returns what is realistcally a scalar, it is what numpy calls a 0D array . Your end users are unlikely to ever notice this thanks to duck typing , but internally if you know a type is guaranteed to be a scalar (0D array) and need to specify this for subsequent type checking you can use typing’s cast: from typing import cast def my_function_2(input: float) -> float: # Pass float to my_function which accepts ArrayLike result = my_function(input) # my_function returns an NDArray, but since we know input was # a single float we can guarantee that result will be a # 0D array, so can cast to float for type checking return cast(float, result) - 10.0
NumPy
numpy.org › doc › stable › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.5 Manual
Whenever a data-type is required ... dtype constructor: What can be converted to a data-type object is described below: ... Used as-is. ... The default data type: float64....
NumPy
numpy.org › doc › stable › reference › typing.html
Typing (numpy.typing) — NumPy v2.5 Manual
Consequently, the likes of float16, float32 and float64 are still sub-types of floating, but, contrary to runtime, they’re not necessarily considered as sub-classes. Deprecated since version 2.3: The NBitBase helper is deprecated and will be removed in a future release. Prefer expressing precision relationships via typing.overload or TypeVar definitions bounded by concrete scalar classes. For example: from typing import TypeVar import numpy as np S = TypeVar("S", bound=np.floating) def func(a: S, b: S) -> S: ...
NumPy
numpy.org › doc › 1.20 › reference › typing.html
Typing (numpy.typing) — NumPy v1.20 Manual
January 31, 2021 - Users who want to write statically typed code should insted use the numpy.ndarray.view method to create a view of the array with a different dtype. The DTypeLike type tries to avoid creation of dtype objects using dictionary of fields like below: >>> x = np.dtype({"field1": (float, 1), "field2": (int, 3)})
w3resource
w3resource.com › numpy › snippet › exploring-numpy-typing.php
Exploring numpy.typing for Enhanced Type Hints
The type hints prevent passing incompatible types. ... import numpy as np from numpy.typing import ArrayLike # Define a function that accepts any array-like object def calculate_sum(data: ArrayLike) -> float: # Convert input to a NumPy array and return the sum return float(np.sum(data)) # Test the function with different inputs result1 = calculate_sum([1, 2, 3]) # List input result2 = calculate_sum(np.array([4.5, 5.5])) # NumPy array input print("Sum of list input:", result1) print("Sum of array input:", result2)
NumPy
numpy.org › doc › 2.4 › reference › typing.html
Typing (numpy.typing) — NumPy v2.4 Manual
Consequently, the likes of float16, float32 and float64 are still sub-types of floating, but, contrary to runtime, they’re not necessarily considered as sub-classes. Deprecated since version 2.3: The NBitBase helper is deprecated and will be removed in a future release. Prefer expressing precision relationships via typing.overload or TypeVar definitions bounded by concrete scalar classes. For example: from typing import TypeVar import numpy as np S = TypeVar("S", bound=np.floating) def func(a: S, b: S) -> S: ...
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.5 Manual
There are 5 basic numerical types ... is the number of bits that are needed to represent a single value in memory. For example, numpy.float64 is a 64 bit floating point data type....
» pip install nptyping
Reddit
reddit.com › r/learnpython › what's the appropriate type-hint for a function that can accept a list of floats or a numpy array?
r/learnpython on Reddit: What's the appropriate type-hint for a function that can accept a list of floats OR a numpy array?
July 11, 2023 -
This is what I've got so far
import numpy.typing as npt
def myfunc(mylist : list[float] | npt.NDArray):
print(len(mylist))It works, but I wonder if there's another way to do this that doesn't involve the |?
I tried npt.ArrayLike, but it complains when you try to access an element from an ArrayLike object, i.e. the following program results in an error:
import numpy as np import numpy.typing as npt myvar: npt.ArrayLike = np.array([1,2,3]) # this line is fine print(myvar[0]) # this line throws an error with mypy because apparently ArrayLike is not indexable
NumPy
numpy.org › doc › 2.2 › reference › typing.html
Typing (numpy.typing) — NumPy v2.2 Manual
Most notably this includes the likes of float128 and complex256. Without the plugin all extended-precision types will, as far as mypy is concerned, be available to all platforms. Assigning the (platform-dependent) precision of c_intp. Without the plugin the type will default to ctypes.c_int64. New in version 1.22. To enable the plugin, one must add it to their mypy configuration file: ... NumPy ...
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 Manual
There are 5 basic numerical types ... is the number of bits that are needed to represent a single value in memory. For example, numpy.float64 is a 64 bit floating point data type....
NumPy
numpy.org › numtype › user › differences
Differences with NumPy - NumType
These types will not be defined on any supported platform. The platform-dependent float96 and float128 types are equivalent aliases of longdouble (#397):, and their complex analogues, complex192 and complex256, alias clongdouble (#391).
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
Generally, problems are easily fixed by explicitly converting array scalars to Python scalars, using the corresponding Python type function (e.g., int, float, complex, str, unicode). The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. int16). Therefore, the use of array scalars ensures identical behaviour between arrays and scalars, irrespective of whether the value is inside an array or not. NumPy scalars also have many of the same methods arrays do.