|
2 | 2 | import functools |
3 | 3 | import os |
4 | 4 | import platform |
| 5 | +import re |
5 | 6 | import subprocess |
6 | 7 | import sys |
7 | 8 | import typing |
@@ -2880,15 +2881,43 @@ def assertRaisesEqual( |
2880 | 2881 |
|
2881 | 2882 | @staticmethod |
2882 | 2883 | def _typing_error_messages(template: str) -> List[str]: |
2883 | | - python_lt_3_9 = ( |
2884 | | - template.replace("Literal", "typing.Literal") |
2885 | | - .replace("NoReturn", "typing.NoReturn") |
2886 | | - .replace("Union", "typing.Union") |
2887 | | - ) |
2888 | | - return [ |
2889 | | - template, |
2890 | | - python_lt_3_9, # for Python <=3.9 |
2891 | | - ] |
| 2884 | + # In Python 3.14+, Union[T1, T2] is represented as "T1 | T2" instead of "Union[T1, T2]". |
| 2885 | + if sys.version_info >= (3, 14): |
| 2886 | + # Convert "Union[...]" to "... | ..." format |
| 2887 | + def convert_union(match: "re.Match[str]") -> str: |
| 2888 | + content = match.group(1) |
| 2889 | + # Split by comma but not within brackets |
| 2890 | + parts = [] |
| 2891 | + bracket_depth = 0 |
| 2892 | + current_part = [] |
| 2893 | + for char in content: |
| 2894 | + if char in "[<(": |
| 2895 | + bracket_depth += 1 |
| 2896 | + current_part.append(char) |
| 2897 | + elif char in "]>)": |
| 2898 | + bracket_depth -= 1 |
| 2899 | + current_part.append(char) |
| 2900 | + elif char == "," and bracket_depth == 0: |
| 2901 | + parts.append("".join(current_part).strip()) |
| 2902 | + current_part = [] |
| 2903 | + else: |
| 2904 | + current_part.append(char) |
| 2905 | + parts.append("".join(current_part).strip()) |
| 2906 | + return " | ".join(parts) |
| 2907 | + |
| 2908 | + converted = re.sub(r"Union\[([^\]]+)\]", convert_union, template) |
| 2909 | + return [converted] |
| 2910 | + elif sys.version_info >= (3, 10): |
| 2911 | + # Python 3.10 - 3.13 |
| 2912 | + return [template] |
| 2913 | + else: |
| 2914 | + # Python <=3.9 |
| 2915 | + python_lt_3_9 = ( |
| 2916 | + template.replace("Literal", "typing.Literal") |
| 2917 | + .replace("NoReturn", "typing.NoReturn") |
| 2918 | + .replace("Union", "typing.Union") |
| 2919 | + ) |
| 2920 | + return [python_lt_3_9] |
2892 | 2921 |
|
2893 | 2922 |
|
2894 | 2923 | class TestValidationError(TestCase): |
|
0 commit comments