Skip to content

Commit 0273cbd

Browse files
committed
Add support for Python 3.14
Resolves #35
1 parent 6bc20d7 commit 0273cbd

4 files changed

Lines changed: 46 additions & 11 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
matrix:
88
# All available versions of Python:
99
# https://github.com/actions/python-versions/blob/main/versions-manifest.json
10-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
10+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
1111
fail-fast: false
1212
steps:
1313
- name: Checkout code

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ raised exceptions, and other details.
524524
525525
* See the [Roadmap](https://github.com/davidfstr/trycast/wiki/Roadmap).
526526
527+
### main (v1.3.0)
528+
529+
* Add support for Python 3.14.
530+
527531
### v1.2.1
528532
529533
* Fix `checkcast()` to declare that it returns a `T` rather than `T | None`.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ classifiers = [ # https://pypi.org/classifiers/
2121
"Programming Language :: Python :: 3.10",
2222
"Programming Language :: Python :: 3.11",
2323
"Programming Language :: Python :: 3.12",
24+
"Programming Language :: Python :: 3.13",
25+
"Programming Language :: Python :: 3.14",
2426
"Topic :: Software Development",
2527
"Typing :: Typed"
2628
]
@@ -87,7 +89,7 @@ line_length = 88
8789
legacy_tox_ini = """
8890
8991
[tox]
90-
envlist = py38,py39,py310,py311,py312
92+
envlist = py38,py39,py310,py311,py312,py313,py314
9193
isolated_build = True
9294
9395
[testenv]

tests.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import os
44
import platform
5+
import re
56
import subprocess
67
import sys
78
import typing
@@ -2880,15 +2881,43 @@ def assertRaisesEqual(
28802881

28812882
@staticmethod
28822883
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]
28922921

28932922

28942923
class TestValidationError(TestCase):

0 commit comments

Comments
 (0)