mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-14 09:34:46 +00:00
6b52c838d7
This allows a slightly cleaner base type specification of: py::class_<Type, Base>("Type") as an alternative to py::class_<Type>("Type", py::base<Base>()) As with the other template parameters, the order relative to the holder or trampoline types doesn't matter. This also includes a compile-time assertion failure if attempting to specify more than one base class (but is easily extendible to support multiple inheritance, someday, by updating the class_selector::set_bases function to set multiple bases).
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
import pytest
|
|
|
|
|
|
def test_inheritance(msg):
|
|
from pybind11_tests import Pet, Dog, Rabbit, Hamster, dog_bark, pet_name_species
|
|
|
|
roger = Rabbit('Rabbit')
|
|
assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
|
|
assert pet_name_species(roger) == "Rabbit is a parrot"
|
|
|
|
polly = Pet('Polly', 'parrot')
|
|
assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
|
|
assert pet_name_species(polly) == "Polly is a parrot"
|
|
|
|
molly = Dog('Molly')
|
|
assert molly.name() + " is a " + molly.species() == "Molly is a dog"
|
|
assert pet_name_species(molly) == "Molly is a dog"
|
|
|
|
fred = Hamster('Fred')
|
|
assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
|
|
|
|
assert dog_bark(molly) == "Woof!"
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
dog_bark(polly)
|
|
assert msg(excinfo.value) == """
|
|
Incompatible function arguments. The following argument types are supported:
|
|
1. (arg0: m.Dog) -> str
|
|
Invoked with: <m.Pet object at 0>
|
|
"""
|
|
|
|
|
|
def test_automatic_upcasting():
|
|
from pybind11_tests import return_class_1, return_class_2, return_none
|
|
|
|
assert type(return_class_1()).__name__ == "DerivedClass1"
|
|
assert type(return_class_2()).__name__ == "DerivedClass2"
|
|
assert type(return_none()).__name__ == "NoneType"
|