Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-11 08:49:45

0001 /*
0002     pybind11/typing.h: Convenience wrapper classes for basic Python types
0003     with more explicit annotations.
0004 
0005     Copyright (c) 2023 Dustin Spicuzza <dustin@virtualroadside.com>
0006 
0007     All rights reserved. Use of this source code is governed by a
0008     BSD-style license that can be found in the LICENSE file.
0009 */
0010 
0011 #pragma once
0012 
0013 #include "detail/common.h"
0014 #include "cast.h"
0015 #include "pytypes.h"
0016 
0017 #include <algorithm>
0018 
0019 #if defined(__cpp_nontype_template_args) && __cpp_nontype_template_args >= 201911L
0020 #    define PYBIND11_TYPING_H_HAS_STRING_LITERAL
0021 #    include <numeric>
0022 #    include <ranges>
0023 #    include <string_view>
0024 #endif
0025 
0026 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0027 PYBIND11_NAMESPACE_BEGIN(typing)
0028 
0029 /*
0030     The following types can be used to direct pybind11-generated docstrings
0031     to have have more explicit types (e.g., `list[str]` instead of `list`).
0032     Just use these in place of existing types.
0033 
0034     There is no additional enforcement of types at runtime.
0035 */
0036 
0037 template <typename... Types>
0038 class Tuple : public tuple {
0039     using tuple::tuple;
0040 };
0041 
0042 template <typename K, typename V>
0043 class Dict : public dict {
0044     using dict::dict;
0045 };
0046 
0047 template <typename T>
0048 class List : public list {
0049     using list::list;
0050 };
0051 
0052 template <typename T>
0053 class Set : public set {
0054     using set::set;
0055 };
0056 
0057 template <typename T>
0058 class Iterable : public iterable {
0059     using iterable::iterable;
0060 };
0061 
0062 template <typename T>
0063 class Iterator : public iterator {
0064     using iterator::iterator;
0065 };
0066 
0067 template <typename Signature>
0068 class Callable;
0069 
0070 template <typename Return, typename... Args>
0071 class Callable<Return(Args...)> : public function {
0072     using function::function;
0073 };
0074 
0075 template <typename T>
0076 class Type : public type {
0077     using type::type;
0078 };
0079 
0080 template <typename... Types>
0081 class Union : public object {
0082     PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type)
0083     using object::object;
0084 };
0085 
0086 template <typename T>
0087 class Optional : public object {
0088     PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type)
0089     using object::object;
0090 };
0091 
0092 template <typename T>
0093 class Final : public object {
0094     PYBIND11_OBJECT_DEFAULT(Final, object, PyObject_Type)
0095     using object::object;
0096 };
0097 
0098 template <typename T>
0099 class ClassVar : public object {
0100     PYBIND11_OBJECT_DEFAULT(ClassVar, object, PyObject_Type)
0101     using object::object;
0102 };
0103 
0104 template <typename T>
0105 class TypeGuard : public bool_ {
0106     using bool_::bool_;
0107 };
0108 
0109 template <typename T>
0110 class TypeIs : public bool_ {
0111     using bool_::bool_;
0112 };
0113 
0114 class NoReturn : public none {
0115     using none::none;
0116 };
0117 
0118 class Never : public none {
0119     using none::none;
0120 };
0121 
0122 #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
0123 template <size_t N>
0124 struct StringLiteral {
0125     constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
0126     char name[N];
0127 };
0128 
0129 template <StringLiteral... StrLits>
0130 class Literal : public object {
0131     PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type)
0132 };
0133 
0134 // Example syntax for creating a TypeVar.
0135 // typedef typing::TypeVar<"T"> TypeVarT;
0136 template <StringLiteral>
0137 class TypeVar : public object {
0138     PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
0139     using object::object;
0140 };
0141 #endif
0142 
0143 PYBIND11_NAMESPACE_END(typing)
0144 
0145 PYBIND11_NAMESPACE_BEGIN(detail)
0146 
0147 template <typename... Types>
0148 struct handle_type_name<typing::Tuple<Types...>> {
0149     static constexpr auto name = const_name("tuple[")
0150                                  + ::pybind11::detail::concat(make_caster<Types>::name...)
0151                                  + const_name("]");
0152 };
0153 
0154 template <>
0155 struct handle_type_name<typing::Tuple<>> {
0156     // PEP 484 specifies this syntax for an empty tuple
0157     static constexpr auto name = const_name("tuple[()]");
0158 };
0159 
0160 template <typename T>
0161 struct handle_type_name<typing::Tuple<T, ellipsis>> {
0162     // PEP 484 specifies this syntax for a variable-length tuple
0163     static constexpr auto name
0164         = const_name("tuple[") + make_caster<T>::name + const_name(", ...]");
0165 };
0166 
0167 template <typename K, typename V>
0168 struct handle_type_name<typing::Dict<K, V>> {
0169     static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ")
0170                                  + make_caster<V>::name + const_name("]");
0171 };
0172 
0173 template <typename T>
0174 struct handle_type_name<typing::List<T>> {
0175     static constexpr auto name = const_name("list[") + make_caster<T>::name + const_name("]");
0176 };
0177 
0178 template <typename T>
0179 struct handle_type_name<typing::Set<T>> {
0180     static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
0181 };
0182 
0183 template <typename T>
0184 struct handle_type_name<typing::Iterable<T>> {
0185     static constexpr auto name
0186         = const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
0187 };
0188 
0189 template <typename T>
0190 struct handle_type_name<typing::Iterator<T>> {
0191     static constexpr auto name
0192         = const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
0193 };
0194 
0195 template <typename Return, typename... Args>
0196 struct handle_type_name<typing::Callable<Return(Args...)>> {
0197     using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
0198     static constexpr auto name
0199         = const_name("collections.abc.Callable[[")
0200           + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
0201           + const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
0202           + const_name("]");
0203 };
0204 
0205 template <typename Return>
0206 struct handle_type_name<typing::Callable<Return(ellipsis)>> {
0207     // PEP 484 specifies this syntax for defining only return types of callables
0208     using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
0209     static constexpr auto name = const_name("collections.abc.Callable[..., ")
0210                                  + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
0211                                  + const_name("]");
0212 };
0213 
0214 template <typename T>
0215 struct handle_type_name<typing::Type<T>> {
0216     static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
0217 };
0218 
0219 template <typename... Types>
0220 struct handle_type_name<typing::Union<Types...>> {
0221     static constexpr auto name = ::pybind11::detail::union_concat(make_caster<Types>::name...);
0222 };
0223 
0224 template <typename T>
0225 struct handle_type_name<typing::Optional<T>> {
0226     static constexpr auto name = make_caster<T>::name | make_caster<none>::name;
0227 };
0228 
0229 template <typename T>
0230 struct handle_type_name<typing::Final<T>> {
0231     static constexpr auto name = const_name("typing.Final[")
0232                                  + ::pybind11::detail::return_descr(make_caster<T>::name)
0233                                  + const_name("]");
0234 };
0235 
0236 template <typename T>
0237 struct handle_type_name<typing::ClassVar<T>> {
0238     static constexpr auto name
0239         = const_name("typing.ClassVar[") + make_caster<T>::name + const_name("]");
0240 };
0241 
0242 template <typename T>
0243 struct handle_type_name<typing::TypeGuard<T>> {
0244     static constexpr auto name = const_name(PYBIND11_TYPE_GUARD_TYPE_HINT) + const_name("[")
0245                                  + make_caster<T>::name + const_name("]");
0246 };
0247 
0248 template <typename T>
0249 struct handle_type_name<typing::TypeIs<T>> {
0250     static constexpr auto name = const_name(PYBIND11_TYPE_IS_TYPE_HINT) + const_name("[")
0251                                  + make_caster<T>::name + const_name("]");
0252 };
0253 
0254 template <>
0255 struct handle_type_name<typing::NoReturn> {
0256     static constexpr auto name = const_name("typing.NoReturn");
0257 };
0258 
0259 template <>
0260 struct handle_type_name<typing::Never> {
0261     static constexpr auto name = const_name(PYBIND11_NEVER_TYPE_HINT);
0262 };
0263 
0264 #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
0265 template <typing::StringLiteral StrLit>
0266 consteval auto sanitize_string_literal() {
0267     constexpr std::string_view v(StrLit.name);
0268     constexpr std::string_view special_chars("!@%{}-");
0269     constexpr auto num_special_chars = std::accumulate(
0270         special_chars.begin(), special_chars.end(), (size_t) 0, [&v](auto acc, const char &c) {
0271             return std::move(acc) + std::ranges::count(v, c);
0272         });
0273     char result[v.size() + num_special_chars + 1];
0274     size_t i = 0;
0275     for (auto c : StrLit.name) {
0276         if (special_chars.find(c) != std::string_view::npos) {
0277             result[i++] = '!';
0278         }
0279         result[i++] = c;
0280     }
0281     return typing::StringLiteral(result);
0282 }
0283 
0284 template <typing::StringLiteral... Literals>
0285 struct handle_type_name<typing::Literal<Literals...>> {
0286     static constexpr auto name
0287         = const_name("typing.Literal[")
0288           + pybind11::detail::concat(const_name(sanitize_string_literal<Literals>().name)...)
0289           + const_name("]");
0290 };
0291 template <typing::StringLiteral StrLit>
0292 struct handle_type_name<typing::TypeVar<StrLit>> {
0293     static constexpr auto name = const_name(sanitize_string_literal<StrLit>().name);
0294 };
0295 #endif
0296 
0297 PYBIND11_NAMESPACE_END(detail)
0298 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)