|
| 1 | +import inspect |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from megatron.core.typed_torch import copy_signature, not_none |
| 7 | + |
| 8 | + |
| 9 | +def source_func(a: int, *, b: str) -> str: |
| 10 | + """Sample function to copy the signature from.""" |
| 11 | + return str(a) + b |
| 12 | + |
| 13 | + |
| 14 | +class SourceClass: |
| 15 | + """Sample class with a method to copy the signature from.""" |
| 16 | + |
| 17 | + def method(self, a: int, *, b: str) -> str: |
| 18 | + """Sample method to copy the signature from.""" |
| 19 | + return str(a) + b |
| 20 | + |
| 21 | + |
| 22 | +@copy_signature(source_func) |
| 23 | +def dest_func_from_func(*args: Any, **kwargs: Any) -> list[str]: |
| 24 | + """Function with copied signature from source_func.""" |
| 25 | + return [source_func(*args, **kwargs)] |
| 26 | + |
| 27 | + |
| 28 | +@copy_signature(source_func, handle_return_type='overwrite') |
| 29 | +def dest_func_from_func_overwrite(*args: Any, **kwargs: Any) -> object: |
| 30 | + """Function with copied signature from source_func, but overwritten return type.""" |
| 31 | + return source_func(*args, **kwargs) |
| 32 | + |
| 33 | + |
| 34 | +@copy_signature(SourceClass.method, handle_first_src_param='skip') |
| 35 | +def dest_func_from_method(*args: Any, **kwargs: Any) -> int: |
| 36 | + """Function with copied signature from SourceClass.method.""" |
| 37 | + return len(SourceClass().method(*args, **kwargs)) |
| 38 | + |
| 39 | + |
| 40 | +@copy_signature(SourceClass.method, handle_return_type='overwrite', handle_first_src_param='skip') |
| 41 | +def dest_func_from_method_overwrite(*args: Any, **kwargs: Any) -> object: |
| 42 | + """Function with copied signature from SourceClass.method, but overwritten return type.""" |
| 43 | + return SourceClass().method(*args, **kwargs) |
| 44 | + |
| 45 | + |
| 46 | +class DestClass: |
| 47 | + """Class with methods that have copied signatures.""" |
| 48 | + |
| 49 | + @copy_signature(source_func, handle_first_dst_param='preserve') |
| 50 | + def dest_method_from_func(self, *args: Any, **kwargs: Any) -> list[str]: |
| 51 | + """Method with copied signature from source_func.""" |
| 52 | + return [source_func(*args, **kwargs)] |
| 53 | + |
| 54 | + @copy_signature(source_func, handle_return_type='overwrite', handle_first_dst_param='preserve') |
| 55 | + def dest_method_from_func_overwrite(self, *args: Any, **kwargs: Any) -> object: |
| 56 | + """Method with copied signature from source_func, but overwritten return type.""" |
| 57 | + return source_func(*args, **kwargs) |
| 58 | + |
| 59 | + @classmethod |
| 60 | + @copy_signature( |
| 61 | + SourceClass.method, handle_first_src_param='skip', handle_first_dst_param='preserve' |
| 62 | + ) |
| 63 | + def dest_method_from_method(cls, *args: Any, **kwargs: Any) -> int: |
| 64 | + """Class method with copied signature from SourceClass.method.""" |
| 65 | + return len(SourceClass().method(*args, **kwargs)) |
| 66 | + |
| 67 | + @copy_signature( |
| 68 | + SourceClass.method, |
| 69 | + handle_return_type='overwrite', |
| 70 | + handle_first_src_param='skip', |
| 71 | + handle_first_dst_param='preserve', |
| 72 | + ) |
| 73 | + def dest_method_from_method_overwrite(self, *args: Any, **kwargs: Any) -> object: |
| 74 | + """Method with copied signature from SourceClass.method, but overwritten return type.""" |
| 75 | + return SourceClass().method(*args, **kwargs) |
| 76 | + |
| 77 | + |
| 78 | +class TestCopySignature: |
| 79 | + def test_original_return_type(self): |
| 80 | + """Test that the original return types are preserved.""" |
| 81 | + f2f: list[str] = dest_func_from_func(1, b='a') |
| 82 | + assert f2f == ['1a'] |
| 83 | + assert inspect.signature(dest_func_from_func) == inspect.Signature( |
| 84 | + [ |
| 85 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 86 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 87 | + ], |
| 88 | + return_annotation=list[str], |
| 89 | + ) |
| 90 | + |
| 91 | + m2f: int = dest_func_from_method(1, b='a') |
| 92 | + assert m2f == 2 |
| 93 | + assert inspect.signature(dest_func_from_method) == inspect.Signature( |
| 94 | + [ |
| 95 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 96 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 97 | + ], |
| 98 | + return_annotation=int, |
| 99 | + ) |
| 100 | + |
| 101 | + f2m: list[str] = DestClass().dest_method_from_func( |
| 102 | + 1, b='a' |
| 103 | + ) + DestClass.dest_method_from_func(DestClass(), 1, b='a') |
| 104 | + assert f2m == ['1a', '1a'] |
| 105 | + assert inspect.signature(DestClass.dest_method_from_func) == inspect.Signature( |
| 106 | + [ |
| 107 | + inspect.Parameter('self', inspect.Parameter.POSITIONAL_OR_KEYWORD), |
| 108 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 109 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 110 | + ], |
| 111 | + return_annotation=list[str], |
| 112 | + ) |
| 113 | + |
| 114 | + m2m: int = DestClass.dest_method_from_method(1, b='a') |
| 115 | + assert m2m == 2 |
| 116 | + assert inspect.signature(DestClass.dest_method_from_method) == inspect.Signature( |
| 117 | + [ |
| 118 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 119 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 120 | + ], |
| 121 | + return_annotation=int, |
| 122 | + ) |
| 123 | + |
| 124 | + def test_overwritten_return_type(self): |
| 125 | + """Test that the return types are overwritten correctly.""" |
| 126 | + f2f: str = dest_func_from_func_overwrite(1, b='a') |
| 127 | + assert f2f == '1a' |
| 128 | + assert inspect.signature(dest_func_from_func_overwrite) == inspect.Signature( |
| 129 | + [ |
| 130 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 131 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 132 | + ], |
| 133 | + return_annotation=str, |
| 134 | + ) |
| 135 | + |
| 136 | + m2f: str = dest_func_from_method_overwrite(1, b='a') |
| 137 | + assert m2f == '1a' |
| 138 | + assert inspect.signature(dest_func_from_method_overwrite) == inspect.Signature( |
| 139 | + [ |
| 140 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 141 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 142 | + ], |
| 143 | + return_annotation=str, |
| 144 | + ) |
| 145 | + |
| 146 | + f2m: str = DestClass().dest_method_from_func_overwrite( |
| 147 | + 1, b='a' |
| 148 | + ) + DestClass.dest_method_from_func_overwrite(DestClass(), 1, b='a') |
| 149 | + assert f2m == '1a1a' |
| 150 | + assert inspect.signature(DestClass.dest_method_from_func_overwrite) == inspect.Signature( |
| 151 | + [ |
| 152 | + inspect.Parameter('self', inspect.Parameter.POSITIONAL_OR_KEYWORD), |
| 153 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 154 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 155 | + ], |
| 156 | + return_annotation=str, |
| 157 | + ) |
| 158 | + |
| 159 | + m2m: str = DestClass().dest_method_from_method_overwrite(1, b='a') |
| 160 | + assert m2m == '1a' |
| 161 | + assert inspect.signature(DestClass.dest_method_from_method_overwrite) == inspect.Signature( |
| 162 | + [ |
| 163 | + inspect.Parameter('self', inspect.Parameter.POSITIONAL_OR_KEYWORD), |
| 164 | + inspect.Parameter('a', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int), |
| 165 | + inspect.Parameter('b', inspect.Parameter.KEYWORD_ONLY, annotation=str), |
| 166 | + ], |
| 167 | + return_annotation=str, |
| 168 | + ) |
| 169 | + |
| 170 | + |
| 171 | +class TestNotNone: |
| 172 | + """Tests not_none.""" |
| 173 | + |
| 174 | + def test_none(self): |
| 175 | + """Test that passing None raises a ValueError.""" |
| 176 | + with pytest.raises(ValueError, match=r'Expected value to be not None'): |
| 177 | + not_none(None) |
| 178 | + |
| 179 | + def test_not_none(self): |
| 180 | + """Test that passing a non-None value returns the value.""" |
| 181 | + value = 42 |
| 182 | + result = not_none(value) |
| 183 | + assert result == value |
0 commit comments