1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
from __future__ import annotations
import sys
import pytest
from overrides import override
class A:
def f(self) -> int | str:
return 1
def test_should_allow_reducing_type():
class B(A):
@override
def f(self) -> int:
return 1
assert B().f() == 1
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires Python3.10 or higher")
def test_should_not_allow_increasing_type():
with pytest.raises(TypeError):
class C(A):
@override
def f(self) -> int | str | list[str]:
return []
assert False, "should not go here"
|