Coverage for trimesh/typed.py: 98%
46 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-22 02:08 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-22 02:08 +0000
1import io
2import typing
3from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
4from io import IOBase
5from pathlib import Path
6from sys import version_info
7from typing import (
8 IO,
9 Any,
10 BinaryIO,
11 Literal,
12 Protocol,
13 TypeAlias,
14 TypeGuard,
15 TypeVar,
16 runtime_checkable,
17)
19import numpy
20from numpy import dtype, float64, floating, generic, int64, integer, ndarray
21from numpy.random import BitGenerator, Generator, SeedSequence
22from numpy.typing import ArrayLike, DTypeLike, NDArray
24if version_info >= (3, 11):
25 from typing import Self
26else:
27 Self = Any
29# most loader routes take `file_obj` which can either be
30# a file-like object or a file path, or sometimes a dict
31# `IOBase` is the base of every stdlib stream and is included because
32# concrete streams like `io.BytesIO` don't satisfy the `IO` protocol
33# under beartype — https://github.com/beartype/beartype/issues/643
34Stream: TypeAlias = IO[str] | IO[bytes] | IOBase
35Loadable: TypeAlias = str | Path | Stream | dict | None
37# for a function that returns "is this a file or not"
38# but with typeguard-narrowing if the answer is yes
39BoolIsFile: TypeAlias = TypeGuard[IO[Any]]
41# numpy integers do not inherit from python integers, i.e.
42# if you type a function argument as an `int` and then pass
43# a value from a numpy array like `np.ones(10, dtype=np.int64)[0]`
44# you may have a type error.
45# these wrappers union numpy integers and python integers
46Integer: TypeAlias = int | integer
48# Numbers which can only be floats and will not accept integers
49# > isinstance(np.ones(1, dtype=np.float32)[0], floating) # True
50# > isinstance(np.ones(1, dtype=np.float32)[0], float) # False
51Floating: TypeAlias = float | floating
53# Many arguments take "any valid number" and don't care if it
54# is an integer or a floating point input.
55Number: TypeAlias = Floating | Integer
57# the literals for specifying what viewer to use
58ViewerType: TypeAlias = Callable | Literal["gl", "jupyter", "marimo"] | None
60# literal for color maps we include in the library
61ColorMapType: TypeAlias = Literal["viridis", "magma", "inferno", "plasma"]
63# the literal for what graph backend engines are available
64GraphEngineType: TypeAlias = Literal["networkx", "scipy"] | None
66# what 3D boolean engines are available
67BooleanEngineType: TypeAlias = Literal["manifold", "blender"] | None
68# what 3D boolean operations can be passed to boolean functions
69BooleanOperationType: TypeAlias = Literal["difference", "union", "intersection"]
71# what are the supported methods for converting a mesh into voxels.
72VoxelizationMethodsType: TypeAlias = Literal["subdivide", "ray", "binvox"]
75@runtime_checkable
76class HttpSessionLike(Protocol):
77 """
78 Structural type for an HTTP session.
80 Matches `httpx.Client` and `requests.Session` so a resolver
81 can take either without trimesh importing them directly.
82 other duck-typed sessions are called as `get(url)` with no
83 additional kwargs. async sessions like `aiohttp.ClientSession`
84 can't be driven synchronously and are rejected at runtime.
85 """
87 def get(self, url: str, *args, **kwargs) -> Any: ...
90# add numpy types like their `numpy.typing.NDArray`
91# but with specific dimensionality, i.e. `NDArray2D[np.float64]`
92DType = TypeVar("DType", bound=generic)
93NDArray1D: TypeAlias = ndarray[tuple[int], dtype[DType]]
94NDArray2D: TypeAlias = ndarray[tuple[int, int], dtype[DType]]
95NDArray3D: TypeAlias = ndarray[tuple[int, int, int], dtype[DType]]
97# the shape of an array like numpy takes it, i.e. `(3, 4)`, `[3, 4]`,
98# `np.array([3, 4])` or a bare integer `3` meaning `(3,)`
99ShapeLike: TypeAlias = Integer | Sequence[Integer] | NDArray1D[integer]
101# anything `numpy.random.default_rng` can normalize into a `Generator`
102# passing a `Generator` lets a caller thread one stream through nested
103# calls -- `default_rng` hands it back rather than re-seeding it
104Seed: TypeAlias = Integer | Sequence[int] | SeedSequence | Generator | BitGenerator | None
107# DEPRECATED : these aliases will be removed after July 2028
108# import them from `typing`, `io`, or `numpy` instead
109List = list
110Dict = dict
111Tuple = tuple
112Set = set
113Optional = typing.Optional
114Union = typing.Union
115TextIO = typing.TextIO
116BytesIO = io.BytesIO
117StringIO = io.StringIO
118BufferedRandom = io.BufferedRandom
119unsignedinteger = numpy.unsignedinteger
122__all__ = [
123 "IO",
124 "Any",
125 "ArrayLike",
126 "BinaryIO",
127 "BoolIsFile",
128 "Callable",
129 "DTypeLike",
130 "Floating",
131 "Hashable",
132 "HttpSessionLike",
133 "Integer",
134 "Iterable",
135 "Literal",
136 "Loadable",
137 "Mapping",
138 "NDArray",
139 "NDArray1D",
140 "NDArray2D",
141 "NDArray3D",
142 "Number",
143 "Seed",
144 "Self",
145 "Sequence",
146 "ShapeLike",
147 "Stream",
148 "ViewerType",
149 "float64",
150 "int64",
151]