|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import Union, overload, List |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from cognite.client.exceptions import CogniteTypeError |
| 9 | +from cognite.client.utils._runtime_type_checking import runtime_type_checked_public_methods |
| 10 | + |
| 11 | + |
| 12 | +class Foo: |
| 13 | + ... |
| 14 | + |
| 15 | + |
| 16 | +class TestTypes: |
| 17 | + @runtime_type_checked_public_methods |
| 18 | + class Types: |
| 19 | + def primitive(self, x: int) -> None: |
| 20 | + ... |
| 21 | + |
| 22 | + def list(self, x: List[str]) -> None: |
| 23 | + ... |
| 24 | + |
| 25 | + def custom_class(self, x: Foo) -> None: |
| 26 | + ... |
| 27 | + |
| 28 | + def test_primitive(self) -> None: |
| 29 | + with pytest.raises( |
| 30 | + CogniteTypeError, |
| 31 | + match=re.escape( |
| 32 | + "Method tests.tests_unit.test_utils.test_runtime_type_checking.TestTypes.Types.primitive() " |
| 33 | + "parameter x='1' violates type hint <class 'int'>, as str '1' not instance of int." |
| 34 | + ), |
| 35 | + ): |
| 36 | + self.Types().primitive("1") |
| 37 | + |
| 38 | + self.Types().primitive(1) |
| 39 | + |
| 40 | + def test_list(self) -> None: |
| 41 | + with pytest.raises( |
| 42 | + CogniteTypeError, |
| 43 | + match=re.escape( |
| 44 | + "Method tests.tests_unit.test_utils.test_runtime_type_checking.TestTypes.Types.list() parameter x='1' " |
| 45 | + "violates type hint typing.List[str], as str '1' not instance of list." |
| 46 | + ), |
| 47 | + ): |
| 48 | + self.Types().list("1") |
| 49 | + |
| 50 | + with pytest.raises( |
| 51 | + CogniteTypeError, |
| 52 | + match=re.escape( |
| 53 | + "Method tests.tests_unit.test_utils.test_runtime_type_checking.TestTypes.Types.list() parameter x=[1] " |
| 54 | + "violates type hint typing.List[str], as list index 0 item int 1 not instance of str." |
| 55 | + ), |
| 56 | + ): |
| 57 | + self.Types().list([1]) |
| 58 | + |
| 59 | + self.Types().list(["ok"]) |
| 60 | + |
| 61 | + def test_custom_type(self) -> None: |
| 62 | + with pytest.raises( |
| 63 | + CogniteTypeError, |
| 64 | + match=re.escape( |
| 65 | + "Method tests.tests_unit.test_utils.test_runtime_type_checking.TestTypes.Types.custom_class() " |
| 66 | + "parameter x='1' violates type hint " |
| 67 | + "<class 'tests.tests_unit.test_utils.test_runtime_type_checking.Foo'>, as str '1' not instance " |
| 68 | + 'of <class "tests.tests_unit.test_utils.test_runtime_type_checking.Foo">' |
| 69 | + ), |
| 70 | + ): |
| 71 | + self.Types().custom_class("1") |
| 72 | + |
| 73 | + self.Types().custom_class(Foo()) |
| 74 | + |
| 75 | + |
| 76 | +class TestOverloads: |
| 77 | + @runtime_type_checked_public_methods |
| 78 | + class WithOverload: |
| 79 | + @overload |
| 80 | + def foo(self, x: int, y: int) -> str: |
| 81 | + ... |
| 82 | + |
| 83 | + @overload |
| 84 | + def foo(self, x: str, y: str) -> str: |
| 85 | + ... |
| 86 | + |
| 87 | + def foo(self, x: Union[int, str], y: Union[int, str]) -> str: |
| 88 | + return f"{x}{y}" |
| 89 | + |
| 90 | + def test_overloads( |
| 91 | + self, |
| 92 | + ) -> None: |
| 93 | + with pytest.raises( |
| 94 | + CogniteTypeError, |
| 95 | + match=re.escape( |
| 96 | + "Method tests.tests_unit.test_utils.test_runtime_type_checking.TestOverloads.WithOverload.foo() " |
| 97 | + "parameter y=1.0 violates type hint typing.Union[int, str], as float 1.0 not int or str." |
| 98 | + ), |
| 99 | + ): |
| 100 | + self.WithOverload().foo(1, 1.0) |
| 101 | + |
| 102 | + # Technically should raise a CogniteTypeError, but beartype isn't very good with overloads yet |
| 103 | + self.WithOverload().foo(1, "1") |
0 commit comments