* fix future type hints
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* remove unused var
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove union_helper
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* fix speelling error
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* base case for union_concat
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* add case for one descr
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* weakref and final test
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Add acrpss_version_type_hint_checker
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* cleanup
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* remove test.pyi
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* use new unions and add fixture
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* timohl suggested cleanup
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* add missing auto
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* move operator| def
---------
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Fix Python 3.8 type hints and add module prefix
These type hints are invalid in Python 3.8.
Add `typing.` prefix to remove ambiguity.
* style: pre-commit fixes
* Add module prefix to Union
* Rename macros
* Improve comment
* Comment out 3.8 type hint macros
Fixing this issue in Python 3.8 will require updating lots of tests. This can be added in a further pull request.
* Add Iterable module prefix
* Add module prefix to Iterator
* Add module prefix to Callable
* Re-add accidentally deleted brackets
* Add module prefix to Optional
* Add module prefix to Final
* Add module prefix to ClassVar
* Add module prefix to TypeGuard
* Add module prefix to TypeIs
* Add module prefix to NoReturn
* Add module prefix to Never
* Add module prefix to Literal
* Add module prefix to Callable
* Add module prefix to Sequence
* Add module prefix to Iterator
* style: pre-commit fixes
* Remove type hint macros
* style: pre-commit fixes
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* ci: support Python 3.14
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: Python 3.14 name change
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* tests: fix expected output to handle Python 3.14
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: tighten CLI and add color on 3.14+
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* tests: ignore failure on 3.14.0b1
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: support Python 3.14.0b1 with interperters
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* Update test_multiple_interpreters.py
* Update test_multiple_interpreters.py
* fix: new breakage for 3.14 fixed
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: handle empty annotations 3.14
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: Python 3.14 may not create the annotations dict
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: use PyUnstable_IsImmortal
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: use sys._is_immortal
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* tests: ignore large values for refcount too
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* style: pre-commit fixes
* ci: enable all free-threaded builds
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: patch for embed
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* Revert "fix: patch for embed"
This reverts commit c4226a0671.
* ci: drop new 3.xt additions
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: logic issue, also add some comments
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* Update include/pybind11/pytypes.h
---------
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Updated STL type hints use support collections.abc
* Updated array_caster to match numpy/eigen typing.Annotated stlye
* Added support for Mapping, Set and Sequence derived from collections.abc.
* Fixed merge of typing.SupportsInt in new tests
* Integrated collections.abc checks into convertible check functions.
* Changed type hint of py::buffer to collections.abc.Buffer
* Changed convertible check function names
* Added comments to convertible check functions
* Removed checks for methods that are already required by the abstract base class
* Improved mapping caster test using more compact a1b2c3 variable
* Renamed and refactored sequence, mapping and set test classes to reuse implementation
* Added tests for mapping and set casters for noconvert mode
* Added tests for sequence caster for noconvert mode
* init
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove import
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove uneeded function
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Add missing import
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Fix type behind detailed_message_enabled flag
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Fix type behind detailed_message_enabled flag
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Add io_name comment
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Extra loops to single function
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Remove unneeded forward declaration
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* clang-tidy
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove stack import
* Fix bug in std::function Callable type
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* remove is_annotation argument
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Update function name and arg names
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
---------
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Added rework of arg/return typing
* Changed `Path` to `pathlib.Path` for compatibility with pybind11-stubgen
* Removed old arg/return type hint implementation
* Added noconvert support for arg/return type hints
* Added commented failing tests for Literals with special characters
* Added return_descr/arg_descr for correct typing in typing::Callable
* Fixed clang-tidy issues
* Changed io_name to have explicit return type (for C++11 support)
* style: pre-commit fixes
* Added support for nested callables
* Fixed missing include
* Fixed is_return_value constructor call
* Fixed clang-tidy issue
* Uncommented test cases for special characters in literals
* Moved literal tests to correct test case
* Added escaping of special characters in typing::Literal
* Readded mistakenly deleted bracket
* Moved sanitize_string_literal to correct namespace
* Added test for Literal with `!` and changed StringLiteral template param name
* Added test for Literal with multiple and repeated special chars
* Simplified string literal sanitization function
* Added test for `->` in literal
* Added test for `->` with io_name
* Removed unused parameter name to prevent warning
* Added escaping of `-` in literal to prevent processing of `->`
* Fixed wrong computation of sanitized string literal length
* Added cast to prevent error with MSVC
* Simplified special character check
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Added arg/return type handling.
* Added support for nested arg/return type in py::typing::List
* Added support for arg/return type in stl/filesystem
* Added tests for arg/return type in stl/filesystem and py::typing::List
* Added arg/return name to more py::typing classes
* Added arg/return type to Callable[...]
* Added tests for typing container classes (also nested)
* Changed typing classes to avoid using C++14 auto return type deduction.
* Fixed clang-tidy errors.
* Changed Enable to SFINAE
* Added test for Tuple[T, ...]
* Added RealNumber with custom caster for testing typing classes.
* Added tests for Set, Iterable, Iterator, Union, and Optional
* Added tests for Callable
* Fixed Callable with ellipsis test
* Changed TypeGuard/TypeIs to use return type (being the narrower type) + Tests
* Added test for use of fallback type name with stl vector
* Updated documentation.
* Fixed unnecessary constructor call in test.
* Fixed reference counting in example type caster.
* Fixed clang-tidy issues.
* Fix for clang-tidy
* Updated cast method to use pybind11 API rather than Python C API in custom caster example
* Updated load to use pybind11 API rather than Python C API in custom caster example
* Changed test of arg/return name to use pybind11 API instead of Python C API
* Updated code in adcanced/cast example and improved documentation text
* Fixed references in custom type caster docs
* Fixed wrong logical and operator in test
* Fixed wrong logical operator in doc example
* Added comment to test about `float` vs `float | int`
* Updated std::filesystem::path docs in cast/overview section
* Remove one stray dot.
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* Initial support for GraalPy
* Mark tests that currently fail on GraalPy with xfail
* Add graalpy to CI
* Limit test deps on graalpy to available binary wheels
* Skip cmake test installed_function on GraalPy
CMake won't find libpython on GraalPy, it either fails or silently picks
CPython's libpython.
* Factor out setting function docstrings into a macro
* Try to narrow down skipped tests
* fix: use `__cpp_nontype_template_args` instead of gnu extensions
* fix: add feature test value
* fix: change `PYBIND11_TYPING_H_HAS_STRING_LITERAL` skip reason
* feat: add `<ranges>` support for `py::tuple` and `py::list`
* fix: format the code
* fix: disable `ranges` in clang < 16
* refactor: move `<ranges>` test macro to `test_pytypes.h`
* refactor: seperate `ranges` test into 3 funcs
* style: compress the if statement
* style: pre-commit fixes
* style: better formatting
---------
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Adds support for TypeGuard and TypeIs
* style: pre-commit fixes
---------
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add a pybing function to clear a list.
* Add required error handling.
* Add `/* py-non-const */` as suggested by @Skylion007
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
* ci: try Python 3.13t
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: support Python 3.13t
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* fix: patch PyPy
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* tests: one more int cast
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* tests: cleanup
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* refactor: use named constant in tests for immortal refcounts
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* docs: move comment about free threaded Python
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
---------
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* Transfer bug fixes from #4888 wholesale. Full test coverage for all fixes is still missing.
* Add cmake option(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION) and use in some tests.
* Provide better type hints for a variety of generic types
* Makes better documentation
* tuple, dict, list, set, function
* Move to py::typing
* style: pre-commit fixes
* Update copyright line with correct year and actual author. The author information was copy-pasted from the git log output.
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* (bugfix): Improve bytes to str decoding error handling
* regroup test
* Further broaden tests
* Add another decode error test
* Fix bug in tests
* Reviewer suggestions
* bugfix: contains now throws an exception if the key is not hashable
* Fix tests and improve robustness
* Remove todo
* Workaround PyPy corner case
* PyPy xfail
* Fix typo
* fix xfail
* Make clang-tidy happy
* Remove redundant exc checking
* fix: PyCapsule_GetDestructor is allowed to return a nullptr destructor
Previously, this code would error out if the destructor happened to be
a nullptr. This is incorrect. nullptrs are allowed for capsule
destructors.
"It is legal for a capsule to have a NULL destructor. This makes a
NULL return code somewhat ambiguous; use PyCapsule_IsValid() or
PyErr_Occurred() to disambiguate."
See:
https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_GetDestructor
I noticed this while working on a type caster related to #3858 DLPack
happens to allow the destructor not to be defined on a capsule, and I
encountered such a case. See:
e2bdd3bee8/include/dlpack/dlpack.h (L219)
* Add test for the fix.
* Update tests/test_pytypes.cpp
I tried this locally and it works!
I never knew that there are cases where `reinterpret_cast` does not work but `static_cast` does. Let's see if all compilers are happy with this.
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
* style: pre-commit fixes
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgkio@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Disable implicit conversion from `0` to `pybind11::handle`.
* Reverse or-ed condition in an attempt to resolve GCC 8.3.0 errors (i386/debian:buster).
* Trying the simpler `std::is_same<T, PyObject *>`
* Add implicit_conversion_from_pytorch_THPObjectPtr_to_handle test.
* Accommodate types with implicit conversions to `PyObject *`, other than `handle` & `handle` subclasses, or integral types.
* Fix copy-paste mishap (picked wrong name).
* Revamp SFINAE construct to actually fix the pytorch issue (already validated against pytorch proper).
The first version of the reduced pytorch code was critically missing the move ctor. The first version of the accompanying test was meaningless.
Note: It turns out the `!std::is_arithmetic<T>` condition is not needed: `int` is not in general implicitly convertible to `PyObject *`, only the literal `0` is.
* Use `NOLINT(performance-noexcept-move-constructor)` for reduced code from the wild (rather than changing the code).
* Use any_of, all_of, negation. It turns out to clang-format nicer.
* Clean up comments for changed code.
* Reduce pytorch situation further, add test for operator ... const.
* Use `none_of` as suggested by @skylion007
* Add `pure_compile_tests_for_handle_from_PyObject_pointers()`
* Fix inconsequential oversight (retested).
* Factor our `is_pyobj_ptr_or_nullptr_t` to make the SFINAE conditions more readable.
* Remove stray line (oversight).
* Make the `pure_compile_tests_for_handle_from_PyObject_pointers()` "rhs-const-complete", too.
* Remove the temporary PYBIND11_UNDO_PR4008 `#ifdef`.
* Add test_perf_accessors (to be merged into test_pytypes).
* Python < 3.8 f-string compatibility
* Use thread_local in inc_ref_counter()
* Intentional breakage, brute-force way to quickly find out how many platforms reach the PYBIND11_HANDLE_REF_DEBUG code, with and without threads.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove Intentional breakage
* Drop perf test, move inc_refs tests to test_pytypes
* Fold in PR #3970 with `#ifdef`s
* Complete test coverage for all newly added code.
* Condense new unit tests via a simple local helper macro.
* Remove PYBIND11_PR3970 define. See https://github.com/pybind/pybind11/pull/3977#issuecomment-1142526417
* Move static keyword first (fixes silly oversight).
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add frozenset, and allow it cast to std::set
For the reverse direction, std::set still casts to set. This is in concordance with the behavior for sequence containers, where e.g. tuple casts to std::vector but std::vector casts to list.
Extracted from #3886.
* Rename set_base to any_set to match Python C API
since this will be part of pybind11 public API
* PR: static_cast, anyset
* Add tests for frozenset
and rename anyset methods
* Remove frozenset default ctor, add tests
Making frozenset non-default constructible means that we need to adjust pyobject_caster to not require that its value is default constructible, by initializing value to a nil handle. This also allows writing C++ functions taking anyset, and is arguably a performance improvement, since there is no need to allocate an object that will just be replaced by load.
Add some more tests, including anyset::empty, anyset::size, set::add and set::clear.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add rationale to `pyobject_caster` default ctor
* Remove ineffectual protected: access control
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Update cast.h
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Move definition to detail/common, change name, apply everywhere
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Rename debug_enabled in tests to detailed_error_messages_enabled
* Adds set_name method of pybind11::capsule class
This calls PyCapsule_SetName on the underlying capsule object.
modified destructors to query capsules's Name
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Handle possible exception thrown by PyCapsule_GetName
Also removed accidentally reintroduced use of `const char *&`.
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
Fixed function name
* Introduced private static function to reuse get_name_or_throw
* added tests for capsule renaming
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* handle python error in flight
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Initialized PyObject * variables to nullptr
* use write-unraisable if PyCapsule_GetName raises
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* get_name_or_throw->get_name_no_throw
If PyCapsule_GetName raises an error we should write as unraisable
to consume it and notify user, and then restore the error in flight if any.
This way this method called from destructor would not modify interpreter
error state.
* used error_scope struct
* Renamed get_name_no_throw->get_name_in_error_scope
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* PYBIND11_OBJECT_CVT should use namespace for error_already_set()
This change makes the macro usable outside of pybind11 namespace.
* added test for use of PYBIND11_OBJECT_CVT for classes in external to pybind11 namespaces
* Extended test_pytypes.cpp and test_pytest.py
The added test defines a dummy function that takes a custom-defined class external::float_
that uses PYBIND11_OBJECT_CVT
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed issues pointed out by CI
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed memory leak in default constructor
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Clear Python error state if pybind11::weakref() fails.
The weakref() constructor calls pybind11_fail() without clearing any
Python interpreter error state. If a client catches the C++ exception
thrown by pybind11_fail(), the Python interpreter will be left in an
error state.
* Add test case for failing to create weakref
* Add Debug asserts for pybind11 fail
* Make error handling more pythonic
* Does this fix PyPy?
* Adapt test to PyPy differences
* Simplify test to remove redundancy
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>